[GraphQL] Use GraphQLList with GraphQLObject Types
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GraphQL] Use GraphQLList with GraphQLObject Types相关的知识,希望对你有一定的参考价值。
When working with collections of things in GraphQL, we‘ll always reach out for the GraphQLList
Type. In this video, we‘ll learn how to use GraphQLList from the graphql
package in combination with a GraphQLObject
Type to create a field that returns a collection in our Schema.
We can use GraphQLList to fetch list objects:
const queryType = new GraphQLObjectType({ name: ‘QueryType‘, description: ‘The root query type‘, fields :{ videos: { type: new GraphQLList(videoType), resolve: getVideos }, video: { type: videoType, args: { id: { type : new GraphQLNonNull(GraphQLID), description: ‘The id of the video‘ } }, resolve: (_, args) => getVideoById(args.id) } } });
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); }); const getVideos = () => new Promise((resolve) => resolve(videos)); exports.getVideoById = getVideoById; exports.getVideos = getVideos;
以上是关于[GraphQL] Use GraphQLList with GraphQLObject Types的主要内容,如果未能解决你的问题,请参考以下文章