# TypeScript: errors: GraphQLError\[\] in MockedResponse

**URL:** <https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346>\
**Category:** Client SDKs\
**Tags:** client, web\
**Created:** [June 9, 2023, 4:27am UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346 "2023-06-09T04:27:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zargold](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/zargold/32/4339_2.png) [@zargold](https://community.apollographql.com/u/zargold)\
**Post date:** [June 9, 2023, 4:27am UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/1 "2023-06-09T04:27:48Z")

</div>

According to the docs: [here in graphql-errors](https://www.apollographql.com/docs/react/development-testing/testing/#graphql-errors)

The correct way to test a pretend error is:

```auto
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`.

```auto
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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![jerelmiller](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jerelmiller/32/3314_2.png) [@jerelmiller](https://community.apollographql.com/u/jerelmiller)\
**Post date:** [June 9, 2023, 4:29pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/2 "2023-06-09T16:29:42Z")

</div>

Hey @zargold 👋

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):

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

```

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

```

What version of `graphql` are you using?

---

<div class="post-metadata">

**Author:** ![zargold](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/zargold/32/4339_2.png) [@zargold](https://community.apollographql.com/u/zargold)\
**Post date:** [June 9, 2023, 4:52pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/3 "2023-06-09T16:52:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jerelmiller](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jerelmiller/32/3314_2.png) [@jerelmiller](https://community.apollographql.com/u/jerelmiller)\
**Post date:** [June 9, 2023, 4:58pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/4 "2023-06-09T16:58:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zargold](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/zargold/32/4339_2.png) [@zargold](https://community.apollographql.com/u/zargold)\
**Post date:** [June 9, 2023, 7:22pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/5 "2023-06-09T19:22:08Z")

</div>

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](https://github.com/apollographql/apollo-tooling/issues/2665)

```auto
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.

---

<div class="post-metadata">

**Author:** ![jerelmiller](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jerelmiller/32/3314_2.png) [@jerelmiller](https://community.apollographql.com/u/jerelmiller)\
**Post date:** [June 12, 2023, 7:37pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/6 "2023-06-12T19:37:09Z")

</div>

@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](https://github.com/apollographql/apollo-tooling#readme) 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](https://the-guild.dev/graphql/codegen).

---

<div class="post-metadata">

**Author:** ![zargold](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/zargold/32/4339_2.png) [@zargold](https://community.apollographql.com/u/zargold)\
**Post date:** [June 13, 2023, 5:07pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/7 "2023-06-13T17:07:55Z")

</div>

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.

```auto
{
"__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.

---

<div class="post-metadata">

**Author:** ![jerelmiller](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jerelmiller/32/3314_2.png) [@jerelmiller](https://community.apollographql.com/u/jerelmiller)\
**Post date:** [June 13, 2023, 10:36pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/8 "2023-06-13T22:36:25Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:48pm UTC](https://community.apollographql.com/t/typescript-errors-graphqlerror-in-mockedresponse/6346/9 "2025-02-06T20:48:44Z")

</div>


