Skip to content

Using @graphql-tools/mock with buildSubgraphSchema breaks _Entity #1772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
michael-watson opened this issue Apr 25, 2022 · 1 comment
Closed
Milestone

Comments

@michael-watson
Copy link
Contributor

Bug Description

buildSubgraphSchema does not provide a __resolveType resolver for _Entity. @graphql-tools/mock provides a function addMocksToSchema that copies an existing schema into a new GraphQLSchema with mocked resolvers. There is a preserveResolvers config option that is meant to keep any existing resolvers, but the preserved elements may still be copied into a new schema object; specifically, unions that don't have resolveType defined.

Since buildSubgraphSchema does not provide a __resolveType for Entity, a new GraphQLUnionType is used based on the toConfig() results and the resolveType - this is just standard graphql-js and the _Entity union should copy over. You can view the relevant code in @graphql-tools/mock here.

Related Issues and PRs
I believe this is also the root of #411 and #348. Another PR addresses the other issue of __resolveReference resolvers being dropped in a similar process with toConfig; that PR is needed for mocks to work and call the appropriate entities __resolveRefernce

Reproduction

Codesandbox example, example query:

query($representations: [_Any!]!) {
  _entities(representations: $representations) {
    ... on Foo {
      name
    }
  }
}

# Variables
{
  representations: [{ __typename: "Foo", id: "1" }]
}

Will give the following error (you can run the above query in sandbox:

{
  errors: [
    [GraphQLError: Abstract type "_Entity" must resolve to an Object type at runtime forfield "Query._entities". Either the "_Entity" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.] {
      locations: [Array],
      path: [Array],
      extensions: [Object]
    }
  ]
}

Workaround

You can provide the _Entity.__resolveType resolver to addMocksToSchema as shown below and in this codesandbox reproduction:

const schema = addMocksToSchema({
  schema: buildSubgraphSchema({ typeDefs }),
  preserveResolvers: true,
  mocks: {},
  resolvers: {
    _Entity: {
      __resolveType(parent) {
        return parent.__typename;
      },
    },
  },
});

The error will be gone, unfortunately you won't get any data back because of the issue around __resolveRefernce, but this is fixed in this PR and should release in the future 🎉

(I couldn't figure out how to install the tarbill in code sandbox, but it worked locally in my identical sample to codesandbox)

@trevor-scheer
Copy link
Contributor

Fixed via #1773

@benweatherman benweatherman added this to the 2.0.2 milestone Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants