-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gql call expressions support (#2509)
Add ```gql(``)```, ```graphql(``)``` call expressions support for highlighting & language support - adds highlighting to grammar - create simper unit tests for just `findGraphQLTags` - removes some complex and seemingly unused legacy relay-related behaviour from `findGraphQLTags`. If you have any cases where graphql language support disappears with a nested graphql template tag/etc that worked before this, please let us know! Co-authored-by: Rikki Schulte <rikki.schulte@gmail.com>
- Loading branch information
Showing
7 changed files
with
307 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'vscode-graphql': patch | ||
'graphql-language-service-server': patch | ||
'graphql-language-service-cli': patch | ||
--- | ||
|
||
Add ```gql(``)```, ```graphql(``)``` call expressions support for highlighting & language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,3 +233,4 @@ runtimes | |
typeahead | ||
typeaheads | ||
unparsable | ||
randomthing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
packages/graphql-language-service-server/src/__tests__/findGraphQLTags-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/** | ||
* Copyright (c) 2022 GraphQL Contributors | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
import { tmpdir } from 'os'; | ||
|
||
import { findGraphQLTags as baseFindGraphQLTags } from '../findGraphQLTags'; | ||
|
||
jest.mock('../Logger'); | ||
|
||
import { Logger } from '../Logger'; | ||
|
||
describe('findGraphQLTags', () => { | ||
const logger = new Logger(tmpdir()); | ||
const findGraphQLTags = (text: string, ext: string) => | ||
baseFindGraphQLTags(text, ext, '', logger); | ||
|
||
it('finds queries in tagged templates', async () => { | ||
const text = ` | ||
// @flow | ||
import {gql} from 'react-apollo'; | ||
import type {B} from 'B'; | ||
import A from './A'; | ||
const QUERY = gql\` | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
\${A.fragments.test} | ||
\` | ||
export function Example(arg: string) {}`; | ||
|
||
const contents = findGraphQLTags(text, '.js'); | ||
expect(contents[0].template).toEqual(` | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
`); | ||
}); | ||
|
||
it('finds queries in call expressions with template literals', async () => { | ||
const text = ` | ||
// @flow | ||
import {gql} from 'react-apollo'; | ||
import type {B} from 'B'; | ||
import A from './A'; | ||
const QUERY = gql(\` | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
\${A.fragments.test} | ||
\`); | ||
export function Example(arg: string) {}`; | ||
|
||
const contents = findGraphQLTags(text, '.js'); | ||
expect(contents[0].template).toEqual(` | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
`); | ||
}); | ||
|
||
it('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 = findGraphQLTags(text, '.ts'); | ||
expect(contents[0].template).toEqual(`#graphql | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
`); | ||
}); | ||
|
||
it('finds queries in /* GraphQL */ prefixed 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 = findGraphQLTags(text, '.ts'); | ||
expect(contents[0].template).toEqual(` | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
`); | ||
}); | ||
|
||
it('finds queries with nested template tag expressions', async () => { | ||
const text = `export default { | ||
else: () => gql\` query {} \` | ||
}`; | ||
|
||
const contents = findGraphQLTags(text, '.ts'); | ||
expect(contents[0].template).toEqual(` query {} `); | ||
}); | ||
|
||
it('finds queries with template tags inside call expressions', async () => { | ||
const text = `something({ | ||
else: () => gql\` query {} \` | ||
})`; | ||
|
||
const contents = findGraphQLTags(text, '.ts'); | ||
expect(contents[0].template).toEqual(` query {} `); | ||
}); | ||
|
||
it('ignores non gql tagged templates', async () => { | ||
const text = ` | ||
// @flow | ||
import randomthing from 'package'; | ||
import type {B} from 'B'; | ||
import A from './A'; | ||
const QUERY = randomthing\` | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
\${A.fragments.test} | ||
\` | ||
export function Example(arg: string) {}`; | ||
|
||
const contents = findGraphQLTags(text, '.js'); | ||
expect(contents.length).toEqual(0); | ||
}); | ||
|
||
it('ignores non gql call expressions with template literals', async () => { | ||
const text = ` | ||
// @flow | ||
import randomthing from 'package'; | ||
import type {B} from 'B'; | ||
import A from './A'; | ||
const QUERY = randomthing(\` | ||
query Test { | ||
test { | ||
value | ||
...FragmentsComment | ||
} | ||
} | ||
\${A.fragments.test} | ||
\`); | ||
export function Example(arg: string) {}`; | ||
|
||
const contents = findGraphQLTags(text, '.js'); | ||
expect(contents.length).toEqual(0); | ||
}); | ||
}); |
Oops, something went wrong.