码迷,mamicode.com
首页 > 其他好文 > 详细

[GraphQL] Use GraphQLList with GraphQLObject Types

时间:2017-01-10 17:50:23      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:blog   color   ati   field   his   each   promise   out   back   

When working with collections of things in GraphQL, we‘ll always reach out for the GraphQLListType. 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

标签:blog   color   ati   field   his   each   promise   out   back   

原文地址:http://www.cnblogs.com/Answer1215/p/6269835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!