Search code examples
node.jsgraphqlreact-apolloapollo-serverexpress-graphql

Apollo Server v2 - GraphQL resolver not being invoked


I am a newbie to the graphql world and I'm trying to setup multiple modular schemas and resolvers using Apollo Server v2.

I noticed a weird behavior where I have issues with the order of my resolvers. In the line Object.assign({}, propertiesResolver, agreementsResolver) all resolvers defined by propertiesResolver don't get invoked since it is the first in the order of resolvers. If I swapped the two sets of resolvers like Object.assign({}, agreementsResolver, propertiesResolver) now resolvers defined by agreementsResolver don't get invoked.

Am I missing some important detail about graphql execution here?

Note: All my schema definitions and corresponding resolvers are correctly defined, I feel something is wrong with the order in which I'm importing things.


Solution

  • When using Object.assign:

    Properties in the target object will be overwritten by properties in the sources if they have the same key. Later sources' properties will similarly overwrite earlier ones.

    Object.assign does not do a deep merge, which is presumably what you're expecting. If two sources have the same property, only the last source's property will be used. So given two objects like:

    const a = {
      Query: {
        foo: () => 'FOO',
      },
    }
    const b = {
      Query: {
        bar: () => 'BAR',
      },
    }
    

    if you use Object.assign to merge them, the resulting object will have a single Query property that matches either a or b (depending on which was the later parameter). In order to do a deep merge that combines objects of properties with the same name, you should use an existing solution, like lodash:

    const resolvers = _.merge(a, b)
    

    or something similar.