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?

ERP
  • 325
  • 2
  • 11
  • 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 }` – hangindev.com Jun 14 '20 at 04: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 – ERP Jun 14 '20 at 04:24

1 Answers1

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
})
ERP
  • 325
  • 2
  • 11