Skip to content

Commit 1a99235

Browse files
committed
migrate over the wire tests to use local schema instance
1 parent 98efb74 commit 1a99235

File tree

7 files changed

+39
-28
lines changed

7 files changed

+39
-28
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"files.insertFinalNewline": true,
55
"editor.trimAutoWhitespace": false,
66
"coverage-gutters.showLineCoverage": true,
7-
"coverage-gutters.coverageBaseDir": "coverage/jest",
7+
"coverage-gutters.coverageBaseDir": "coverage",
88
"coverage-gutters.coverageFileNames": [
99
"lcov.info",
1010
"cov.xml",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"cypress-open": "yarn workspace graphiql cypress-open",
5050
"dev-graphiql": "yarn workspace graphiql dev",
5151
"e2e": "yarn run e2e:build && yarn workspace graphiql e2e",
52+
"e2e:server": "yarn workspace graphiql e2e-server",
5253
"e2e:build": "WEBPACK_SERVE=1 yarn workspace graphiql build-bundles",
5354
"eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --max-warnings=0 --ignore-path .gitignore --cache .",
5455
"format": "yarn eslint --fix && yarn pretty",
@@ -70,7 +71,7 @@
7071
"repo:fix": "manypkg fix",
7172
"repo:resolve": "node scripts/set-resolution.js",
7273
"t": "yarn test",
73-
"test": "yarn jest",
74+
"test": "yarn e2e:server yarn jest",
7475
"test:ci": "yarn build && jest --coverage && yarn vitest",
7576
"test:coverage": "yarn jest --coverage",
7677
"test:watch": "yarn jest --watch",

packages/graphiql/test/afterDevServer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ module.exports = function afterDevServer(_app, _server, _compiler) {
1010
});
1111
// eslint-disable-next-line react-hooks/rules-of-hooks
1212
useServer({ schema }, wsServer);
13+
return wsServer;
1314
};

packages/graphiql/test/e2e-server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ app.post('/graphql-error/graphql', (_req, res, next) => {
4343
app.use(express.static(path.resolve(__dirname, '../')));
4444
app.use('index.html', express.static(path.resolve(__dirname, '../dev.html')));
4545

46-
app.listen(process.env.PORT || 0, function () {
46+
// messy but it allows close
47+
const server = require('node:http').createServer(app);
48+
server.listen(process.env.PORT || 3100, function () {
4749
const { port } = this.address();
4850

4951
console.log(`Started on http://localhost:${port}/`);
@@ -56,5 +58,7 @@ app.listen(process.env.PORT || 0, function () {
5658
process.exit();
5759
});
5860
});
61+
const wsServer = WebSocketsServer();
5962

60-
WebSocketsServer();
63+
module.exports.server = server;
64+
module.exports.wsServer = wsServer;

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,17 +1013,12 @@ export class MessageProcessor {
10131013
...DEFAULT_SUPPORTED_EXTENSIONS,
10141014
...DEFAULT_SUPPORTED_GRAPHQL_EXTENSIONS,
10151015
];
1016-
// only local schema lookups if all of the schema entries are local files that we can resolve
1016+
// only local schema lookups if all of the schema entries are local files
10171017
const sdlOnly = unwrappedSchema.every(schemaEntry =>
10181018
allExtensions.some(
1019-
// local schema file URIs for lookup don't start with http, and end with an extension that is not json but may
1020-
// be graphql, gql, ts, js, javascript, or even vue, svelte, etc.
1021-
// would be awesome to use tree sitter to expand our parser to other languages, and then we could support SDL literal
1022-
// definitions in other languages!
1023-
ext =>
1024-
!schemaEntry.startsWith('http') &&
1025-
schemaEntry.endsWith(ext) &&
1026-
ext !== 'json',
1019+
// local schema file URIs for lookup don't start with http, and end with an extension.
1020+
// though it isn't often used, technically schema config could include a remote .graphql file
1021+
ext => !schemaEntry.startsWith('http') && schemaEntry.endsWith(ext),
10271022
),
10281023
);
10291024
// if we are caching the config schema, and it isn't a .graphql file, cache it

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,17 @@ describe('MessageProcessor with no config', () => {
7878
});
7979

8080
describe('project with simple config and graphql files', () => {
81+
let app;
8182
afterEach(() => {
8283
mockfs.restore();
8384
});
85+
beforeAll(async () => {
86+
app = await import('../../../graphiql/test/e2e-server');
87+
});
88+
afterAll(() => {
89+
app.server.close();
90+
app.wsServer.close();
91+
});
8492
it('caches files and schema with .graphql file config, and the schema updates with watched file changes', async () => {
8593
const project = new MockProject({
8694
files: [
@@ -94,7 +102,6 @@ describe('project with simple config and graphql files', () => {
94102
});
95103
await project.init('query.graphql');
96104
expect(project.lsp._logger.error).not.toHaveBeenCalled();
97-
// console.log(project.lsp._graphQLCache.getSchema('schema.graphql'));
98105
expect(await project.lsp._graphQLCache.getSchema()).toBeDefined();
99106
// TODO: for some reason the cache result formats the graphql query??
100107
const docCache = project.lsp._textDocumentCache;
@@ -208,46 +215,48 @@ describe('project with simple config and graphql files', () => {
208215
it('caches files and schema with a URL config', async () => {
209216
const project = new MockProject({
210217
files: [
211-
['query.graphql', 'query { bar }'],
212-
['fragments.graphql', 'fragment Ep on Episode {\n created \n}'],
218+
['query.graphql', 'query { test { isTest, ...T } }'],
219+
['fragments.graphql', 'fragment T on Test {\n isTest \n}'],
213220
[
214221
'graphql.config.json',
215-
'{ "schema": "https://rickandmortyapi.com/graphql", "documents": "./**.graphql" }',
222+
'{ "schema": "http://localhost:3100/graphql", "documents": "./**.graphql" }',
216223
],
217224
],
218225
});
219226

220-
await project.init('query.graphql');
227+
const initParams = await project.init('query.graphql');
228+
expect(initParams.diagnostics).toEqual([]);
229+
230+
expect(project.lsp._logger.error).not.toHaveBeenCalled();
221231

222232
const changeParams = await project.lsp.handleDidChangeNotification({
223233
textDocument: { uri: project.uri('query.graphql'), version: 1 },
224-
contentChanges: [
225-
{ text: 'query { episodes { results { ...Ep, nop } } }' },
226-
],
234+
contentChanges: [{ text: 'query { test { isTest, ...T or } }' }],
227235
});
228236
expect(changeParams?.diagnostics[0].message).toEqual(
229-
'Cannot query field "nop" on type "Episode".',
237+
'Cannot query field "or" on type "Test".',
230238
);
231-
expect(project.lsp._logger.error).not.toHaveBeenCalled();
232-
// console.log(project.lsp._graphQLCache.getSchema('schema.graphql'));
233239
expect(await project.lsp._graphQLCache.getSchema()).toBeDefined();
240+
241+
// schema file is present and contains schema
234242
const file = await readFile(join(genSchemaPath), { encoding: 'utf-8' });
235243
expect(file.split('\n').length).toBeGreaterThan(10);
244+
245+
// hover works
236246
const hover = await project.lsp.handleHoverRequest({
237247
position: {
238248
character: 10,
239249
line: 0,
240250
},
241251
textDocument: { uri: project.uri('query.graphql') },
242252
});
243-
expect(project.lsp._textDocumentCache.size).toEqual(3);
253+
expect(hover.contents).toContain('`test` field from `Test` type.');
244254

245-
expect(hover.contents).toContain('Get the list of all episodes');
255+
// ensure that fragment definitions work
246256
const definitions = await project.lsp.handleDefinitionRequest({
247257
textDocument: { uri: project.uri('query.graphql') },
248-
position: { character: 33, line: 0 },
258+
position: { character: 26, line: 0 },
249259
});
250-
// ensure that fragment definitions work
251260
expect(definitions[0].uri).toEqual(project.uri('fragments.graphql'));
252261
expect(serializeRange(definitions[0].range)).toEqual({
253262
start: {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.default = require('../../../../graphiql/test/e2e-server.js');

0 commit comments

Comments
 (0)