Skip to content

Commit

Permalink
[v4] convert graphiql e2e server to ESM (#3697)
Browse files Browse the repository at this point in the history
* aa

* fix netlify

* fix

* fix fails on ci
  • Loading branch information
dimaMachina authored Aug 8, 2024
1 parent 2005871 commit bf3ea52
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 66 deletions.
2 changes: 1 addition & 1 deletion functions/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
HandlerContext as NetlifyHandlerContext,
} from '@netlify/functions';

import schema from '../packages/graphiql/test/schema';
import { schema } from '../packages/graphiql/test/schema.js';

/**
* Handler options when using the netlify adapter
Expand Down
14 changes: 0 additions & 14 deletions packages/graphiql/test/afterDevServer.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/graphiql/test/bad-schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports.schema = {
export const badSchema = {
__schema: {
queryType: {
name: 'Query',
Expand Down
30 changes: 0 additions & 30 deletions packages/graphiql/test/beforeDevServer.js

This file was deleted.

37 changes: 25 additions & 12 deletions packages/graphiql/test/e2e-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable no-console */
const { createServer } = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const express = require('express');
const { createHandler } = require('graphql-http/lib/use/express');
const { GraphQLError } = require('graphql');
const schema = require('./schema');
/* eslint-disable no-console, import-x/no-extraneous-dependencies */
import { createServer } from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import express from 'express';
import { GraphQLError } from 'graphql';
import { createHandler } from 'graphql-http/lib/use/express';
import { useServer } from 'graphql-ws/lib/use/ws';
import { WebSocketServer } from 'ws';

import { schema } from './schema.js';
import { badSchema } from './bad-schema.js';

const app = express();
const { schema: badSchema } = require('./bad-schema');
const WebSocketsServer = require('./afterDevServer');

// Server
app.post('/graphql', createHandler({ schema }));
Expand All @@ -38,6 +42,9 @@ app.post('/graphql-error/graphql', (_req, res, next) => {

// On CI we test the UMD build
if (process.env.CI === 'true') {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// const __dirname = import.meta.dirname; // can be converted to, after Node.js upgrade to v20

const indexHtml = fs.readFileSync(
path.join(__dirname, '..', 'index.html'),
'utf8',
Expand Down Expand Up @@ -81,7 +88,7 @@ const server = createServer(app);
server.listen(process.env.PORT || 3100, function () {
const { port } = this.address();

console.log(`Started on http://localhost:${port}/`);
console.log(`Started on http://localhost:${port}`);
console.log('PID', process.pid);

process.once('SIGINT', () => {
Expand All @@ -92,4 +99,10 @@ server.listen(process.env.PORT || 3100, function () {
});
});

WebSocketsServer();
const wsServer = new WebSocketServer({
path: '/subscriptions',
port: 8081,
});

// eslint-disable-next-line react-hooks/rules-of-hooks
useServer({ schema }, wsServer);
3 changes: 3 additions & 0 deletions packages/graphiql/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
9 changes: 4 additions & 5 deletions packages/graphiql/test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* LICENSE file in the root directory of this source tree.
*/

const {
// eslint-disable-next-line import-x/no-extraneous-dependencies
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLUnionType,
Expand All @@ -19,7 +20,7 @@ const {
GraphQLString,
GraphQLID,
GraphQLList,
} = require('graphql');
} from 'graphql';

// Test Schema
const TestEnum = new GraphQLEnumType({
Expand Down Expand Up @@ -381,11 +382,9 @@ const TestSubscriptionType = new GraphQLObjectType({
},
});

const myTestSchema = new GraphQLSchema({
export const schema = new GraphQLSchema({
query: TestType,
mutation: TestMutationType,
subscription: TestSubscriptionType,
description: 'This is a test schema for GraphiQL',
});

module.exports = myTestSchema;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import mockfs from 'mock-fs';
import { join } from 'node:path';
import { MockFile, MockProject } from './__utils__/MockProject';
// import { readFileSync } from 'node:fs';
import { FileChangeType } from 'vscode-languageserver';
import { serializeRange } from './__utils__/utils';
import { readFile } from 'node:fs/promises';
Expand All @@ -14,6 +13,7 @@ import {
parse,
} from 'graphql';
import fetchMock from 'fetch-mock';
import { schema as graphiqlSchema } from '../../../graphiql/test/schema';

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

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

it('caches files and schema with a URL config', async () => {
mockSchema(require('../../../graphiql/test/schema'));
mockSchema(graphiqlSchema);

const project = new MockProject({
files: [
Expand Down Expand Up @@ -495,7 +495,7 @@ describe('MessageProcessor with config', () => {
});

it('caches multiple projects with files and schema with a URL config and a local schema', async () => {
mockSchema(require('../../../graphiql/test/schema'));
mockSchema(graphiqlSchema);

const project = new MockProject({
files: [
Expand Down

0 comments on commit bf3ea52

Please sign in to comment.