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 || ; 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": ,
"column":
}
]
}
]
}

Then if get id args will get result as normal:

{
video (id:"a"){
id
title
duration
watched
}
}

[GraphQL] Use GraphQLNonNull for Required Fields的更多相关文章

  1. hadoop报错 Message missing required fields: callId, status

    今天群里有人问hadoop的问题,说百度上怎么都查不到,还好hadoop之前玩过一阵,也遇上过这个问题 hadoop-2.2.0  hbase 0.95-hadoop2的 ,hdfs正常 ,启动 hb ...

  2. Can't parse message of type "gazebo.msgs.Packet" because it is missing required fields: stamp, type

    在gazebo的仿真环境中,采用强化学习HER算法训练baxter执行reach.slide和pick and place任务. 运行HER算法,此时尚未启动gazebo仿真环境,出现如下报错: [l ...

  3. [转] Node.js 服务端实践之 GraphQL 初探

    https://medium.com/the-graphqlhub/your-first-graphql-server-3c766ab4f0a2#.n88wyan4e 0.问题来了 DT 时代,各种业 ...

  4. django-form and fields validation

    参考资料 清除数据与表单验证 清除数据时会进行表单验证. 在表格处理时有三种clean方法可调用,通常是在对表单调用is_valid()时执行. clean响应:一般有两种结果,如果处理的数据有问题, ...

  5. salesforce 零基础学习(五十四)常见异常友好消息提示

    异常或者error code汇总:https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_con ...

  6. Django--自定义用户认证

    Django自带的用户认证 以前都是用Django自带的用户认证,用户名字段一对一关系对应Django--User表(其实它也是继承了abstractbaseuser). 1 2 3 from dja ...

  7. thrift:swift项目笔记

    先声明:此swift不是Apple公司的那个swift开发语言,而是facebook的另一个开源项目. facebook的thrift IDL文件,如果默认用thrift -gen java生成jav ...

  8. 【笔记】jstree插件的基本使用

    官网地址:https://www.jstree.com/ json返回参数格式:推荐第二种方式 不需要在重新拼接返回格式 不刷新页面重新初始化 jstree时使用:$.jstree.destroy() ...

  9. protobuf中文教程(第一篇)

    声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...

随机推荐

  1. testng.xml顺序执行多个case配置

    testng.xml顺序执行多个case配置 项目结构如图:

  2. Week1 Java 基础知识

    JDK=JRE+Tools   JRE=JVM+API 封装:1.模块化:将属性和行为封装在类中,程序定义很多类:2.信息隐蔽:将类的细节部分隐藏起来,用户只通过受保护的接口访问某个类. 继承:父类和 ...

  3. Caffe学习系列(20):用训练好的caffemodel来进行分类

    caffe程序自带有一张小猫图片,存放路径为caffe根目录下的 examples/images/cat.jpg, 如果我们想用一个训练好的caffemodel来对这张图片进行分类,那该怎么办呢? 如 ...

  4. CentOS7 安装 mongodb

    https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-red-hat/

  5. Docker Hub仓库注册,使用

    首先保证可以访问Docker Hub,所以需要先把host替换一下 : Google hosts ; 然后就是注册Docker Hub账户:https://hub.docker.com/; 然后就是在 ...

  6. C#设计模式(11)——外观模式(Facade Pattern)

    一.引言 在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化,然而为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 ...

  7. JS学习笔记12_优化

    一.可维护性优化 1.添加注释 注释能够增强代码的可读性以及可维护性,当然,理想情况是满满的注释,但这不太现实.所以我们只需要在一些关键的地方添上注释: 函数和方法:尤其是返回值,因为直接看不出来 大 ...

  8. 帅呆了!ASP.NET Core每秒能处理115万个请求

    今天看到一篇英文博文 -- ASP.NET Core – 2300% More Requests Served Per Second,被震撼了!ASP.NET Core每秒能处理115万个请求(是的, ...

  9. .net 获取当前周及根据年和周获取起始结束时间

    获取当前日期周数 int str = new System.Globalization.GregorianCalendar().GetWeekOfYear(System.DateTime.Now, S ...

  10. easyui combobox 中实现 checkbox

    $('#cc').combobox({ url:'combobox_data1.json', method:'get', valueField:'id', textField:'text', pane ...