How to use mergeSchemas for remote schemas with Apollo Server

732 Views Asked by At

I have some problems with merging remote schemas. As a base I've taken the Apollo GraphQL package referenced in the tutorial (https://www.apollographql.com/docs/tutorial/introduction/). I want to prototype the following architecture:

  • 2 services
  • 1 gateway that delegates to requests to these services
  • I don't want to use solutions as Apollo Federation
  • I want to use the separation of concerns pattern

Here are the base type def of the two services:

Company schema:

type Query {
  company: Company
}

type Company {
  id: ID!
  name: String
}

i18n schema:

type Query {
  i18n: I18N
}

type I18N {
  id: ID!
  username: String
}

type Company {
  id: ID!
  i18n(id: ID): I18N
}

The services implement the corresponding resolvers. The gateway generates remote schemes for these services and tries to merge them. And this is the point that causes some problems

const schema = mergeSchemas({
    schemas: [executableI18NSchema, executableCompanySchema],
});

The first issue is that it seems that the order of schemas in the array matters. If executableCompanySchema is the last element I can query company data properly. As soon as I move it to the first position I get messages like "Cannot query field name on type Company because mergeSchemas only takes the typeDefs from the last array element and overrides the once from the previous one. I would expect that a merge function merges the given schemas properly.

So I've done some research and found out that a stitchSchemas function exists in more recent graphql-tools version. So I've update them to the latest version and right now stitchSchemas but also mergeSchemas return the expected typeDefs etc. for the merged schema. Looks like I've made some progress, but as soon as I try to make a query request in Playground I get an error: maximum call stack size exceeded. Thus the Apollo server seems to be incompatible with the latest graphql-tools version.

Any thoughts or ideas?

0

There are 0 best solutions below