TypeScript: errors: GraphQLError[] in MockedResponse

According to the docs: here in graphql-errors

The correct way to test a pretend error is:

const dogMock = {
  // ...
  result: {
    errors: [new GraphQLError("Error!")],
  },
};

Indeed the following used to work when I used a previous version of apollo client: (@apollo/client@3.3.15). However as of the latest version 3.7.15 the following results in a typescript error. MockedResponse does not expect ‘errors’ to be populated with anything but undefined.

export function getCheckItemAvailabilityMock(zipCode = '91234'): MockedResponse {
    const variables: CheckItemAvailabilityVariables = {
        input: { zipCode },
    };

    return {
        request: {
            query: checkItemAvailability,
            variables,
        },
        result: {
            errors: [new GraphQLError('unauthorized')],
        },
    };
}

Type ‘GraphQLError’ is not assignable to type ‘undefined’.

For me to pass with the example/documentation code I’d need to type the array of GraphQLErrors as undefined.

const dogMock = {
  // ...
  result: {
    errors: [new GraphQLError("Error!")] as unknown as undefined,
  },
};

Is the documentation outdated or is there some other way in which I can test the errors behavior?

Hey @zargold :wave:

Thats really strange you’re seeing that error. I just tried the following 2 code snippets in our codebase with success (TypeScript works just fine):

const testmock: MockedResponse = {
  request: { query: gql`query { __typename }` },
  result: {
    errors: [new GraphQLError('test')],
  },
};
function getMockError(): MockedResponse {
  return {
    request: { query: gql`query { __typename }` },
    result: {
      errors: [new GraphQLError('test')],
    },
  };
}

What version of graphql are you using?

─ mypackage:
│ └─ graphql@npm:14.5.8 (via npm:14.5.8)

Would you mind trying graphql > v16 and see if that makes a difference?

shouldn’t this be in peerDependencies?

I’m not sure I’m able to upgrade with my setup:

when I upgraded graphql to 16.6.0.
I got:
node_modules/apollo-language-server/node_modules/apollo-graphql/lib/schema/buildSchemaFromSDL.js:1 Error: Cannot find module 'graphql/validation/rules/PossibleTypeExtensions'

It seems like I’m having that problem with latest version of:

  • apollo-graphql (0.9.7)
  • apollo-language-server
  • @apollographql/apollo-tools (lists highest version of graphql in peer dependencies to be ^15.0.0).

If I upgrade everything I get: 2.34.0: Cannot find module 'graphql/validation/rules/UniqueTypeNames' when generating code from a graph introspection · Issue #2665 · apollographql/apollo-tooling · GitHub

Error: Cannot find module 'graphql/validation/rules/UniqueTypeNames'
    Require stack:
    - /me/monorepo/web/node_modules/apollo-language-server/node_modules/@apollo/federation/dist/composition/rules.js
    - /me/monorepo/web/node_modules/apollo-language-server/node_modules/@apollo/federation/dist/composition/compose.js
    - /me/monorepo/web/node_modules/apollo-language-server/node_modules/@apollo/federation/dist/composition/index.js
    - /me/monorepo/web/node_modules/apollo-language-server/node_modules/@apollo/federation/dist/index.js
    - /me/monorepo/web/node_modules/apollo-language-server/lib/providers/schema/file.js
    - /me/monorepo/web/node_modules/apollo-language-server/lib/providers/schema/index.js
    - /me/monorepo/web/node_modules/apollo-language-server/lib/project/base.js
    - /me/monorepo/web/node_modules/apollo-language-server/lib/index.js
    - /me/monorepo/web/node_modules/apollo/lib/commands/client/codegen.js
    - /me/monorepo/web/node_modules/@oclif/config/lib/plugin.js
    - /me/monorepo/web/node_modules/@oclif/config/lib/config.js
    - /me/monorepo/web/node_modules/@oclif/config/lib/index.js
    - /me/monorepo/web/node_modules/@oclif/command/lib/command.js
    - /me/monorepo/web/node_modules/@oclif/command/lib/index.js
    - /me/monorepo/web/node_modules/apollo/bin/run

So yeah I definitely can’t upgrade graphql to 16.6.0 but maybe I can try some other version… it seems though that only maybe ^14.0.0 ^15.0.0 should be compatible per peer-dependencies in the chain that can generate my graphql types.

@zargold is there any way you are able to disable use of apollo-tooling to try graphql 16 temporarily? Really I just want to narrow down if the graphql version is the culprit of the issues you’re seeing.

FYI apollo-tooling was deprecated at various stages in 2022/2023 so I’d highly recommend moving off that tool if you can. If you’re using it for codegen purposes, we’d recommend looking switching to GraphQL codegen.

Thank you for continuing to look into this with me.

It doesn’t appear that the GraphQL codegen library you suggested I use will support generating types based on an introspection request. It appears to require the original .graphql schema files and use AST to generate types from that. (I’m not 100% sure.) The schema output of the introspection request comes out as JSON.

Using graphql’s built-in getIntrospectionQuery I could make a request to my graphql API and get back a schema like this… This was convenient because I could point to different environments where different versions of the schema were deployed with ease.

{
"__schema": {
        "queryType": { "name": "Query" },
        "mutationType": { "name": "Mutation" },
        "subscriptionType": null,
        "types": [
            {
                "kind": "INPUT_OBJECT",
                "name": "getCategoriesInput",
                "description": null,
                "fields": [],
                "inputFields": [
                    {
                        "name": "userPk",
                        "description": null,
                        "type": {
                            "kind": "NON_NULL",
                            "name": null,
                            "ofType": { "kind": "SCALAR", "name": "ID", "ofType": null }
                        },
                        "defaultValue": null
                    },
                    {
                        "name": "categories",
                        "description": null,
                        "type": {
                            "kind": "NON_NULL",
                            "name": null,
                            "ofType": {
                                "kind": "LIST",
                                "name": null,
                                "ofType": {
                                    "kind": "NON_NULL",
                                    "name": null,
                                    "ofType": { "kind": "SCALAR", "name": "ID", "ofType": null }
                                }
                            }
                        },
                        "defaultValue": null
                    }
                ],
                "interfaces": [],
                "enumValues": [],
                "possibleTypes": []
            },
"...": "..."
}
}

I currently only generate my types from the results of the introspection query. I think it will take a good amount of effort for me to access the .graphql schema files. If this is the only way then I guess I have no choice. Who knew 3.3.5 => 3.7.15 upgrade would be this much of a breaking change.

@zargold my apologies, I think we got a little off track. I was only mentioning the apollo tooling deprecation in case you weren’t aware of it. That tool shouldn’t have to do with the 3.3 => 3.7 upgrade as they are separate projects. Feel free to keep using that tool for codegen as long as you like, just know it won’t get any bug fixes or new features.

Back to the original issue, you mentioned you weren’t able to install graphql v16 because the apollo tooling package has a peer dependency on it that requires it to be <= v15. I’m only asking if you’re able to find a way to install graphql v16 and see if you’re still running into the TypeScript issue you mentioned in the original post. I want to rule out the graphql version being the culprit before trying to figure out what else might be causing this, especially since I haven’t been able to reproduce this issue in the apollo client repo myself.