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

Validate against @stream on different instances of the same field #4342

Merged
merged 1 commit into from
Feb 12, 2025
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
40 changes: 29 additions & 11 deletions src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,22 @@ describe('Validate: Overlapping fields can be merged', () => {
`);
});

it('Same stream directives supported', () => {
expectValid(`
it('stream directive used on different instances of the same field', () => {
expectErrors(`
fragment differentDirectivesWithDifferentAliases on Dog {
name @stream(label: "streamLabel", initialCount: 1)
name @stream(label: "streamLabel", initialCount: 1)
}
`);
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have overlapping stream directives. See https://github.com/graphql/defer-stream-wg/discussions/100. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
],
},
]);
});

it('different stream directive label', () => {
Expand All @@ -116,7 +125,7 @@ describe('Validate: Overlapping fields can be merged', () => {
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
'Fields "name" conflict because they have overlapping stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
Expand All @@ -134,7 +143,7 @@ describe('Validate: Overlapping fields can be merged', () => {
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
'Fields "name" conflict because they have overlapping stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
Expand All @@ -152,7 +161,7 @@ describe('Validate: Overlapping fields can be merged', () => {
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
'Fields "name" conflict because they have overlapping stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
Expand All @@ -170,7 +179,7 @@ describe('Validate: Overlapping fields can be merged', () => {
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
'Fields "name" conflict because they have overlapping stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
Expand All @@ -188,7 +197,7 @@ describe('Validate: Overlapping fields can be merged', () => {
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
'Fields "name" conflict because they have overlapping stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
Expand All @@ -206,7 +215,7 @@ describe('Validate: Overlapping fields can be merged', () => {
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
'Fields "name" conflict because they have overlapping stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
Expand All @@ -216,12 +225,21 @@ describe('Validate: Overlapping fields can be merged', () => {
});

it('different stream directive both missing args', () => {
expectValid(`
expectErrors(`
fragment conflictingArgs on Dog {
name @stream
name @stream
}
`);
`).toDeepEqual([
{
message:
'Fields "name" conflict because they have overlapping stream directives. See https://github.com/graphql/defer-stream-wg/discussions/100. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
],
},
]);
});

it('Same aliases with different field targets', () => {
Expand Down
33 changes: 17 additions & 16 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,14 @@ function findConflict(

const directives1 = node1.directives ?? [];
const directives2 = node2.directives ?? [];
if (!sameStreams(directives1, varMap1, directives2, varMap2)) {
return [
[responseName, 'they have differing stream directives'],
[node1],
[node2],
];
const overlappingStreamReason = hasNoOverlappingStreams(
directives1,
varMap1,
directives2,
varMap2,
);
if (overlappingStreamReason !== undefined) {
return [[responseName, overlappingStreamReason], [node1], [node2]];
}

// The return type for each field.
Expand Down Expand Up @@ -830,28 +832,27 @@ function getStreamDirective(
return directives.find((directive) => directive.name.value === 'stream');
}

function sameStreams(
function hasNoOverlappingStreams(
directives1: ReadonlyArray<DirectiveNode>,
varMap1: Map<string, ValueNode> | undefined,
directives2: ReadonlyArray<DirectiveNode>,
varMap2: Map<string, ValueNode> | undefined,
): boolean {
): string | undefined {
const stream1 = getStreamDirective(directives1);
const stream2 = getStreamDirective(directives2);
if (!stream1 && !stream2) {
// both fields do not have streams
return true;
return;
} else if (stream1 && stream2) {
// check if both fields have equivalent streams
return sameArguments(
stream1.arguments,
varMap1,
stream2.arguments,
varMap2,
);
if (sameArguments(stream1.arguments, varMap1, stream2.arguments, varMap2)) {
// This was allowed in previous alpha versions of `graphql-js`.
return 'they have overlapping stream directives. See https://github.com/graphql/defer-stream-wg/discussions/100';
}
return 'they have overlapping stream directives';
}
// fields have a mix of stream and no stream
return false;
return 'they have overlapping stream directives';
}

// Two types conflict if both types could not apply to a value simultaneously.
Expand Down
Loading