Skip to content

Commit

Permalink
fix: test file/prettier whitespace conflict, add test for inline SDL …
Browse files Browse the repository at this point in the history
…in ts
  • Loading branch information
acao committed May 1, 2024
1 parent 3d9df81 commit f24bb65
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# deliate whitespace character assertions:
packages/graphql-language-service-server/src/__tests__/parseDocument-test.ts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"prepublishOnly": "./scripts/prepublish.sh",
"postbuild": "wsrun --exclude-missing postbuild",
"pretty": "yarn pretty-check --write",
"pretty-check": "prettier --cache --check --ignore-path .gitignore --ignore-path .eslintignore .",
"pretty-check": "prettier --cache --check --ignore-path .gitignore --ignore-path .prettierignore --ignore-path .eslintignore .",
"ci:version": "yarn changeset version && yarn build && yarn format",
"release": "yarn build && yarn build-bundles && (wsrun release --exclude-missing --serial --recursive --changedSince main -- || true) && yarn changeset publish",
"release:canary": "(node scripts/canary-release.js && yarn build-bundles && yarn changeset publish --tag canary) || echo Skipping Canary...",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const fooTypePosition = {
end: { line: 2, character: 24 },
};

const fooInlineTypePosition = {
start: { line: 5, character: 0 },
end: { line: 5, character: 24 },
};

const genSchemaPath =
'/tmp/graphql-language-service/test/projects/default/generated-schema.graphql';

Expand Down Expand Up @@ -96,7 +101,7 @@ describe('MessageProcessor with no config', () => {
});
});

describe('project with simple config and graphql files', () => {
describe('MessageProcessor with config', () => {
let app;
afterEach(() => {
mockfs.restore();
Expand Down Expand Up @@ -447,12 +452,15 @@ describe('project with simple config and graphql files', () => {
'b/fragments.ts',
'\n\n\nexport const fragment = gql`\n\n fragment T on Test { isTest }\n`',
],
['b/schema.graphql', schemaFile[1]],
[
'b/schema.ts',
`\n\nexport const schema = gql(\`\n${schemaFile[1]}\`)`,
],
[
'package.json',
`{ "graphql": { "projects": {
"a": { "schema": "http://localhost:3100/graphql", "documents": "./a/**" },
"b": { "schema": "./b/schema.graphql", "documents": "./b/**" } }
"b": { "schema": "./b/schema.ts", "documents": "./b/**" } }
}
}`,
],
Expand Down Expand Up @@ -491,25 +499,29 @@ describe('project with simple config and graphql files', () => {
// this confirms that autocomplete respects cross-project boundaries for types.
// it performs a definition request for the foo field in Query
const schemaCompletion1 = await project.lsp.handleCompletionRequest({
textDocument: { uri: project.uri('b/schema.graphql') },
position: { character: 21, line: 0 },
textDocument: { uri: project.uri('b/schema.ts') },
position: { character: 21, line: 3 },
});
expect(schemaCompletion1.items.map(i => i.label)).toEqual(['Foo']);
// it performs a definition request for the Foo type in Test.test
const schemaDefinition = await project.lsp.handleDefinitionRequest({
textDocument: { uri: project.uri('b/schema.graphql') },
position: { character: 21, line: 4 },
textDocument: { uri: project.uri('b/schema.ts') },
position: { character: 21, line: 6 },
});
expect(serializeRange(schemaDefinition[0].range)).toEqual(fooTypePosition);
expect(serializeRange(schemaDefinition[0].range)).toEqual(
fooInlineTypePosition,
);
expect(project.lsp._logger.error).not.toHaveBeenCalled();
// simulate a watched schema file change (codegen, etc)
project.changeFile(
'b/schema.graphql',
schemaFile[1] + '\ntype Example1 { field: }',
'b/schema.ts',
`\n\nexport const schema = gql(\`\n${
schemaFile[1] + '\ntype Example1 { field: }'
}\`\n)`,
);
await project.lsp.handleWatchedFilesChangedNotification({
changes: [
{ uri: project.uri('b/schema.graphql'), type: FileChangeType.Changed },
{ uri: project.uri('b/schema.ts'), type: FileChangeType.Changed },
],
});
// TODO: repeat this with other changes to the schema file and use a
Expand All @@ -522,8 +534,8 @@ describe('project with simple config and graphql files', () => {
// });
// console.log(project.fileCache.get('b/schema.graphql'));
const schemaCompletion = await project.lsp.handleCompletionRequest({
textDocument: { uri: project.uri('b/schema.graphql') },
position: { character: 25, line: 5 },
textDocument: { uri: project.uri('b/schema.ts') },
position: { character: 25, line: 8 },
});
// TODO: SDL completion still feels incomplete here... where is Int?
// where is self-referential Example1?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ query Test {
`);
});

it('finds queries in call expressions with with newlines preceding the template', async () => {
const text = `
import {gql} from 'react-apollo';
import type {B} from 'B';
import A from './A';
const QUERY = gql(
\`
query Test {
test {
value
...FragmentsComment
}
}
\`);
export function Example(arg: string) {}`;

const contents = findGraphQLTags(text, '.ts');
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';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/** prettier-ignore-file */
import { parseDocument } from '../parseDocument';

describe('parseDocument', () => {
Expand Down Expand Up @@ -89,8 +88,7 @@ describe('parseDocument', () => {
`);
});

it
('parseDocument finds queries in tagged templates using tsx', async () => {
it('parseDocument finds queries in tagged templates using tsx', async () => {
const text = `
import {gql} from 'react-apollo';
import {B} from 'B';
Expand Down Expand Up @@ -304,7 +302,7 @@ describe('parseDocument', () => {
export function Example(arg: string) {}`;

const contents = parseDocument(text, 'test.ts');
/* prettier-ignore-start */
// please let me keep this whitespace prettier!
expect(contents[0].query).toEqual(/* GraphQL */ `
query Test {
test {
Expand All @@ -314,7 +312,6 @@ describe('parseDocument', () => {
}
`);
/* prettier-ignore-end */
});

it('parseDocument ignores non gql tagged templates', async () => {
Expand Down

0 comments on commit f24bb65

Please sign in to comment.