Skip to content

Commit

Permalink
fix: Specify file names for conflicting operation definition message (#…
Browse files Browse the repository at this point in the history
…29)

* Display names in the 'multiple definitions for operation' error

* Update CHANGELOG
  • Loading branch information
Jason Zukewich committed Oct 7, 2021
1 parent be33ad4 commit a319aab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### Next

- Specify files with conflicting documents for the 'There are multiple definitions' message. [#29](https://github.com/apollographql/vscode-graphql/pull/29)

### 1.19.6

- Fix 'Run in explorer' link. [#25](https://github.com/apollographql/vscode-graphql/pull/25)
Expand Down
29 changes: 19 additions & 10 deletions src/language-server/project/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,26 @@ export abstract class GraphQLProject implements GraphQLSchemaProvider {
}

checkForDuplicateOperations(): void {
const operations = Object.create(null);
for (const document of this.documents) {
if (!document.ast) continue;
for (const definition of document.ast.definitions) {
if (definition.kind === Kind.OPERATION_DEFINITION && definition.name) {
if (operations[definition.name.value]) {
throw new Error(
`️️There are multiple definitions for the \`${definition.name.value}\` operation. Please rename or remove all operations with the duplicated name before continuing.`
);
const filePathForOperationName: Record<string, string> = {};
for (const [fileUri, documentsForFile] of this.documentsByFile.entries()) {
const filePath = URI.parse(fileUri).fsPath;
for (const document of documentsForFile) {
if (!document.ast) continue;
for (const definition of document.ast.definitions) {
if (
definition.kind === Kind.OPERATION_DEFINITION &&
definition.name
) {
const operationName = definition.name.value;
if (operationName in filePathForOperationName) {
const conflictingFilePath =
filePathForOperationName[operationName];
throw new Error(
`️️There are multiple definitions for the \`${definition.name.value}\` operation. Please fix all naming conflicts before continuing.\nConflicting definitions found at ${filePath} and ${conflictingFilePath}.`
);
}
filePathForOperationName[operationName] = filePath;
}
operations[definition.name.value] = definition;
}
}
}
Expand Down

0 comments on commit a319aab

Please sign in to comment.