[GraphQL] Write a GraphQL Schema in JavaScript

Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but as our application grows, or when we start using more complex types like interfaces or unions, we find that we can’t use a GraphQL Language file in the same way as before. In this video, we’ll learn how to translate a GraphQL Schema written in GraphQL into a GraphQL Schema written in JavaScript.

const express   = require(‘express‘);
const graphqlHttp = require(‘express-graphql‘);

const server = express();
const port   = process.env.PORT || 3000;

const {
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLBoolean,
    GraphQLID
      } = require(‘graphql‘);

const videoType = new GraphQLObjectType({
    name: ‘video‘,
    description: ‘A video on Egghead.io‘,
    fields: {
        id: {
            type: GraphQLID,
            description: ‘The id of the video‘
        },
        title: {
            type: GraphQLString,
            description: ‘The title of the video‘
        },
        duration: {
            type: GraphQLInt,
            description: ‘The duration of the video‘
        },
        watched: {
            type: GraphQLBoolean,
            description: ‘Whether or no the viewer watched the video‘
        }
    }
})

const queryType = new GraphQLObjectType({
    name: ‘QueryType‘,
    description: ‘The root query type‘,
    fields :{
        video: {
            type: videoType,
            resolve: () => {
                return new Promise((resolve) => {
                    resolve({
                        id: ‘a‘,
                        title: ‘GraphQL‘,
                        duration: 180,
                        watched: false })
                })
            }
        }
    }
});

const schema = new GraphQLSchema({
    query: queryType
});

/*
const schema = buildSchema(`
    type Video {
        id: ID,
        title: String,
        duration: Int,
        watched: Boolean
    }

    type Query {
        video: Video,
        videos: [Video]
    }

    type Schema{
        query: Query
    }
`);

const videos = [
    {
        id       : ‘1‘,
        title    : ‘react‘,
        duration : 180,
        watched  : true
    },
    {
        id       : ‘2‘,
        title    : ‘relay‘,
        duration : 230,
        watched  : false
    }
];

const resolvers = {
    video  : () => ({
        id       : ‘1‘,
        title    : ‘bar‘,
        duration : 180,
        watched  : true
    }),
    videos : () => videos
};*/

server.use(‘/graphql‘, graphqlHttp({
                                     schema,
                                     graphiql  : true, // use graphiql interface
                                 }));

server.listen(port, () => {
    console.log(`Listening on http`)
})
时间: 2024-11-17 21:52:15

[GraphQL] Write a GraphQL Schema in JavaScript的相关文章

[GraphQL] Create a GraphQL Schema

we’ll take a look at the GraphQL Language and write out our first GraphQL Schema. We’ll use the graphql package available to us through npm to parse our graphql language file and resolve our initial query. const { graphql, buildSchema } = require('gr

[GraphQL] Deploy a GraphQL dev playground with graphql-up

In this lesson we'll use a simple GraphQL IDL schema to deploy and explore a fully functional GraphQL service in minutes with graphql-up. Install: npm i -g graphql-up -g Create schema: type Person { id: ID!, name: String!, tasks: [Task!]! @relation(n

[GraphQL] Query a GraphQL API with graphql-request

To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation in the body of the request. In this lesson, we will use the browser’s fetch method to request the total days skied from our GraphQL API. const query

GraphQL入门指南

Introduction GraphQL is Facebook's new query language for fetching application data in a uniform way. GraphQL并不是一个面向图数据库的查询语言,而是一个数据抽象层,包括数据格式.数据关联.查询方式定义与实现等等一揽子的东西.GraphQL也并不是一个具体的后端编程框架,如果将REST看做适合于简单逻辑的查询标准,那么GraphQL可以做一个独立的抽象层,通过对于多个REST风格的简单的接口

Why GraphQL is Taking Over APIs

A few years ago, I managed a team at DocuSign that was tasked with re-writing the main DocuSign web app which was used by tens of millions of users. The APIs didn’t exist yet to support our new shiny front-end app because since the beginning the web

UWP GraphQL数据查询的实现

1. 缘起 Facebook 的移动应用从 2012 年就开始使用 GraphQL.GraphQL 规范于 2015 年开源,现已经在多种环境下可用,并被各种体量的团队所使用. 在这个链接可以看到更多的GraphQL使用者. 2. GraphQL是什么 英文官网:GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. 中文官网:GraphQL

[GraphQL] Use GraphQL's Object Type for Basic Types

We can create the most basic components of our GraphQL Schema using GraphQL's Object Types. These types allow us to group related fields together under a specific type, such as a Video or a User, and then allows us to fetch these types when we query

GraphQL介绍&使用nestjs构建GraphQL查询服务

GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接看查询语句就能知道查询出来的数据是什么样的.本质上属于API Layer层,负责前端请求的合并.数据整理等功能. 查询示例 使用几个简单的例子看下GraphQL的查询是什么样子的. 普通查询 { me { name } } 查询出来的数据格式如下: { "me": { "name

GraphQL新手上路

GraphQL是什么? GraphQL 既是一种用于API的查询语言也是一个满足你数据查询的运行时(来自:官方解释) 理解起来就是,GraphQL有自己查询语法,发起的API请求中通过传递查询语句来告诉服务端需要哪些操作和具体数据字段,GraphQL定义了实现规范,各种的语言分别实现了GraphQL功能框架,通过框架可以对查询语法进行解释执行,然后返回数据输出给客户端 GraphQL的优势 以下所有查询和输出都是来自我的DEMO,DEMO的实现和源码Github地址下面会提到 语法特性满足各种需