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

feat: fragment-arguments execution #6013

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/delegate/src/mergeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,16 @@ function handleResolverResult(
const type = schema.getType(object.__typename) as GraphQLObjectType;
const { fields } = collectFields(schema, EMPTY_OBJECT, EMPTY_OBJECT, type, selectionSet);
const nullResult: Record<string, any> = {};
for (const [responseKey, fieldNodes] of fields) {
for (const [responseKey, fieldGroups] of fields) {
const combinedPath = [...path, responseKey];
if (resolverResult instanceof GraphQLError) {
nullResult[responseKey] = relocatedError(resolverResult, combinedPath);
} else if (resolverResult instanceof Error) {
nullResult[responseKey] = locatedError(resolverResult, fieldNodes, combinedPath);
nullResult[responseKey] = locatedError(
resolverResult,
fieldGroups.map(group => group.fieldNode),
combinedPath,
);
} else {
nullResult[responseKey] = null;
}
Expand Down
294 changes: 293 additions & 1 deletion packages/executor/src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { inspect } from 'cross-inspect';
import {
GraphQLArgumentConfig,
Expand Down Expand Up @@ -30,6 +29,13 @@
},
});

const NestedType: GraphQLObjectType = new GraphQLObjectType({
name: 'NestedType',
fields: {
echo: fieldWithInputArg({ type: GraphQLString }),
},
});

const TestInputObject = new GraphQLInputObjectType({
name: 'TestInputObject',
fields: {
Expand Down Expand Up @@ -98,6 +104,10 @@
defaultValue: 'Hello World',
}),
list: fieldWithInputArg({ type: new GraphQLList(GraphQLString) }),
nested: {
type: NestedType,
resolve: () => ({}),
},
nnList: fieldWithInputArg({
type: new GraphQLNonNull(new GraphQLList(GraphQLString)),
}),
Expand All @@ -117,6 +127,15 @@
return executeSync({ schema, document, variableValues });
}

function executeQueryWithFragmentArguments(
query: string,
variableValues?: { [variable: string]: unknown },
) {
// TODO: figure out how to do custom parser here
const document = parse(query, { experimentalFragmentArguments: true });

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Type Check on GraphQL v16

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 100 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 10 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Browser Test

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 3 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / ESM Test

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 50 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Type Check on GraphQL v17.0.0-alpha.1

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Type Check on GraphQL v15

Object literal may only specify known properties, but 'experimentalFragmentArguments' does not exist in type 'ParseOptions'. Did you mean to write 'experimentalFragmentVariables'?

Check failure on line 135 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 1000 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pending, how do we want to tackle having an alternative parser?

return executeSync({ schema, document, variableValues });
}

describe('Execute: Handles inputs', () => {
describe('Handles objects and nullability', () => {
describe('using inline structs', () => {
Expand Down Expand Up @@ -1038,4 +1057,277 @@
});
});
});

describe('using fragment arguments', () => {
it('when there are no fragment arguments', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a on TestType {
fieldWithNonNullableStringInput(input: "A")
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"A"',
},
});
});

it('when a value is required and provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: "A")
}
fragment a($value: String!) on TestType {
fieldWithNonNullableStringInput(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"A"',
},
});
});

it('when a value is required and not provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($value: String!) on TestType {
fieldWithNullableStringInput(input: $value)
}
`);

expect(result).toHaveProperty('errors');
expect(result.errors).toHaveLength(1);
expect(result.errors?.at(0)?.message).toMatch(/Argument "value" of required type "String!"/);
});

it('when the definition has a default and is provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: "A")
}
fragment a($value: String! = "B") on TestType {
fieldWithNonNullableStringInput(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"A"',
},
});
});

it('when the definition has a default and is not provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($value: String! = "B") on TestType {
fieldWithNonNullableStringInput(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"B"',
},
});
});

it('when a definition has a default, is not provided, and spreads another fragment', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($a: String! = "B") on TestType {
...b(b: $a)
}
fragment b($b: String!) on TestType {
fieldWithNonNullableStringInput(input: $b)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"B"',
},
});
});

it('when the definition has a non-nullable default and is provided null', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: null)
}
fragment a($value: String! = "B") on TestType {
fieldWithNullableStringInput(input: $value)
}
`);

expect(result).toHaveProperty('errors');
expect(result.errors).toHaveLength(1);
expect(result.errors?.at(0)?.message).toMatch(/Argument "value" of non-null type "String!"/);
});

it('when the definition has no default and is not provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($value: String) on TestType {
fieldWithNonNullableStringInputAndDefaultArgumentValue(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInputAndDefaultArgumentValue: '"Hello World"',
},
});
});

it('when an argument is shadowed by an operation variable', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String! = "A") {
...a(x: "B")
}
fragment a($x: String) on TestType {
fieldWithNullableStringInput(input: $x)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNullableStringInput: '"B"',
},
});
});

it('when a nullable argument with a field default is not provided and shadowed by an operation variable', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String = "A") {
...a
}
fragment a($x: String) on TestType {
fieldWithNonNullableStringInputAndDefaultArgumentValue(input: $x)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInputAndDefaultArgumentValue: '"Hello World"',
},
});
});

it('when a fragment-variable is shadowed by an intermediate fragment-spread but defined in the operation-variables', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String = "A") {
...a
}
fragment a($x: String) on TestType {
...b
}
fragment b on TestType {
fieldWithNullableStringInput(input: $x)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNullableStringInput: '"A"',
},
});
});

it('when a fragment is used with different args', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String = "Hello") {
a: nested {
...a(x: "a")
}
b: nested {
...a(x: "b", b: true)
}
hello: nested {
...a(x: $x)
}
}
fragment a($x: String, $b: Boolean = false) on NestedType {
a: echo(input: $x) @skip(if: $b)
b: echo(input: $x) @include(if: $b)
}
`);
expectJSON(result).toDeepEqual({
data: {
a: {
a: '"a"',
},
b: {
b: '"b"',
},
hello: {
a: '"Hello"',
},
},
});
});

it('when the argument variable is nested in a complex type', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: "C")
}
fragment a($value: String) on TestType {
list(input: ["A", "B", $value, "D"])
}
`);
expectJSON(result).toDeepEqual({
data: {
list: '["A", "B", "C", "D"]',
},
});
});

it('when argument variables are used recursively', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(aValue: "C")
}
fragment a($aValue: String) on TestType {
...b(bValue: $aValue)
}
fragment b($bValue: String) on TestType {
list(input: ["A", "B", $bValue, "D"])
}
`);
expectJSON(result).toDeepEqual({
data: {
list: '["A", "B", "C", "D"]',
},
});
});

it('when argument passed in as list', () => {
const result = executeQueryWithFragmentArguments(`
query Q($opValue: String = "op") {
...a(aValue: "A")
}
fragment a($aValue: String, $bValue: String) on TestType {
...b(aValue: [$aValue, "B"], bValue: [$bValue, $opValue])
}
fragment b($aValue: [String], $bValue: [String], $cValue: String) on TestType {
aList: list(input: $aValue)
bList: list(input: $bValue)
cList: list(input: [$cValue])
}
`);
expectJSON(result).toDeepEqual({
data: {
aList: '["A", "B"]',
bList: '[null, "op"]',
cList: '[null]',
},
});
});
});
});
Loading
Loading