Can I use "mergeResolvers" from graphql-tools in BuildSubgraphSchema

Hello,

I’m in the process of migrating our service to Apollo Federation and when calling the function “buildSubgraphSchema”, I want to pass my resolvers and merge them together as in a real world you would have them split across your code. Something like this:

buildSubgraphSchema({
      resolvers: mergeResolvers(myListOfResolvers),
})

I’m using “@graphql-tools/merge” with the mergeResolvers function but when running my test the resolvers don’t seems to be called. Can I use this function ? or is there something similar to merge multiple resolvers together ? I found the documentation provided very light regarding this topic.

Hey @Pierre_Biver, I would love to talk more about this live! We recently started up an Apollo Discord for more live interactions and if you head over there, feel free to ping me directly (@watson) and we can dig in deeper. I don’t know if mergeResolvers would work or not, but I don’t see any reason why it wouldn’t.

Can you help me understand if there is anything special you are doing with mergeResolvers? You might have a pattern with your resolvers where the Query resolver is split over multiple files, in which case I’ve just see lodash/merge get used to do this. The legacy SpaceXLand API has a good example and that could be provided to buildSubgraphSchema.

If it’s to just bring together a set of resolvers, you could consider exposing them in a single index that is exported and used in buildSubgraphSchema. I typically favor this pattern because of it’s simplicity, but it does mean having to add the resolver into the index for it to be added. (I’m assuming you’re doing something similar to create myListOfResolvers).

For example, you can co-locate all of your resolvers into a resolvers folder that has an index (example):

import { Query } from "./Query";
import { Mutation } from "./Mutation";
import { Thing } from "./Thing";

const resolvers = {
  ...Query,
  ...Mutation,
  ...Thing,
};

export default resolvers;

Then in your server code, you would just import those resolvers (example):

import resolvers from "./resolvers";
...
const schema = new ApolloServer({
  schema: buildSubgraphSchema({ resolvers }),
});