-
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.
Add KnownOperationTypes rule (#3601)
New Spec PR: graphql/graphql-spec#1098 Old Spec PR: graphql/graphql-spec#947 Original issue raised by @benjaminjkraft : #3592
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { describe, it } from 'mocha'; | ||
|
||
import { KnownOperationTypesRule } from '../rules/KnownOperationTypesRule.js'; | ||
|
||
import { expectValidationErrors } from './harness.js'; | ||
|
||
function expectErrors(queryStr: string) { | ||
return expectValidationErrors(KnownOperationTypesRule, queryStr); | ||
} | ||
|
||
function expectValid(queryStr: string) { | ||
expectErrors(queryStr).toDeepEqual([]); | ||
} | ||
|
||
describe('Validate: Known operation types', () => { | ||
it('one known operation', () => { | ||
expectValid(` | ||
{ field } | ||
`); | ||
}); | ||
|
||
it('unknown mutation operation', () => { | ||
expectErrors(` | ||
mutation { field } | ||
`).toDeepEqual([ | ||
{ | ||
message: 'The mutation operation is not supported by the schema.', | ||
locations: [{ line: 2, column: 7 }], | ||
}, | ||
]); | ||
}); | ||
|
||
it('unknown subscription operation', () => { | ||
expectErrors(` | ||
subscription { field } | ||
`).toDeepEqual([ | ||
{ | ||
message: 'The subscription operation is not supported by the schema.', | ||
locations: [{ line: 2, column: 7 }], | ||
}, | ||
]); | ||
}); | ||
|
||
it('mixture of known and unknown operations', () => { | ||
expectErrors(` | ||
query { field } | ||
mutation { field } | ||
subscription { field } | ||
`).toDeepEqual([ | ||
{ | ||
message: 'The mutation operation is not supported by the schema.', | ||
locations: [{ line: 3, column: 7 }], | ||
}, | ||
{ | ||
message: 'The subscription operation is not supported by the schema.', | ||
locations: [{ line: 4, column: 7 }], | ||
}, | ||
]); | ||
}); | ||
}); |
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,32 @@ | ||
import { GraphQLError } from '../../error/GraphQLError.js'; | ||
|
||
import type { ASTVisitor } from '../../language/visitor.js'; | ||
|
||
import type { ValidationContext } from '../ValidationContext.js'; | ||
|
||
/** | ||
* Known Operation Types | ||
* | ||
* A GraphQL document is only valid if when it contains an operation, | ||
* the root type for the operation exists within the schema. | ||
* | ||
* See https://spec.graphql.org/draft/#sec-Operation-Type-Existence | ||
*/ | ||
export function KnownOperationTypesRule( | ||
context: ValidationContext, | ||
): ASTVisitor { | ||
const schema = context.getSchema(); | ||
return { | ||
OperationDefinition(node) { | ||
const operation = node.operation; | ||
if (!schema.getRootType(operation)) { | ||
context.reportError( | ||
new GraphQLError( | ||
`The ${operation} operation is not supported by the schema.`, | ||
{ nodes: node }, | ||
), | ||
); | ||
} | ||
}, | ||
}; | ||
} |
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