-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spec compliance: Validation error on multi-field subscription (#882)
As per the spec, a subscription should simply use the first field defined, and not make any other assertions during the Subscribe operation. Instead, a validation rule should detect this and report it.
- Loading branch information
Showing
7 changed files
with
156 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import { describe, it } from 'mocha'; | ||
import { expectPassesRule, expectFailsRule } from './harness'; | ||
import { | ||
SingleFieldSubscriptions, | ||
singleFieldOnlyMessage, | ||
} from '../rules/SingleFieldSubscriptions'; | ||
|
||
|
||
describe('Validate: Subscriptions with single field', () => { | ||
|
||
it('valid subscription', () => { | ||
expectPassesRule(SingleFieldSubscriptions, ` | ||
subscription ImportantEmails { | ||
importantEmails | ||
} | ||
`); | ||
}); | ||
|
||
it('fails with more than one root field', () => { | ||
expectFailsRule(SingleFieldSubscriptions, ` | ||
subscription ImportantEmails { | ||
importantEmails | ||
notImportantEmails | ||
} | ||
`, [ { | ||
message: singleFieldOnlyMessage('ImportantEmails'), | ||
locations: [ { line: 4, column: 9 } ], | ||
path: undefined, | ||
} ]); | ||
}); | ||
|
||
it('fails with more than one root field including introspection', () => { | ||
expectFailsRule(SingleFieldSubscriptions, ` | ||
subscription ImportantEmails { | ||
importantEmails | ||
__typename | ||
} | ||
`, [ { | ||
message: singleFieldOnlyMessage('ImportantEmails'), | ||
locations: [ { line: 4, column: 9 } ], | ||
path: undefined, | ||
} ]); | ||
}); | ||
|
||
it('fails with many more than one root field', () => { | ||
expectFailsRule(SingleFieldSubscriptions, ` | ||
subscription ImportantEmails { | ||
importantEmails | ||
notImportantEmails | ||
spamEmails | ||
} | ||
`, [ { | ||
message: singleFieldOnlyMessage('ImportantEmails'), | ||
locations: [ { line: 4, column: 9 }, { line: 5, column: 9 } ], | ||
path: undefined, | ||
} ]); | ||
}); | ||
|
||
it('fails with more than one root field in anonymous subscriptions', () => { | ||
expectFailsRule(SingleFieldSubscriptions, ` | ||
subscription { | ||
importantEmails | ||
notImportantEmails | ||
} | ||
`, [ { | ||
message: singleFieldOnlyMessage(null), | ||
locations: [ { line: 4, column: 9 } ], | ||
path: undefined, | ||
} ]); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* @flow */ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import type { ValidationContext } from '../index'; | ||
import { GraphQLError } from '../../error'; | ||
import type { OperationDefinitionNode } from '../../language/ast'; | ||
|
||
|
||
export function singleFieldOnlyMessage(name: ?string): string { | ||
return (name ? `Subscription "${name}" ` : 'Anonymous Subscription ') + | ||
'must select only one top level field.'; | ||
} | ||
|
||
/** | ||
* Subscriptions must only include one field. | ||
* | ||
* A GraphQL subscription is valid only if it contains a single root field. | ||
*/ | ||
export function SingleFieldSubscriptions(context: ValidationContext): any { | ||
return { | ||
OperationDefinition(node: OperationDefinitionNode) { | ||
if (node.operation === 'subscription') { | ||
if (node.selectionSet.selections.length !== 1) { | ||
context.reportError(new GraphQLError( | ||
singleFieldOnlyMessage(node.name && node.name.value), | ||
node.selectionSet.selections.slice(1) | ||
)); | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters