-
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.
Extract check for unique type names into separate rule
- Loading branch information
1 parent
51eda7b
commit 257797a
Showing
7 changed files
with
215 additions
and
77 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
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,146 @@ | ||
/** | ||
* Copyright (c) 2018-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
*/ | ||
|
||
import { describe, it } from 'mocha'; | ||
import { buildSchema } from '../../utilities'; | ||
import { expectSDLValidationErrors } from './harness'; | ||
import { | ||
UniqueTypeNames, | ||
existedTypeNameMessage, | ||
duplicateTypeNameMessage, | ||
} from '../rules/UniqueTypeNames'; | ||
|
||
function expectSDLErrors(sdlStr, schema) { | ||
return expectSDLValidationErrors(schema, UniqueTypeNames, sdlStr); | ||
} | ||
|
||
function expectValidSDL(sdlStr, schema) { | ||
expectSDLErrors(sdlStr, schema).to.deep.equal([]); | ||
} | ||
|
||
describe('Validate: Unique type names', () => { | ||
it('no types', () => { | ||
expectValidSDL(` | ||
directive @test on SCHEMA | ||
`); | ||
}); | ||
|
||
it('one type', () => { | ||
expectValidSDL(` | ||
type Foo | ||
`); | ||
}); | ||
|
||
it('many types', () => { | ||
expectValidSDL(` | ||
type Foo | ||
type Bar | ||
type Baz | ||
`); | ||
}); | ||
|
||
it('type and non-type definitions named the same', () => { | ||
expectValidSDL(` | ||
query Foo { __typename } | ||
fragment Foo on Query { __typename } | ||
directive @Foo on SCHEMA | ||
type Foo | ||
`); | ||
}); | ||
|
||
it('types named the same', () => { | ||
expectSDLErrors(` | ||
type Foo | ||
scalar Foo | ||
type Foo | ||
interface Foo | ||
union Foo | ||
enum Foo | ||
input Foo | ||
`).to.deep.equal([ | ||
{ | ||
message: duplicateTypeNameMessage('Foo'), | ||
locations: [{ line: 2, column: 12 }, { line: 4, column: 14 }], | ||
}, | ||
{ | ||
message: duplicateTypeNameMessage('Foo'), | ||
locations: [{ line: 2, column: 12 }, { line: 5, column: 12 }], | ||
}, | ||
{ | ||
message: duplicateTypeNameMessage('Foo'), | ||
locations: [{ line: 2, column: 12 }, { line: 6, column: 17 }], | ||
}, | ||
{ | ||
message: duplicateTypeNameMessage('Foo'), | ||
locations: [{ line: 2, column: 12 }, { line: 7, column: 13 }], | ||
}, | ||
{ | ||
message: duplicateTypeNameMessage('Foo'), | ||
locations: [{ line: 2, column: 12 }, { line: 8, column: 12 }], | ||
}, | ||
{ | ||
message: duplicateTypeNameMessage('Foo'), | ||
locations: [{ line: 2, column: 12 }, { line: 9, column: 13 }], | ||
}, | ||
]); | ||
}); | ||
|
||
it('adding new type to existing schema', () => { | ||
const schema = buildSchema('type Foo'); | ||
|
||
expectValidSDL('type Bar', schema); | ||
}); | ||
|
||
it('adding new type to existing schema with same-named directive', () => { | ||
const schema = buildSchema('directive @Foo on SCHEMA'); | ||
|
||
expectValidSDL('type Foo', schema); | ||
}); | ||
|
||
it('adding conflicting types to existing schema', () => { | ||
const schema = buildSchema('type Foo'); | ||
const sdl = ` | ||
scalar Foo | ||
type Foo | ||
interface Foo | ||
union Foo | ||
enum Foo | ||
input Foo | ||
`; | ||
|
||
expectSDLErrors(sdl, schema).to.deep.equal([ | ||
{ | ||
message: existedTypeNameMessage('Foo'), | ||
locations: [{ line: 2, column: 14 }], | ||
}, | ||
{ | ||
message: existedTypeNameMessage('Foo'), | ||
locations: [{ line: 3, column: 12 }], | ||
}, | ||
{ | ||
message: existedTypeNameMessage('Foo'), | ||
locations: [{ line: 4, column: 17 }], | ||
}, | ||
{ | ||
message: existedTypeNameMessage('Foo'), | ||
locations: [{ line: 5, column: 13 }], | ||
}, | ||
{ | ||
message: existedTypeNameMessage('Foo'), | ||
locations: [{ line: 6, column: 12 }], | ||
}, | ||
{ | ||
message: existedTypeNameMessage('Foo'), | ||
locations: [{ line: 7, column: 13 }], | ||
}, | ||
]); | ||
}); | ||
}); |
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,67 @@ | ||
/** | ||
* Copyright (c) 2018-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
*/ | ||
|
||
import type { SDLValidationContext } from '../ValidationContext'; | ||
import { GraphQLError } from '../../error/GraphQLError'; | ||
import type { ASTVisitor } from '../../language/visitor'; | ||
import type { TypeDefinitionNode } from '../../language/ast'; | ||
|
||
export function duplicateTypeNameMessage(typeName: string): string { | ||
return `There can be only one type named "${typeName}".`; | ||
} | ||
|
||
export function existedTypeNameMessage(typeName: string): string { | ||
return ( | ||
`Type "${typeName}" already exists in the schema. ` + | ||
'It cannot also be defined in this type definition.' | ||
); | ||
} | ||
|
||
/** | ||
* Unique type names | ||
* | ||
* A GraphQL document is only valid if all defined types have unique names. | ||
*/ | ||
export function UniqueTypeNames(context: SDLValidationContext): ASTVisitor { | ||
const knownTypeNames = Object.create(null); | ||
const schema = context.getSchema(); | ||
|
||
return { | ||
ScalarTypeDefinition: checkTypeName, | ||
ObjectTypeDefinition: checkTypeName, | ||
InterfaceTypeDefinition: checkTypeName, | ||
UnionTypeDefinition: checkTypeName, | ||
EnumTypeDefinition: checkTypeName, | ||
InputObjectTypeDefinition: checkTypeName, | ||
}; | ||
|
||
function checkTypeName(node: TypeDefinitionNode) { | ||
const typeName = node.name.value; | ||
|
||
if (schema && schema.getType(typeName)) { | ||
context.reportError( | ||
new GraphQLError(existedTypeNameMessage(typeName), node.name), | ||
); | ||
return; | ||
} | ||
|
||
if (knownTypeNames[typeName]) { | ||
context.reportError( | ||
new GraphQLError(duplicateTypeNameMessage(typeName), [ | ||
knownTypeNames[typeName], | ||
node.name, | ||
]), | ||
); | ||
} else { | ||
knownTypeNames[typeName] = node.name; | ||
} | ||
|
||
return false; | ||
} | ||
} |
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