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:
- 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,
}
}
- Make a request to the stitched gateway with the following query:
{
entities{
relationIds
}
}
- 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'
}
]
- 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 } }'
}
}
- 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
- stab at #2735 — committed to ardatan/graphql-tools by yaacovCR 3 years ago
- fix(stitching-directives): to allow keys to include lists (#2811) * remove unnecessary type * rename things key was being overused and sometimes used incorrectly * filter original result ... — committed to ardatan/graphql-tools by yaacovCR 3 years ago
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.