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

OverlappingFieldsCanBeMergedRule suggestions #15

Merged
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
6 changes: 6 additions & 0 deletions src/validation/ValidationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ export class ValidationContext extends ASTValidationContext {
return this._typeInfo.getFragmentSignature();
}

getFragmentSignatureByName(): (
fragmentName: string,
) => Maybe<FragmentSignature> {
return this._typeInfo.getFragmentSignatureByName();
}

getEnumValue(): Maybe<GraphQLEnumValue> {
return this._typeInfo.getEnumValue();
}
Expand Down
121 changes: 112 additions & 9 deletions src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1303,31 +1303,134 @@ describe('Validate: Overlapping fields can be merged', () => {
]);
});

it('encounters conflict in fragment - field no args', () => {
expectErrors(`
it('allows operations with overlapping fields with arguments using identical operation variables', () => {
expectValid(`
query ($y: Int = 1) {
a(x: $y)
...WithArgs
...WithArgs(x: 1)
}
fragment WithArgs on Type {
fragment WithArgs($x: Int = 1) on Type {
a(x: $y)
}
`).toDeepEqual([]);
`);
});

it('encounters conflict in fragment/field', () => {
expectErrors(`
it('allows operations with overlapping fields with identical variable arguments passed via fragment arguments', () => {
expectValid(`
query ($y: Int = 1) {
a(x: $y)
...WithArgs(x: $y)
}
fragment WithArgs($x: Int) on Type {
a(x: $x)
}
`).toDeepEqual([]);
`);
});

it('allows operations with overlapping fields with identical variable arguments passed via nested fragment arguments', () => {
expectValid(`
query ($z: Int = 1) {
a(x: $z)
...WithArgs(y: $z)
}
fragment WithArgs($y: Int) on Type {
...NestedWithArgs(x: $y)
}
fragment NestedWithArgs($x: Int) on Type {
a(x: $x)
}
`);
});

it('allows operations with overlapping fields with identical arguments via fragment variable defaults', () => {
expectValid(`
query {
a(x: 1)
...WithArgs
}
fragment WithArgs($x: Int = 1) on Type {
a(x: $x)
}
`);
});

it('raises errors with overlapping fields with arguments that conflict via operation variables even with defaults and fragment variable defaults', () => {
expectErrors(`
query ($y: Int = 1) {
a(x: $y)
...WithArgs
}
fragment WithArgs($x: Int = 1) on Type {
a(x: $x)
}
`).toDeepEqual([
{
message:
'Fields "a" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 11 },
{ line: 7, column: 11 },
],
},
]);
});

it('allows operations with overlapping list fields with identical variable arguments passed via fragment arguments', () => {
expectValid(`
query Query($stringListVarY: [String]) {
complicatedArgs {
stringListArgField(stringListArg: $stringListVarY)
...WithArgs(stringListVarX: $stringListVarY)
}
}
fragment WithArgs($stringListVarX: [String]) on Type {
stringListArgField(stringListArg: $stringListVarX)
}
`);
});

it('allows operations with overlapping list fields with identical variable arguments in item position passed via fragment arguments', () => {
expectValid(`
query Query($stringListVarY: [String]) {
complicatedArgs {
stringListArgField(stringListArg: [$stringListVarY])
...WithArgs(stringListVarX: $stringListVarY)
}
}
fragment WithArgs($stringListVarX: [String]) on Type {
stringListArgField(stringListArg: [$stringListVarX])
}
`);
});

it('allows operations with overlapping input object fields with identical variable arguments passed via fragment arguments', () => {
expectValid(`
query Query($complexVarY: ComplexInput) {
complicatedArgs {
complexArgField(complexArg: $complexVarY)
...WithArgs(complexVarX: $complexVarY)
}
}
fragment WithArgs($complexVarX: ComplexInput) on Type {
complexArgField(complexArg: $complexVarX)
}
`);
});

it('allows operations with overlapping input object fields with identical variable arguments in field position passed via fragment arguments', () => {
expectValid(`
query Query($boolVarY: Boolean) {
complicatedArgs {
complexArgField(complexArg: {requiredArg: $boolVarY})
...WithArgs(boolVarX: $boolVarY)
}
}
fragment WithArgs($boolVarX: Boolean) on Type {
complexArgField(complexArg: {requiredArg: $boolVarX})
}
`);
});

// This is currently not validated, should we?
it('encounters nested field conflict in fragments that could otherwise merge', () => {
expectErrors(`
query ValidDifferingFragmentArgs($command1: DogCommand, $command2: DogCommand) {
Expand Down
Loading
Loading