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. I535卡刷土豆修改4.1.2版本ROMV4過程

    I535卡刷土豆修改版本ROMV4過程 一.首先在电脑上安装I535的电脑驱动程序 二.備份EFS,備份舊ROM(网上有教程) 三.解锁:下载EZ-Unlock解锁. 四.檢查Recovery是否為最 ...

  2. 【原创】-- Linux 下利用dnw进行USB下载

    原帖地址: http://blog.csdn.net/jjzhoujun2010 http://blog.csdn.net/yf210yf/article/details/6700391 http:/ ...

  3. Linux下paste命令

    paste 用于将多个文件按照列队列进行合并. 该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 1.原文件: 1>a.txt [root@localhost home]# ...

  4. java编程规范

    一.规范存在的意义 应用编码规范对于软件本身和软件开发人员而言尤为重要,有以下几个原因: 1.好的编码规范可以尽可能的减少一个软件的维护成本 , 并且几乎没有任何一个软件,在其整个生命周期中,均由最初 ...

  5. 手机数据抓包以及wireshark技巧

    本文主要讨论一种非常方便的抓取Android和iphone手机网络数据包的办法,以及介绍wireshark最常用的技巧 抓包工具介绍 (1).网页抓包工具 Chrome浏览器插件 FireBug 插件 ...

  6. ubuntu下使用nginx部署Laravel

    问题描述 Laravel是PHP下当今最受欢迎的web应用开发框架,github上start数远超第二名Symfony,以前我用这个框架做项目的时候通常就是扔到apache里面,然后配置.htacce ...

  7. Apache axis2 + Eclipse 开发 WebService

    yd小结注意:1.axis2的2个插件的版本必须与引入的jar包匹配,如果不同则可能报以下错误 “没有实现序列化方法”或 “org.apache.axis2.databinding.utils.wri ...

  8. 编写高质量代码改善C#程序的157个建议读书笔记【11-20】

    章节索引 建议11:区别对待 == 和Equals 建议12:重写Equals也要重写GetHashCode 建议13:为类型输出格式化字符串 建议14:正确实现浅拷贝和深拷贝 建议15:使用dyna ...

  9. 菜鸟日记-HTML

    第一部分  HTML---Hyper Text Markup Language--超文本标记语言 1.HTML标准:<html> <head> 网页上的控制信息 <tit ...

  10. fir.im Weekly - 工欲善其事,必先利其器

    这周为大家搜罗了一些优秀的 GitHub 资源.APP开发工具.产品UI设计资源等等,同时也有一些程序员个人成长的Tips,"软硬件兼备"也许事半功倍不只是梦! Open-Sour ...