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

mapSchema() wipes out federated schema resolveReference() function #2687

Closed
robross0606 opened this issue Mar 4, 2021 · 13 comments · Fixed by apollographql/federation#1746
Closed
Labels
feature New addition or enhancement to existing solutions stage/4-pull-request A pull request has been opened that aims to solve the issue

Comments

@robross0606
Copy link

robross0606 commented Mar 4, 2021

Describe the bug
When applying mapSchema() to a federated schema, any resolveReference functions on types are wiped out.

To Reproduce
The following Jest test shows that even a NooP transform using mapSchema() wipes out the federated resolveReference function.

const gql = require('graphql-tag')
const { mapSchema } = require('@graphql-tools/utils')
const { buildFederatedSchema } = require('@apollo/federation')

test('mapSchema() wipes out resolveReference function', async () => {
  const federatedEntity = gql`
    type FederatedEntity @key(fields: "id") {
      id: ID!
      properties: [String!]!
    }
  `
  const federatedEntityResolver = {
    FederatedEntity: {
      __resolveReference: async object => {
        object.properties = ['a', 'couple', 'of', 'properties']
        return object
      }
    }
  }

  let schema = buildFederatedSchema([
    {
      typeDefs: federatedEntity,
      resolvers: federatedEntityResolver
    }
  ])

  // Confirm that federated `resolveReference` function exists.
  expect(schema._typeMap.FederatedEntity.resolveReference).toBeTruthy()

  // NooP transform
  const mappedSchema = mapSchema(schema)

  // This should pass but it fails because `resolveReference` has been wiped out.
  expect(mappedSchema._typeMap.FederatedEntity.resolveReference).toBeTruthy()
})

image

Expected behavior
One would expect mapping a schema should pass thru anything not specifically mapped.

Environment:

  • OS: Tested on Windows 10 and multiple flavors of Linux
  • @graphql-tools/utils: 7.5.0
  • NodeJS: 14.15.5
@ardatan
Copy link
Owner

ardatan commented Mar 5, 2021

Thanks for the reporting this missing feature. Would you create a draft PR then we can help you?

@robross0606
Copy link
Author

Thanks for the reporting this missing feature. Would you create a draft PR then we can help you?

Unfortunately, I don't have the bandwidth or background on your code base to fix this issue. I know it is happening, but not why.

@rajagopalx
Copy link

Is there any workaround for this?

@yaacovCR
Copy link
Collaborator

The fix would be for Apollo federation to use extensions to extend the schema with metadata instead of custom properties. A workaround would involve copying the custom federation stuff like resolveReference and whatever else from the old schema to the mapped schema...

@yaacovCR
Copy link
Collaborator

This is not a bug in graphql tools. It is a feature request to handle the custom GraphQLSchema objects created by Apollo...

we could try to do that, but we would need to investigate what hidden metadata they are using. It’s not our code base that you need to be familiar with to help fix this, it’s Apollo’s...

@ardatan ardatan added the feature New addition or enhancement to existing solutions label Mar 25, 2021
@kkckkc
Copy link

kkckkc commented Apr 22, 2021

I understand that this is not an issue with graphql-tools per se, but I really need this - with the slight difference in that I'm interested in resolveObject instead of resolveReference.

I have a simple patch to fix my immediate issue, i.e. propagating these two fields as part of mapSchema and wrapSchema - but I haven't looked at the full potential scope of this issue across all functionality.

Should I submit a PR?

@yaacovCR
Copy link
Collaborator

Sure, thanks!!! We love PRs :)

@Urigo Urigo added the stage/4-pull-request A pull request has been opened that aims to solve the issue label Apr 26, 2021
@robross0606
Copy link
Author

I just found this same exact issue occurs if you use const { addResolversToSchema } = require('@graphql-tools/schema'). Calls to that function completely wipe out Aopllo Federation resolvers.

@ardatan
Copy link
Owner

ardatan commented Oct 20, 2021

@robross0606 You can use updateResolversInPlace flag to avoid recreation of the schema.

@MrDesjardins
Copy link

I am having the same issue. When I and using buildSubgraphSchema + mapSchema the resolveReference is out.

@ShawnWilsonCB
Copy link

Is there any workaround at all for this?

@a7ul
Copy link

a7ul commented Mar 2, 2022

There is a workaround here https://community.apollographql.com/t/are-object-resolvers-broken-with-custom-schema-directives/1578/4

trevor-scheer added a commit to apollographql/federation that referenced this issue Apr 19, 2022
The entity __resolveReference resolver is currently placed directly
on to GraphQL*Type objects. This breaks the API, as the expectation
is to put things like this on to the `extensions` field of objects
contained within a GraphQLSchema.

This moves the resolver off of the object and into the extensions
where it belongs.

Fixes: #1131
Fixes: ardatan/graphql-tools#2687
Fixes: ardatan/graphql-tools#2857
trevor-scheer added a commit to apollographql/federation that referenced this issue Apr 20, 2022
The entity __resolveReference resolver is currently placed directly
on to GraphQL*Type objects. This breaks the API, as the expectation
is to put things like this on to the `extensions` field of objects
contained within a GraphQLSchema.

This moves the resolver off of the object and into the extensions
where it belongs.

Fixes: #1131
Fixes: ardatan/graphql-tools#2687
Fixes: ardatan/graphql-tools#2857
trevor-scheer added a commit to apollographql/federation that referenced this issue Apr 20, 2022
The entity __resolveReference resolver is currently placed directly
on to GraphQL*Type objects. This breaks the API, as the expectation
is to put things like this on to the `extensions` field of objects
contained within a GraphQLSchema.

This moves the resolver off of the object and into the extensions
where it belongs.

Fixes: #1131
Fixes: ardatan/graphql-tools#2687
Fixes: ardatan/graphql-tools#2857
trevor-scheer added a commit to apollographql/federation that referenced this issue Apr 20, 2022
#1747)

The entity __resolveReference resolver is currently placed directly
on to GraphQL*Type objects. This breaks the API, as the expectation
is to put things like this on to the `extensions` field of objects
contained within a GraphQLSchema.

This moves the resolver off of the object and into the extensions
where it belongs.

Fixes: #1131
Fixes: ardatan/graphql-tools#2687
Fixes: ardatan/graphql-tools#2857
@trevor-scheer
Copy link
Contributor

#2857 (comment)

@ardatan I think this issue can be closed out as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition or enhancement to existing solutions stage/4-pull-request A pull request has been opened that aims to solve the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants