mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify

Home Page:https://mercurius.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better error messages on passing array of executable schemas.

brainrepo opened this issue · comments

In Mercurius' schema option, we can pass schemas as either a string or an executable schema.

If we pass them as a string, we can also pass an array of schemas in the string type, in which case Mercurius will merge them.

However, if we pass them as an executable schema, we can only pass one.
Currently, there is no check to prevent the passing of an array of executable schemas.
If we pass two executable schemas, we get an error.

Possible solutions:

  • improve error handling
  • use schema stitching against the executable schemas to get just one executable schema

mercurius/index.js

Lines 148 to 171 in b6ac057

let schema = opts.schema
if (Array.isArray(schema)) {
schema = schema.join('\n')
}
if (typeof schema === 'string') {
schema = buildSchema(schema)
} else if (!opts.schema) {
schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {}
}),
mutation: opts.defineMutation
? new GraphQLObjectType({
name: 'Mutation',
fields: {}
})
: undefined
})
}
fastifyGraphQl.schema = schema

Why would you need to pass an array of executable schemas?

Why would you need to pass an array of executable schemas?

It can be useful if you decide to split in modules your GraphQL application.
In my previous project (pretty big) we divided the GraphQL api in domains/modules and each module was responsible of just few types and resolvers.

understood, makes sense