[GraphQL] Use GraphQLNonNull for Required Fields
2016-12-31 12:24
344 查看
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.
If gave the query:
Then will get the result as:
Then if get id args will get result as normal:
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 } }
相关文章推荐
- [GraphQL] Use GraphQLList with GraphQLObject Types
- [GraphQL] Use Arguments in a GraphQL Query
- [GraphQL] Write a GraphQL Mutation
- MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields
- Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios
- PB协议报错 it is missing required fields: (cannot determine missing fields for lite message)
- ora-01552 :cannot use system rollback segment for non-system tablespace 解决
- Name for parameter binding must not be null or empty! On JDKs < 8, you need to use @Param for named
- ORA-01552 cannot use system rollback segment for non-system tablespace
- 【Graphql实践】使用 Apollo(iOS) 访问 Github 的 Graphql API
- [GraphQL] Serve a GraphQL Schema as Middleware in Express
- ORA-01552: cannot use system rollback segment for non-system tablespace 'USERS'
- [GraphQL] Create a GraphQL Schema
- storm运行异常之No output fields defined for component:stream XxxBolt:null
- [GraphQL] Add an Interface to a GraphQL Schema
- 当使用phpize的时候出现Can't find PHP headers in /usr/include/php The php-devel package is required for use o
- How to perform a SQL query for fields that are null (or not null)
- [GraphQL] Deploy a GraphQL dev playground with graphql-up
- “Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targe
- your MySQL server version for the right syntax to use near 'NULL' at line 1