Skip to content

Commit

Permalink
Adds support for #graphql comment prefix in the language server (#1941)
Browse files Browse the repository at this point in the history
* Adds support for #graphql in the language server

* Update findGraphQLTags.ts

* Adds tests

* Create slow-books-repeat.md

* Fixes TS

* lower changeset to patch

* fix for false positive on lint/format

Co-authored-by: Dotan Simha <dotansimha@gmail.com>
  • Loading branch information
arcanis and dotansimha authored Sep 30, 2021
1 parent e103cac commit 2fd5bf7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/slow-books-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphql-language-service-server': patch
'graphql-language-service': patch
---

Adds support for `#graphql` and `/* GraphQL */` in the language server
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ packages/graphiql/*.html

# Vendored files
/packages/graphiql/test/vendor

.changeset
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
js-green-licenses.json
.changeset
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,64 @@ query Test {
`);
});

it('parseDocument finds queries in #graphql-annotated templates', async () => {
const text = `
import {gql} from 'react-apollo';
import {B} from 'B';
import A from './A';
const QUERY: string = \`#graphql
query Test {
test {
value
...FragmentsComment
}
}
\${A.fragments.test}
\`
export function Example(arg: string) {}`;

const contents = parseDocument(text, 'test.ts');
expect(contents[0].query).toEqual(`#graphql
query Test {
test {
value
...FragmentsComment
}
}
`);
});

it('parseDocument finds queries in /*GraphQL*/-annotated templates', async () => {
const text = `
import {gql} from 'react-apollo';
import {B} from 'B';
import A from './A';
const QUERY: string = /* GraphQL */ \`
query Test {
test {
value
...FragmentsComment
}
}
\${A.fragments.test}
\`
export function Example(arg: string) {}`;

const contents = parseDocument(text, 'test.ts');
expect(contents[0].query).toEqual(`
query Test {
test {
value
...FragmentsComment
}
}
`);
});

it('parseDocument ignores non gql tagged templates', async () => {
const text = `
// @flow
Expand Down
22 changes: 22 additions & 0 deletions packages/graphql-language-service-server/src/findGraphQLTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ export function findGraphQLTags(text: string, ext: string): TagResult[] {
}
}
},
TemplateLiteral: (node: TemplateLiteral) => {
const hasGraphQLPrefix = node.quasis[0].value.raw.startsWith(
'#graphql\n',
);
const hasGraphQLComment = Boolean(
node.leadingComments?.[0]?.value.match(/^\s*GraphQL\s*$/),
);
if (hasGraphQLPrefix || hasGraphQLComment) {
const loc = node.quasis[0].loc;
if (loc) {
const range = new Range(
new Position(loc.start.line - 1, loc.start.column),
new Position(loc.end.line - 1, loc.end.column),
);
result.push({
tag: '',
template: node.quasis[0].value.raw,
range,
});
}
}
},
};
visit(ast, visitors);
return result;
Expand Down

0 comments on commit 2fd5bf7

Please sign in to comment.