首页 > 代码库 > [GraphQL] Use GraphQLList with GraphQLObject Types
[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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。