[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 || ;
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的更多相关文章
- hadoop报错 Message missing required fields: callId, status
今天群里有人问hadoop的问题,说百度上怎么都查不到,还好hadoop之前玩过一阵,也遇上过这个问题 hadoop-2.2.0 hbase 0.95-hadoop2的 ,hdfs正常 ,启动 hb ...
- 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 ...
- [转] Node.js 服务端实践之 GraphQL 初探
https://medium.com/the-graphqlhub/your-first-graphql-server-3c766ab4f0a2#.n88wyan4e 0.问题来了 DT 时代,各种业 ...
- django-form and fields validation
参考资料 清除数据与表单验证 清除数据时会进行表单验证. 在表格处理时有三种clean方法可调用,通常是在对表单调用is_valid()时执行. clean响应:一般有两种结果,如果处理的数据有问题, ...
- salesforce 零基础学习(五十四)常见异常友好消息提示
异常或者error code汇总:https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_con ...
- Django--自定义用户认证
Django自带的用户认证 以前都是用Django自带的用户认证,用户名字段一对一关系对应Django--User表(其实它也是继承了abstractbaseuser). 1 2 3 from dja ...
- thrift:swift项目笔记
先声明:此swift不是Apple公司的那个swift开发语言,而是facebook的另一个开源项目. facebook的thrift IDL文件,如果默认用thrift -gen java生成jav ...
- 【笔记】jstree插件的基本使用
官网地址:https://www.jstree.com/ json返回参数格式:推荐第二种方式 不需要在重新拼接返回格式 不刷新页面重新初始化 jstree时使用:$.jstree.destroy() ...
- protobuf中文教程(第一篇)
声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...
随机推荐
- bzoj 3389
题意:给定1维连续T<= 1000000个点,以及n<=10000个线段,求最少的线段覆盖该区间.. 思路:很显然,贪心是可以做的..不过这一题最有意思的是使可以转换为最短路模型.. 如果 ...
- 敏捷开发 与 Scrum
敏捷开发以用户的需求进化为核心,采用迭代.循序渐进的方法进行软件开发.在敏捷开发中,软件项目在构建初期被切分成多个子项目,各个子项目的成果都经过测试,具备可视.可集成和可运行使用的特征.换言之,就是把 ...
- Ubuntu配置git
安装ssh sudo apt-get install openssh-server sudo apt-get install openssh-client 启动SSH服务 sudo /etc/init ...
- mvc项目问题清单以及解决方法
项目开发中遇到的一些问题以及解决方法. 1. 脚本相关 mvc中RemoteAttribute使用,在IE浏览器下面会将结果缓存起来(304).因为IE浏览器判断Url的链接参数都没有变化,所以直接返 ...
- 标签简化Spring-MVC配置
新填入@RequestMapping标签 和@org.springframework.stereotype.Controller标签 这样做就是通过标签来简化之前,对HandlerMapping的配置 ...
- 【转】关于Mahalanobis距离的笔记
Mahalanobis距离是用来度量一个点P和一个分布D之间的距离,它是衡量点P与分布D的均值之间存在多少个标准差的一个多维泛化版本. 如果P就位于分布D的均值处,则该距离为0:该距离随着P的偏离均值 ...
- atitit. hb 原生sql跨数据库解决原理 获得hb 数据库类型运行期获得Dialect
atitit. hb 原生sql跨数据库解决原理 获得hb 数据库类型运行期获得Dialect #-----原理 Hibernate 运行期获得Dialect 2010-07-28 12:59 ...
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- unity 读取excel表 生成asset资源文件
做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...
- jsp实现验证码
在web开发领域里面,验证码是一个比较常见的功能,而归根到底,验证码其实就是一组随机数,或者是一个随机算术 一.基本知识 1.为什么需要验证码? 验证码,很多时候出现在注册页面或者登陆界面,在这些页面 ...