Skip to content
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

getResolversFromSchema fails to extract Federation __resolveReference resolvers #4768

Closed
4 tasks
Tracked by #5201 ...
darrellwarde opened this issue Oct 20, 2022 · 3 comments
Closed
4 tasks
Tracked by #5201 ...

Comments

@darrellwarde
Copy link

Issue workflow progress

Progress of the issue based on the Contributor Workflow

  • 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox

    Make sure to fork this template and run yarn generate in the terminal.

    Please make sure the GraphQL Tools package versions under package.json matches yours.

  • 2. A failing test has been provided
  • 3. A local solution has been provided
  • 4. A pull request is pending review

Describe the bug

getResolversFromSchema seems to fail to extract __resolveReference resolvers from a subgraph schema.

To Reproduce
Steps to reproduce the behavior:

Run the following code:

const { buildSubgraphSchema } = require("@apollo/subgraph");
const { getResolversFromSchema } = require("@graphql-tools/utils");
const { gql } = require("graphql-tag");

const typeDefs = gql`
  type User @key(fields: "id") {
    id: ID!
  }

  type Query {
    user: User!
  }
`;

const resolvers = {
  User: {
    __resolveReference: () => ({ id: 1 })
  },
  Query: {
    user: () => ({ id: 1 })
  }
};

const schema = buildSubgraphSchema({
  typeDefs,
  resolvers
});

const extractedResolvers = getResolversFromSchema(schema);

console.log(extractedResolvers);

The output is as follows:

[Object: null prototype] {
  User: {},
  Query: {
    user: { resolve: [Function: user] },
    _entities: { resolve: [Function: _entities] },
    _service: { resolve: [Function: _service] }
  },
  _FieldSet: GraphQLScalarType {
    name: '_FieldSet',
    description: undefined,
    specifiedByURL: undefined,
    serialize: [Function: identityFunc],
    parseValue: [Function: identityFunc],
    parseLiteral: [Function (anonymous)],
    extensions: [Object: null prototype] {},
    astNode: undefined,
    extensionASTNodes: []
  },
  _Any: GraphQLScalarType {
    name: '_Any',
    description: undefined,
    specifiedByURL: undefined,
    serialize: [Function: identityFunc],
    parseValue: [Function: identityFunc],
    parseLiteral: [Function (anonymous)],
    extensions: [Object: null prototype] {},
    astNode: undefined,
    extensionASTNodes: []
  },
  _Service: {},
  _Entity: { __resolveType: [Function: __resolveType] }
}

You can see that the User type has lost its __resolveReference resolver.

Expected behavior

I would expect to see the __resolveReference resolvers which I defined within the output.

Environment:

  • OS: macOS 12.5.1
  • @graphql-tools/utils: 8.12.0
  • NodeJS: 16.13.0
@ardatan
Copy link
Owner

ardatan commented Oct 20, 2022

This is not a bug in GraphQL Tools actually but a feature request for getResoiversFromSchema to support Apollo's specific extensions in the resolvers map.
Also see #2687 (comment)

@darrellwarde
Copy link
Author

Interesting, thanks for the prompt response!

It sounds a bit like lunacy, but I'm looking at executing buildSubgraphSchema, extracting the relevant parts, performing some other actions on them, and finally passing all of that into makeExecutableSchema - do you know if there's a combination of existing functions that can be used to extract everything I need, so that the output of makeExecutableSchema is the same as the output as buildSubgraphSchema? It seems that the __resolveReference resolvers are passed into the extensions of the GraphQL schema - is it possible for extensions to be extracted and passed into makeExecutableSchema right now?

@ardatan
Copy link
Owner

ardatan commented Oct 20, 2022

It seems Apollo is putting resolveReference under extensions. Maybe you can use that;
https://github.com/apollographql/federation/blob/main/subgraph-js/src/schema-helper/buildSchemaFromSDL.ts#L181

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

2 participants