[GraphQL] Use Arguments in a GraphQL Query
2016-12-31 12:11
351 查看
In GraphQL, every field and nested object is able to take in arguments of varying types in order to do common operations like fetching an object by it's ID, filtering, sorting, and more. In this video, we'll update a field to take in an id argument and then learn how to use that argument in our resolve method to fetch a video by its id.
data:
In the web interface, enter 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, 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: 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`) })
data:
const videoA = { id: 'a', title: 'Create a GraphQL Schema', duration: 120, watched: true, }; const videoB = { id: 'b', title: 'Ember.js CLI', duration: 240, watched: false, }; const videos = [videoA, videoB]; const getVideoById = (id) => new Promise((resolve) => { const [video] = videos.filter((video) => { return video.id === id; }); resolve(video); }); exports.getVideoById = getVideoById;
In the web interface, enter the query:
{ video (id: "b"){ id title duration watched } }
相关文章推荐
- [GraphQL] Use GraphQLList with GraphQLObject Types
- [GraphQL] Write a GraphQL Schema in JavaScript
- [GraphQL] Use GraphQLNonNull for Required Fields
- [GraphQL] Serve a GraphQL Schema as Middleware in Express
- Fatal error: Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY' in
- spring.data.jpa.query :Cannot use native queries with dynamic sorting and/or pagination in method
- 【Graphql实践】使用 Apollo(iOS) 访问 Github 的 Graphql API
- Thinking in GraphQL #Facebook Relay文档翻译#
- How to Use Lambda Expressions in a Query
- Use command line arguments in Node.js
- Graphql介绍(Introduction to GraphQL)
- Avoid non-default constructors in fragments: use a default constructor plus setArguments()错误提示解决方法
- [GraphQL] Write a GraphQL Mutation
- Have you considered making your output() function use variable arguments like printf()? In the examp
- Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bu
- [BTS] Use TSQL to query a XML field in SQLServer
- [GraphQL] Create a GraphQL Schema
- Avoid non-default constructors in fragments:use a default constructor plus Fragment#SetArguments(Bu)
- How to Use a QSqlQueryModel in QML
- [GraphQL] Deploy a GraphQL dev playground with graphql-up