构建GraphQL查询和变异时,使用这两种方法有什么区别?

时间:2019-08-27 17:43:46

标签: javascript graphql graphql-js

在使用GraphQL时,我学会了两种不同的方法,

第一种方法包括构建模式:

    module.exports = buildSchema(`
        type Event {
            _id: ID!
            title: String!
            description: String!
            price: Float!
            date: String!
            creator: User!
         }
    `)

然后在单独的文件中创建解析器:

    module.exports = {
    events: async () => {
      try {
        const events = await Event.find();
        return events
      } catch (err) {
        throw err;
      }
    }}

第二种方法如下:

    const PostType = new GraphQLObjectType({
        name: 'Post',
            fields: () => ({
            id: { type: GraphQLID },
            title: { type: GraphQLString },
            content: { type: GraphQLString },
            score: { type: GraphQLInt }
            })
        });
    const RootQuery = new GraphQLObjectType({
        name: 'RootQuery',
        fields: {
            posts: {
            type: new GraphQLList(PostType),
            resolve(parent, args) {
                return Post.find();
            }   
        }
    });

我已经同时使用了这两种方法,但是它们都可以工作,但是我不确定我应该使用哪种方法,第一种方法似乎更简单,但是第二种方法似乎可以执行graphQL类型。

0 个答案:

没有答案