0

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?

2
  • 1
    Hard to tell what you are exporting from mutation.js and query.js and what you are expecting in schema.js but you can always merge objects and create a new one, e.g. const Query= { ...userQuery, ...carQuery } Commented Jun 14, 2020 at 4:11
  • Sorry, so on both mutation.js and query.js I export GraphQLObjectType, and I would like to create a GraphQLSchema with the combination of all mutations and queries and so yeah your solution could work here @Hangindev Commented Jun 14, 2020 at 4:24

1 Answer 1

1

@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
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.