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

Support native ESM via .mjs. #433

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
presets: [
[
'env',
{
modules: process.env.ESM ? false : 'commonjs',
targets: {
node: require('./package.json').engines.node.substring(2), // Strip `>=`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until babel/babel#7277 is implemented I think it's better to use explicit string.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Then you would not have a single source of truth.

Copy link
Member

@IvanGoncharov IvanGoncharov May 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this construct assumes >= <version> and it's very brittle approach.
I personally think engines should contain only node version that we run tests against. See my PR for graphql-js: https://github.com/graphql/graphql-js/pull/1338/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2R21

},
},
],
],
plugins: ['transform-class-properties', 'transform-flow-strip-types']
};
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -2,10 +2,8 @@ language: node_js

# https://github.com/nodejs/LTS
node_js:
- "8"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 is still in Active LTS and we should definitely test it + we need to test 10

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v10 is tested as it is the current stable release used via 'node'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use explicit version, especially for consistency with graphql-js and other graphql/* repos.

- "7"
- "node"
- "6"
- "4"

git:
depth: 5
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ Just mount `express-graphql` as a route handler:

```js
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { graphqlHTTP } = require('express-graphql');

const app = express();

@@ -38,7 +38,7 @@ Use `.get` or `.post` (or both) rather than `.use` to configure your route handl

```js
const restify = require('restify');
const graphqlHTTP = require('express-graphql');
const { graphqlHTTP } = require('express-graphql');

const app = restify.createServer();

@@ -162,7 +162,7 @@ This example uses [`express-session`][] to provide GraphQL with the currently lo

```js
const session = require('express-session');
const graphqlHTTP = require('express-graphql');
const { graphqlHTTP } = require('express-graphql');

const app = express();

@@ -207,7 +207,7 @@ This example illustrates adding the amount of time consumed by running the
provided query, which could perhaps be used by your development tools.

```js
const graphqlHTTP = require('express-graphql');
const { graphqlHTTP } = require('express-graphql');

const app = express();

@@ -273,9 +273,9 @@ running a GraphQL request. This function is used internally to handle the
incoming request, you may use it directly for building other similar services.

```js
const graphqlHTTP = require('express-graphql');
const { getGraphQLParams } = require('express-graphql');

graphqlHTTP.getGraphQLParams(request).then(params => {
getGraphQLParams(request).then(params => {
// do something...
})
```
2 changes: 1 addition & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
*/

import express from 'express';
import graphqlHTTP from '../src/';
import { graphqlHTTP } from '../src/';
import { buildSchema } from 'graphql';

// Construct a schema, using GraphQL schema language
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -19,7 +19,11 @@
"middleware",
"api"
],
"main": "dist/index.js",
"engines": {
"node": ">=6"
},
"main": "dist/index",
"module": "dist/index.mjs",
"directories": {
"lib": "./dist"
},
@@ -34,26 +38,25 @@
},
"babel": {
"presets": [
"es2015"
],
"plugins": [
"transform-class-properties",
"transform-flow-strip-types"
"./.babelrc.js"
]
},
"scripts": {
"prepublish": ". ./resources/prepublish.sh",
"test": "npm run lint && npm run check && npm run testonly",
"testonly": "mocha $npm_package_options_mocha",
"lint": "eslint src",
"prettier": "prettier --write 'src/**/*.js'",
"check": "flow check",
"build": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist && npm run build:flow",
"build": "npm run build:clean && npm run build:esm && npm run build:cjs && npm run build:flow",
"build:clean": "rm -rf dist/*",
"build:esm": "ESM=1 babel src -d dist/esm --ignore __tests__ && for file in $(find dist/esm -name '*.js'); do mv \"$file\" `echo \"$file\" | sed 's/dist\\/esm/dist/g; s/.js$/.mjs/g'`; done && rm -rf dist/esm",
"build:cjs": "babel src -d dist --ignore __tests__",
"build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/dist\\//g'`.flow; done",
"watch": "node resources/watch.js",
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
"cover:lcov": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha",
"preversion": "npm test",
"prepublish": ". ./resources/prepublish.sh",
"start": "babel-node examples/index.js"
},
"dependencies": {
@@ -69,7 +72,7 @@
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-flow-strip-types": "6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-env": "^1.6.1",
"babel-register": "^6.26.0",
"babel-runtime": "^6.26.0",
"body-parser": "1.18.2",
2 changes: 1 addition & 1 deletion src/__tests__/http-test.js
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ import {
GraphQLError,
BREAK,
} from 'graphql';
import graphqlHTTP from '../';
import { graphqlHTTP } from '../';

const QueryRootType = new GraphQLObjectType({
name: 'QueryRoot',
2 changes: 1 addition & 1 deletion src/__tests__/usage-test.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import { describe, it } from 'mocha';
import request from 'supertest';
import express from 'express';
import { GraphQLSchema } from 'graphql';
import graphqlHTTP from '../';
import { graphqlHTTP } from '../';

describe('Useful errors when incorrectly used', () => {
it('requires an option factory function', () => {
6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -139,8 +139,7 @@ type Middleware = (request: $Request, response: $Response) => Promise<void>;
* Middleware for express; takes an options object or function as input to
* configure behavior, and returns an express middleware.
*/
module.exports = graphqlHTTP;
function graphqlHTTP(options: Options): Middleware {
export function graphqlHTTP(options: Options): Middleware {
if (!options) {
throw new Error('GraphQL middleware requires options.');
}
@@ -390,8 +389,7 @@ export type GraphQLParams = {
* Provided a "Request" provided by express or connect (typically a node style
* HTTPClientRequest), Promise the GraphQL request parameters.
*/
module.exports.getGraphQLParams = getGraphQLParams;
function getGraphQLParams(request: $Request): Promise<GraphQLParams> {
export function getGraphQLParams(request: $Request): Promise<GraphQLParams> {
return parseBody(request).then(bodyData => {
const urlData = (request.url && url.parse(request.url, true).query) || {};
return parseGraphQLParams(urlData, bodyData);
Loading