Skip to content

Commit 2fd5bf7

Browse files
arcanisdotansimha
andauthored
Adds support for #graphql comment prefix in the language server (#1941)
* 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>
1 parent e103cac commit 2fd5bf7

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

.changeset/slow-books-repeat.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'graphql-language-service-server': patch
3+
'graphql-language-service': patch
4+
---
5+
6+
Adds support for `#graphql` and `/* GraphQL */` in the language server

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ packages/graphiql/*.html
6060

6161
# Vendored files
6262
/packages/graphiql/test/vendor
63+
64+
.changeset

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
js-green-licenses.json
2+
.changeset

packages/graphql-language-service-server/src/__tests__/MessageProcessor-test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,64 @@ query Test {
400400
`);
401401
});
402402

403+
it('parseDocument finds queries in #graphql-annotated templates', async () => {
404+
const text = `
405+
import {gql} from 'react-apollo';
406+
import {B} from 'B';
407+
import A from './A';
408+
409+
const QUERY: string = \`#graphql
410+
query Test {
411+
test {
412+
value
413+
...FragmentsComment
414+
}
415+
}
416+
\${A.fragments.test}
417+
\`
418+
419+
export function Example(arg: string) {}`;
420+
421+
const contents = parseDocument(text, 'test.ts');
422+
expect(contents[0].query).toEqual(`#graphql
423+
query Test {
424+
test {
425+
value
426+
...FragmentsComment
427+
}
428+
}
429+
`);
430+
});
431+
432+
it('parseDocument finds queries in /*GraphQL*/-annotated templates', async () => {
433+
const text = `
434+
import {gql} from 'react-apollo';
435+
import {B} from 'B';
436+
import A from './A';
437+
438+
const QUERY: string = /* GraphQL */ \`
439+
query Test {
440+
test {
441+
value
442+
...FragmentsComment
443+
}
444+
}
445+
\${A.fragments.test}
446+
\`
447+
448+
export function Example(arg: string) {}`;
449+
450+
const contents = parseDocument(text, 'test.ts');
451+
expect(contents[0].query).toEqual(`
452+
query Test {
453+
test {
454+
value
455+
...FragmentsComment
456+
}
457+
}
458+
`);
459+
});
460+
403461
it('parseDocument ignores non gql tagged templates', async () => {
404462
const text = `
405463
// @flow

packages/graphql-language-service-server/src/findGraphQLTags.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ export function findGraphQLTags(text: string, ext: string): TagResult[] {
167167
}
168168
}
169169
},
170+
TemplateLiteral: (node: TemplateLiteral) => {
171+
const hasGraphQLPrefix = node.quasis[0].value.raw.startsWith(
172+
'#graphql\n',
173+
);
174+
const hasGraphQLComment = Boolean(
175+
node.leadingComments?.[0]?.value.match(/^\s*GraphQL\s*$/),
176+
);
177+
if (hasGraphQLPrefix || hasGraphQLComment) {
178+
const loc = node.quasis[0].loc;
179+
if (loc) {
180+
const range = new Range(
181+
new Position(loc.start.line - 1, loc.start.column),
182+
new Position(loc.end.line - 1, loc.end.column),
183+
);
184+
result.push({
185+
tag: '',
186+
template: node.quasis[0].value.raw,
187+
range,
188+
});
189+
}
190+
}
191+
},
170192
};
171193
visit(ast, visitors);
172194
return result;

0 commit comments

Comments
 (0)