Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Sync up config files with 'graphql-js'
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored and danielrearden committed Jul 5, 2020
1 parent 4e18770 commit 14b7164
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 354 deletions.
3 changes: 1 addition & 2 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
throw-deprecation: true
check-leaks: true
require:
- '@babel/register'
- ts-node/register
- './resources/register.js'
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"serializable",
"subcommand",
"charsets",
"downlevel"

// TODO: remove bellow words
"Graphi", // GraphiQL
Expand Down
8 changes: 5 additions & 3 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import express from 'express';
import { buildSchema } from 'graphql';
/* eslint-disable import/unambiguous, node/no-unpublished-require */

import { graphqlHTTP } from '../src';
const express = require('express');
const { buildSchema } = require('graphql');

const { graphqlHTTP } = require('../src');

// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
Expand Down
28 changes: 28 additions & 0 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import express from 'express';
import { buildSchema } from 'graphql';

import { graphqlHTTP } from '../src';

// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
type Query {
hello: String
}
`);

// The root provides a resolver function for each API endpoint
const rootValue = {
hello: () => 'Hello world!',
};

const app = express();
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue,
graphiql: true,
}),
);
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
57 changes: 57 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"private": true,
"main": "index.js",
"types": "index.d.ts",
"typesVersions": {
"<3.8": {
"*": [
"ts3.4/*"
]
}
},
"sideEffects": false,
"homepage": "https://github.com/graphql/express-graphql",
"bugs": {
Expand Down Expand Up @@ -40,7 +47,7 @@
"check:spelling": "cspell '**/*'",
"check:integrations": "mocha --full-trace integrationTests/*-test.js",
"build": "node resources/build.js",
"start": "node -r @babel/register examples/index.js"
"start": "ts-node -r ./resources/register.js examples/index.js"
},
"dependencies": {
"accepts": "^1.3.7",
Expand All @@ -54,10 +61,13 @@
"@babel/plugin-transform-flow-strip-types": "7.10.4",
"@babel/preset-env": "7.10.4",
"@babel/register": "7.10.4",
"@types/accepts": "1.3.5",
"@types/body-parser": "1.19.0",
"@types/chai": "4.2.11",
"@types/connect": "3.4.33",
"@types/content-type": "1.1.3",
"@types/express": "4.17.6",
"@types/http-errors": "1.6.3",
"@types/mocha": "7.0.2",
"@types/multer": "1.4.3",
"@types/node": "14.0.14",
Expand All @@ -70,6 +80,7 @@
"body-parser": "1.19.0",
"chai": "4.2.0",
"connect": "3.7.0",
"downlevel-dts": "0.5.0",
"eslint": "7.3.1",
"eslint-plugin-flowtype": "5.2.0",
"eslint-plugin-import": "2.22.0",
Expand Down
44 changes: 24 additions & 20 deletions resources/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,37 @@ const fs = require('fs');
const path = require('path');
const assert = require('assert');

const babel = require('@babel/core');
const ts = require('typescript');

const { rmdirRecursive, readdirRecursive, showStats } = require('./utils');
const tsConfig = require('../tsconfig.json');

const {
transformLoadFileStaticallyFromNPM,
} = require('./load-statically-from-npm');
const {
exec,
rmdirRecursive,
readdirRecursive,
showStats,
} = require('./utils');

if (require.main === module) {
rmdirRecursive('./dist');
fs.mkdirSync('./dist');

const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
for (const filepath of srcFiles) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./dist', filepath);

fs.mkdirSync(path.dirname(destPath), { recursive: true });
if (filepath.endsWith('.js')) {
fs.copyFileSync(srcPath, destPath + '.flow');

const cjs = babelBuild(srcPath, { envName: 'cjs' });
fs.writeFileSync(destPath, cjs);
} else if (filepath.endsWith('d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
}
const { options } = ts.convertCompilerOptionsFromJson(
tsConfig.compilerOptions,
process.cwd(),
);
const program = ts.createProgram({
rootNames: srcFiles.map((filepath) => path.join('./src', filepath)),
options,
});
program.emit(undefined, undefined, undefined, undefined, {
after: [transformLoadFileStaticallyFromNPM],
});
exec('npx downlevel-dts ./dist ./dist/ts3.4');

fs.copyFileSync('./LICENSE', './dist/LICENSE');
fs.copyFileSync('./README.md', './dist/README.md');
Expand All @@ -40,10 +48,6 @@ if (require.main === module) {
showStats();
}

function babelBuild(srcPath, options) {
return babel.transformFileSync(srcPath, options).code + '\n';
}

function buildPackageJSON() {
const packageJSON = require('../package.json');
delete packageJSON.private;
Expand Down
36 changes: 16 additions & 20 deletions resources/load-statically-from-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

const fs = require('fs');

const ts = require('typescript');

/**
* Eliminates function call to `invariant` if the condition is met.
*
* Transforms:
*
* loadFileStaticallyFromNPM(<npm path>)
Expand All @@ -15,23 +15,19 @@ const fs = require('fs');
*
* "<file content>"
*/
module.exports = function inlineInvariant(context) {
return {
visitor: {
CallExpression(path) {
const { node } = path;

if (
node.callee.type === 'Identifier' &&
node.callee.name === 'loadFileStaticallyFromNPM'
) {
const npmPath = node.arguments[0].value;
const filePath = require.resolve(npmPath);
const content = fs.readFileSync(filePath, 'utf-8');

path.replaceWith(context.types.stringLiteral(content));
}
},
},
module.exports.transformLoadFileStaticallyFromNPM = function (context) {
return function visit(node) {
if (ts.isCallExpression(node)) {
if (
ts.isIdentifier(node.expression) &&
node.expression.text === 'loadFileStaticallyFromNPM'
) {
const npmPath = node.arguments[0].text;
const filePath = require.resolve(npmPath);
const content = fs.readFileSync(filePath, 'utf-8');
return ts.createStringLiteral(content);
}
}
return ts.visitEachChild(node, visit, context);
};
};
11 changes: 11 additions & 0 deletions resources/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const {
transformLoadFileStaticallyFromNPM,
} = require('./load-statically-from-npm');

require('ts-node').register({
transformers: () => ({
after: [transformLoadFileStaticallyFromNPM],
}),
});
Loading

0 comments on commit 14b7164

Please sign in to comment.