• [GraphQL] Use GraphQLNonNull for Required Fields


    While certain fields in a GraphQL Schema can be optional, there are some fields or arguments that are necessary in order to either fulfill a query, or to provide a guarantee to people using the Schema that some field exists. In this video, we'll take a look at turning an argument in a NonNull argument by applying the GraphQLNonNull type in order to guarantee that the given argument is supplied in the query.

    const express   = require('express');
    const graphqlHttp = require('express-graphql');
    const { getVideoById } = require('./data/index');
    const server = express();
    const port   = process.env.PORT || 3000;
    
    const {
        GraphQLSchema,
        GraphQLObjectType,
        GraphQLString,
        GraphQLInt,
        GraphQLNonNull,
        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,
                args: {
                    id: {
                        type : new GraphQLNonNull(GraphQLID),
                        description: 'The id of the video'
                    }
                },
                resolve: (_, args) => getVideoById(args.id)
            }
        }
    });
    
    const schema = new GraphQLSchema({
        query: queryType
    });
    
    server.use('/graphql', graphqlHttp({
                                         schema,
                                         graphiql  : true, // use graphiql interface
                                     }));
    
    server.listen(port, () => {
        console.log(`Listening on http`)
    })

    If gave the query:

    {
      video{
        id
        title
        duration
        watched
      }
    }

    Then will get the result as:

    {
      "errors": [
        {
          "message": "Field "video" argument "id" of type "ID!" is required but not provided.",
          "locations": [
            {
              "line": 2,
              "column": 3
            }
          ]
        }
      ]
    }

    Then if get id args will get result as normal:

    {
      video (id:"a"){
        id
        title
        duration
        watched
      }
    }
  • 相关阅读:
    Tool工具页面代码
    Tool工具生成代码数据库Model生成代码
    类别切换 分页
    ASP.NET AJAX无刷新验证用户名
    VSS的配置和使用
    js 常用方法大全
    灵异——1995年北京330路公交车失踪案
    C#用HttpWebRequest通过代理服务器验证后抓取网页内容 。。。。。
    win2003远程 客户端无法连接到远程计算机。
    .net中点击button按钮显示下一条记录(上一条 下一条)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6239032.html
Copyright © 2020-2023  润新知