graphql-tools: argsFromKeys doesn't work consistently between MergedTypeConfig and @key directives when selecting list types

Describe the bug A selectionSet referencing a list type will resolve as an object type when using the @key SDL directive, but retrieves the list normally when defining the MergeTypeConfig manually.

To Reproduce Steps to reproduce the behavior:

  1. Create two subschemas:

Schema A:

type Entity {
  relations: [Relation!]!
}

type Relation {
  id: ID!
}

type Query {
  entities: [Entity!]!
}

Resolvers A:

{
  Query: {
    entities: () => [
      {
        relations: [{ id: 2 }, { id: 3 }]
      }
    ]
  }
}

Schema B:

type Entity @key(selectionSet: "{ relations { id } }") {
  relationIds: [String!]!
}

scalar Key

type Query {
  _entities(keys: [Key!]!): [Entity!]! @merge
}

Resolvers B:

{
  Entity: {
    relationIds: ({ relations }) => relations.map(({ id }) => id),
  },
  Query: {
    _entities: (_, { keys }) => keys,
  }
}
  1. Make a request to the stitched gateway with the following query:
{
  entities{
    relationIds
  }
}
  1. You should receive an error:
relations.map is not a function

Logging the keys parameter of the _entities resolver, you’ll see:

[
  {
    relations: [Object: null prototype] { id: undefined },
    __typename: 'Entity'
  }
]
  1. Remove the stitching directives and add the following MergedTypeConfig to the config for subschema B:
{
  Entity: {
    argsFromKeys: (keys) => ({ keys }),
    fieldName: '_entities',
    key: ({ relations }) => ({ relations }),
    selectionSet: '{ relations { id } }'
  }
}
  1. Resend the query in step 2. You should now get the ids correctly.
{
  "data": {
    "entities": [
      {
        "relationIds": [
          "2",
          "3"
        ]
      }
    ]
  }
}

Logging the keys parameter:

[{"relations":[{"id":"2"},{"id":"3"}]}]

Expected behavior

The behavior should match between the two scenarios.

Environment:

  • OS:
  • graphql-tools: 7.0.2
  • NodeJS: 14.15.1

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Meaning it should, but does not yet

Thanks so much for tests, looking good in latest release, please reopen as necessary 😃

Should be able to fix that pretty easily.