ERP
ERP

Reputation: 333

GraphQL how to create a schema from file separated mutations and queries

Say I have a structure like this:

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

How can I create a Schema from user's mutation.js and query.js, and cars' mutation.js and query.js combined inside schema.js?

Upvotes: 0

Views: 200

Answers (1)

ERP
ERP

Reputation: 333

@Hangindev is right, to concatenate all mutations and queries I need to export GraphQLObjectType fields, like so:

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

and adding them later into the Schema:

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
})

Upvotes: 1

Related Questions