Skip to content

Commit 795cffc

Browse files
authored
Error with duplicate operation names (#1466)
1 parent 400bf99 commit 795cffc

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- <First `apollo-graphql` related entry goes here>
2121
- `apollo-language-server`
2222
- Add debugging util classes for better error/warning handling [#1429](https://github.com/apollographql/apollo-tooling/pull/1429)
23+
- Add error for duplicate client operation names [#1466](https://github.com/apollographql/apollo-tooling/pull/1466)
2324
- `apollo-tools`
2425
- <First `apollo-tools` related entry goes here>
2526
- `vscode-apollo`

packages/apollo-language-server/src/project/base.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
TypeSystemExtensionNode,
99
isTypeSystemExtensionNode,
1010
DefinitionNode,
11-
GraphQLSchema
11+
GraphQLSchema,
12+
Kind
1213
} from "graphql";
1314

1415
import {
@@ -203,6 +204,7 @@ export abstract class GraphQLProject implements GraphQLSchemaProvider {
203204

204205
fileWasDeleted(uri: DocumentUri) {
205206
this.removeGraphQLDocumentsFor(uri);
207+
this.checkForDuplicateOperations();
206208
}
207209

208210
documentDidChange(document: TextDocument) {
@@ -216,6 +218,24 @@ export abstract class GraphQLProject implements GraphQLSchemaProvider {
216218
} else {
217219
this.removeGraphQLDocumentsFor(document.uri);
218220
}
221+
this.checkForDuplicateOperations();
222+
}
223+
224+
checkForDuplicateOperations(): void {
225+
const operations = Object.create(null);
226+
for (const document of this.documents) {
227+
if (!document.ast) continue;
228+
for (const definition of document.ast.definitions) {
229+
if (definition.kind === Kind.OPERATION_DEFINITION && definition.name) {
230+
if (operations[definition.name.value]) {
231+
throw new Error(
232+
`️️There are multiple definitions for the ${definition.name.value} operation. All operations in a project must have unique names. If generating types, only the types for the first definition found will be generated.`
233+
);
234+
}
235+
operations[definition.name.value] = definition;
236+
}
237+
}
238+
}
219239
}
220240

221241
private removeGraphQLDocumentsFor(uri: DocumentUri) {

0 commit comments

Comments
 (0)