Skip to content

Commit

Permalink
Collect custom scalars from arguments and input object fields
Browse files Browse the repository at this point in the history
Closes #6239

Closes #4574
  • Loading branch information
kamilkisiela committed Jan 17, 2025
1 parent ba20748 commit 93dfbb2
Show file tree
Hide file tree
Showing 5 changed files with 673 additions and 18 deletions.
9 changes: 9 additions & 0 deletions .changeset/little-donuts-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'hive-apollo-router-plugin': patch
'@graphql-hive/core': patch
'@graphql-hive/apollo': patch
'@graphql-hive/envelop': patch
'@graphql-hive/yoga': patch
---

Collect custom scalars from arguments and input object fields
13 changes: 12 additions & 1 deletion packages/libraries/core/src/client/collect-schema-coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ export function collectSchemaCoordinates(args: {
}

function collectNode(node: ObjectFieldNode | ArgumentNode) {
const inputType = args.typeInfo.getInputType()!;
const inputType = args.typeInfo.getInputType();

if (!inputType) {
throw new Error('Expected an Input type, got nothing');
}

const inputTypeName = resolveTypeName(inputType);

if (node.value.kind === Kind.ENUM) {
Expand Down Expand Up @@ -279,6 +284,12 @@ export function collectSchemaCoordinates(args: {

const parentInputTypeName = resolveTypeName(parentInputType);

if (isScalarType(parentInputType)) {
collectInputType(parentInputTypeName);
// Prevent the visitor into going deeper into ObjectField
return null;
}

collectNode(node);
collectInputType(parentInputTypeName, node.name.value);
},
Expand Down
81 changes: 81 additions & 0 deletions packages/libraries/core/tests/collect-schema-coordinates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,85 @@ describe('collectSchemaCoordinates', () => {
});
expect(Array.from(result)).toEqual(['Query.node', 'Node.id', 'User.id']);
});

test('custom scalar as argument', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
random(json: JSON): String
}
scalar JSON
`);
const result = collectSchemaCoordinates({
documentNode: parse(/* GraphQL */ `
query {
random(json: { key: { value: "value" } })
}
`),
schema,
processVariables: false,
variables: null,
typeInfo: new TypeInfo(schema),
});
expect(Array.from(result)).toEqual(['Query.random', 'Query.random.json', 'JSON']);
});

test('custom scalar in input object field', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
random(input: I): String
}
input I {
json: JSON
}
scalar JSON
`);
const result = collectSchemaCoordinates({
documentNode: parse(/* GraphQL */ `
query {
random(input: { json: { key: { value: "value" } } })
}
`),
schema,
processVariables: false,
variables: null,
typeInfo: new TypeInfo(schema),
});
expect(Array.from(result)).toEqual(['Query.random', 'Query.random.input', 'I.json', 'JSON']);
});

test('deeply nested inputs', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
random(a: A): String
}
input A {
b: B
}
input B {
c: C
}
input C {
d: String
}
`);
const result = collectSchemaCoordinates({
documentNode: parse(/* GraphQL */ `
query {
random(a: { b: { c: { d: "D" } } })
}
`),
schema,
processVariables: false,
variables: null,
typeInfo: new TypeInfo(schema),
});
expect(Array.from(result).sort()).toEqual(
['Query.random', 'Query.random.a', 'A.b', 'B.c', 'C.d', 'String'].sort(),
);
});
});
Loading

0 comments on commit 93dfbb2

Please sign in to comment.