Tjeerd
Tjeerd

Reputation: 166

Getting Cannot read property 'args' of undefined when trying to stitch schema's with apollo server

There seem to be some similar questions but none at the same point as where I get the error and none with the 'args' of undefined.

I've gotten it to work to have multiple other graphql apis and merge them. Just mergin them is going well but when I try to make a stitch I get the error shown below:

"errors": [
{
  "message": "Cannot read property 'args' of undefined",
  "locations": [
    {
      "line": 9,
      "column": 9
    }
  ],
  "path": [
    "flows",
    "edges",
    0,
    "node",
    "sourceAddresses"
  ],
  "extensions": {
    "code": "INTERNAL_SERVER_ERROR",
    "exception": {
      "stacktrace": [
        "TypeError: Cannot read property 'args' of undefined",
        "    at /opt/node_modules/graphql-tools/src/transforms/AddArgumentsAsVariables.ts:103:15",
        "    at Array.forEach (<anonymous>)",
        "    at /opt/node_modules/graphql-tools/src/transforms/AddArgumentsAsVariables.ts:95:39",
        "    at Array.map (<anonymous>)",
        "    at addVariablesToRootField (/opt/node_modules/graphql-tools/src/transforms/AddArgumentsAsVariables.ts:66:36)",
        "    at AddArgumentsAsVariablesTransform.transformRequest (/opt/node_modules/graphql-tools/src/transforms/AddArgumentsAsVariables.ts:31:11)",
        "    at /opt/node_modules/graphql-tools/src/transforms/transforms.ts:24:21",
        "    at Array.reduce (<anonymous>)",
        "    at Object.applyRequestTransforms (/opt/node_modules/graphql-tools/src/transforms/transforms.ts:21:21)",
        "    at /opt/node_modules/graphql-tools/src/stitching/delegateToSchema.ts:90:28"
      ]
    }
  }
}

]

I've got a schemaRetriever service that retrieves the schemas from 3 GraphQL Api's. For now I'm trying to get 1 stitch working so the code isn't really 'nice' at the moment.

Here is the piece of code where I try to get a stitch going:

    const aSchema = await this.schemaRetrieverService.retrieveASchema();
const housesSchema = await this.schemaRetrieverService.retrieveHousesSchema();
const addressSchema = await this.schemaRetrieverService.retrieveAddressSchema();

return mergeSchemas({
  schemas: [
    aSchema,
    housesSchema,
    addressSchema,
        `
    extend type Houses {
      houseAddresses: Addresses
    }

    extend type Addresses {
      houses: Houses
    }

    type Query {
      addressByHouseId(id: ID!): Addresses
    }
  `
  ],
  resolvers: {
    Houses: {
      houseAddresses: {
        fragment: `... on Houses { id }`,
        resolve(house, args, context, info) {
          return info.mergeInfo.delegateToSchema({
            schema: addressSchema,
            operation: 'query',
            fieldName: 'addressByHouseId',
            args: {
              id: house.houseAddress,
            },
            context,
            info,
          });
        },
      },
    },
  },
});

I've verified that 'house.houseAddress' contains the correct ID that is needed and I keep getting the same error although I'm putting a valid ID in the args variable. Am I missing something small here? or big? If someone could give me a pointer I would be so grateful.

Many thanks in advance and if any information is missing please let me know I'll try to add all necessary info.

Upvotes: 1

Views: 1432

Answers (1)

Tjeerd
Tjeerd

Reputation: 166

I've found an answer to this problem I'm having and as sharing is caring I'll answer my own question here. As I was new to this I didn't really know what happened in the background. For people who might be facing the same issue.

Because I was so focused on the stitching I forgot there are functions available in my microservices for which I retrieve the schema's. When stitching the Fieldname given to a stitch should be a function that can be called on the entity.

This means that fieldName: 'addressByHouseId', refers to a function that should exist on the Houses type.

You could either add this function on the entity on the microservice level or what I did in my case refer to the (by default generated with api-platform) houses(id) function. Now there is an actual implementation that can be used for the stitch.

Upvotes: 3

Related Questions