ardatan / graphql-tools

:wrench: Utility library for GraphQL to build, stitch and mock GraphQL schemas in the SDL-first approach
https://www.graphql-tools.com
MIT License
5.35k stars 818 forks source link

selectionSet is ignored when overrides existing fields #6637

Open sixmen opened 3 weeks ago

sixmen commented 3 weeks ago

Issue workflow progress

Progress of the issue based on the Contributor Workflow


Describe the bug

When I use stitch & delegate, some fields described in selectionSet does not pass to the source schema.

To Reproduce Steps to reproduce the behavior:

https://codesandbox.io/p/devbox/t2xglt

import { buildSchema, graphql } from 'graphql';
import { addResolversToSchema } from '@graphql-tools/schema';
import { stitchSchemas } from '@graphql-tools/stitch';
import { delegateToSchema } from '@graphql-tools/delegate';

const sub_schema = addResolversToSchema({
  schema: buildSchema(`
  type Query {
    current_user: User
  }

  type User {
    id: ID!
    name: String!
    age: Int!
  }
`),
  resolvers: {
    Query: {
      current_user: () => ({ id: '5', name: 'John Doe', age: 10 }),
    },
  },
});

const stitched_schema = stitchSchemas({
  subschemas: [
    {
      schema: sub_schema,
      createProxyingResolver: (options) => {
        return (_parent, _args, context, info) => {
          const operationName = info.operation.name ? info.operation.name.value : undefined;
          return delegateToSchema({
            schema: options.subschemaConfig,
            operation: options.operation,
            context,
            info,
            operationName,
          });
        };
      },
    },
  ],
  resolvers: {
    User: {
      name: {
        selectionSet: '{ age }',
        resolve: (parent) => `${parent.name}(${parent.age})`,
      },
    },
  },
});

console.log((await graphql({ schema: stitched_schema, source: '{ current_user { name } }' })).data.current_user.name);

Expected behavior

Environment:

Additional context

It works well with @graphql-tools/delegate: 10.0.9

I think https://github.com/ardatan/graphql-tools/pull/6134 makes this problem. If I change if (!skipAddingDependencyNodes) to if (true) (ignore changes of the PR), I can get the correct answer.

If I add an another field instead of overriding existing fields (ie. name -> name_age), it works well.