Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Specify file names for conflicting operation definition message #29

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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