【问题标题】:GraphQL how to create a schema from file separated mutations and queriesGraphQL 如何从文件分隔的突变和查询中创建模式
【发布时间】:2020-10-03 16:19:48
【问题描述】:

假设我有这样的结构:

src/
├── user/
│   ├── mutation.js
│   ├── query.js
├── cars/
│   ├── mutation.js
│   ├── query.js
└── schema.js

如何从用户的 mutation.js 和 query.js 以及汽车的 mutation.js 和 query.js 在 schema.js 中组合创建架构?

【问题讨论】:

  • 很难说出您从 mutation.jsquery.js 导出的内容以及您对 schema.js 的期望,但您始终可以合并对象并创建一个新对象,例如const Query= { ...userQuery, ...carQuery }
  • 抱歉,我在mutation.js 和query.js 上都导出了GraphQLObjectType,我想创建一个包含所有突变和查询的GraphQLSchema,所以你的解决方案可以在这里工作@Hangindev

标签: node.js graphql graphql-mutation


【解决方案1】:

@Hangindev 是对的,要连接我需要导出 GraphQLObjectType 字段的所有突变和查询,如下所示:

const userMutation = {
    addUser: {
        type: UserType,
        args: {
            username: {type: GraphQLString},
            email: {type: GraphQLString},
        },
        resolve(parent, args) {
            let author = new User({
                username: args.username,
                email: args.email,
            });
            return author.save();
        }
    },
}

module.exports = userMutation

稍后将它们添加到架构中:

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        ...userMutation,
        ...foodPostMutation
    }
})

const Query = new GraphQLObjectType({
    name: 'Query',
    fields: {
        ...userQuery,
        ...foodPostQuery
    }
})

module.exports = new GraphQLSchema({
    query: Query,
    mutation: Mutation
})

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2016-05-17
    • 2021-09-09
    • 2019-07-26
    • 2017-09-23
    • 2017-08-24
    • 2021-10-14
    • 2016-09-08
    • 2019-12-21
    相关资源
    最近更新 更多