Skip to content

Commit eb6f53b

Browse files
PabloSzxdotansimha
andauthored
ESM support for core and all plugins (#259)
Co-authored-by: Dotan Simha <[email protected]>
1 parent a5bee94 commit eb6f53b

File tree

29 files changed

+432
-70
lines changed

29 files changed

+432
-70
lines changed

.changeset/two-kiwis-accept.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
'@envelop/core': minor
3+
'@envelop/apollo-tracing': minor
4+
'@envelop/auth0': minor
5+
'@envelop/dataloader': minor
6+
'@envelop/depth-limit': minor
7+
'@envelop/disable-introspection': minor
8+
'@envelop/extended-validation': minor
9+
'@envelop/filter-operation-type': minor
10+
'@envelop/generic-auth': minor
11+
'@envelop/graphql-jit': minor
12+
'@envelop/graphql-middleware': minor
13+
'@envelop/graphql-modules': minor
14+
'@envelop/opentelemetry': minor
15+
'@envelop/parser-cache': minor
16+
'@envelop/persisted-operations': minor
17+
'@envelop/preload-assets': minor
18+
'@envelop/rate-limiter': minor
19+
'@envelop/sentry': minor
20+
'@envelop/validation-cache': minor
21+
'@envelop/testing': minor
22+
'@envelop/types': minor
23+
---
24+
25+
ESM Support for all plugins and envelop core

benchmark/k6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const options = {
1818
thresholds: {
1919
no_errors: ['rate=1.0'],
2020
expected_result: ['rate=1.0'],
21-
http_req_duration: ['p(95)<=12'],
21+
http_req_duration: ['p(95)<=15'],
2222
graphql_execute: ['p(95)<=1'],
2323
graphql_context: ['p(95)<=1'],
2424
graphql_validate: ['p(95)<=1'],

examples/with-esm/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Envelop with ESM
2+
3+
If you are using ESM in your project, you can consume Envelop's core and plugins using ESM.
4+
5+
All bundles produced from this repo has `mjs` in addition to CommonJS.
6+
7+
### Running this example
8+
9+
To run this example:
10+
11+
1. Make sure to install deps on the root of the repo.
12+
2. Run `yarn build`.
13+
3. Run `yarn dev` in this directory.
14+

examples/with-esm/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@envelop-examples/with-esm",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"dev": "tsup-node src/index.ts --format esm --watch src --onSuccess \"node dist/index.mjs\"",
6+
"start": "tsup-node src/index.ts --format esm --onSuccess \"node dist/index.mjs\""
7+
},
8+
"dependencies": {
9+
"@envelop/core": "*",
10+
"@graphql-tools/schema": "7.1.5",
11+
"fastify": "3.14.0",
12+
"graphql-helix": "1.6.1"
13+
},
14+
"devDependencies": {
15+
"@types/node": "15.12.2",
16+
"tsup": "4.11.2",
17+
"typescript": "4.3.2"
18+
}
19+
}

examples/with-esm/src/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* eslint-disable no-console */
2+
import fastify from 'fastify';
3+
import { getGraphQLParameters, processRequest, renderGraphiQL, shouldRenderGraphiQL } from 'graphql-helix';
4+
import { envelop, useLogger, useSchema, useTiming } from '@envelop/core';
5+
import { makeExecutableSchema } from '@graphql-tools/schema';
6+
7+
const schema = makeExecutableSchema({
8+
typeDefs: /* GraphQL */ `
9+
type Query {
10+
hello: String!
11+
}
12+
`,
13+
resolvers: {
14+
Query: {
15+
hello: () => 'World',
16+
},
17+
},
18+
});
19+
20+
const getEnveloped = envelop({
21+
plugins: [useSchema(schema), useLogger(), useTiming()],
22+
});
23+
const app = fastify();
24+
25+
app.route({
26+
method: ['GET', 'POST'],
27+
url: '/graphql',
28+
async handler(req, res) {
29+
const { parse, validate, contextFactory, execute, schema } = getEnveloped({ req });
30+
const request = {
31+
body: req.body,
32+
headers: req.headers,
33+
method: req.method,
34+
query: req.query,
35+
};
36+
37+
if (shouldRenderGraphiQL(request)) {
38+
res.type('text/html');
39+
res.send(renderGraphiQL({}));
40+
} else {
41+
const { operationName, query, variables } = getGraphQLParameters(request);
42+
const result = await processRequest({
43+
operationName,
44+
query,
45+
variables,
46+
request,
47+
schema,
48+
parse,
49+
validate,
50+
execute,
51+
contextFactory,
52+
});
53+
54+
if (result.type === 'RESPONSE') {
55+
res.status(result.status);
56+
res.send(result.payload);
57+
} else {
58+
// You can find a complete example with GraphQL Subscriptions and stream/defer here:
59+
// https://github.com/contrawork/graphql-helix/blob/master/examples/fastify/server.ts
60+
res.send({ errors: [{ message: 'Not Supported in this demo' }] });
61+
}
62+
}
63+
},
64+
});
65+
66+
app.listen(3000, () => {
67+
console.log(`GraphQL server is running in http://127.0.0.1:3000/graphql.`);
68+
});

examples/with-esm/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2019",
4+
"module": "esnext",
5+
"strict": true,
6+
"noUnusedLocals": true,
7+
"noUnusedParameters": true,
8+
"noImplicitReturns": true,
9+
"noFallthroughCasesInSwitch": true,
10+
"noUncheckedIndexedAccess": true,
11+
"noImplicitOverride": true,
12+
"noPropertyAccessFromIndexSignature": true,
13+
"noEmit": true,
14+
"moduleResolution": "node",
15+
"esModuleInterop": true,
16+
"skipLibCheck": true,
17+
"forceConsistentCasingInFileNames": true
18+
}
19+
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
"./examples/lambda-aws",
1414
"./examples/azure-functions",
1515
"./examples/google-cloud-functions",
16-
"./examples/cloudflare-workers"
16+
"./examples/cloudflare-workers",
17+
"./examples/with-esm"
1718
],
1819
"scripts": {
1920
"prepare": "husky install",
2021
"benchmark": "NODE_ENV=production ts-node --project tsconfig.benchmark.json benchmark/execute.case.ts",
2122
"postinstall": "patch-package",
2223
"lint": "eslint --config .eslintrc.json --ext .ts .",
23-
"prebuild": "rimraf packages/*/dist",
24+
"prebuild": "rimraf packages/*/dist packages/plugins/*/dist",
2425
"build": "tsc --project tsconfig.json && bob build",
2526
"test": "jest",
2627
"test:ci": "jest --coverage",
@@ -48,7 +49,7 @@
4849
"husky": "6.0.0",
4950
"lint-staged": "11.0.0",
5051
"@types/node": "15.6.1",
51-
"bob-the-bundler": "1.2.1",
52+
"bob-the-bundler": "1.4.1",
5253
"eslint-config-standard": "16.0.3",
5354
"eslint-plugin-import": "2.23.4",
5455
"eslint-plugin-node": "11.1.0",

packages/core/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@
1818
"nodejs",
1919
"typescript"
2020
],
21-
"main": "dist/index.cjs.js",
22-
"module": "dist/index.esm.js",
21+
"main": "dist/index.js",
22+
"module": "dist/index.mjs",
23+
"exports": {
24+
".": {
25+
"require": "./dist/index.js",
26+
"import": "./dist/index.mjs"
27+
},
28+
"./*": {
29+
"require": "./dist/*.js",
30+
"import": "./dist/*.mjs"
31+
}
32+
},
2333
"typings": "dist/index.d.ts",
2434
"typescript": {
2535
"definition": "dist/index.d.ts"
@@ -34,7 +44,7 @@
3444
"devDependencies": {
3545
"@graphql-tools/utils": "7.10.0",
3646
"@graphql-tools/schema": "7.1.5",
37-
"bob-the-bundler": "1.2.1",
47+
"bob-the-bundler": "1.4.1",
3848
"graphql": "15.5.0",
3949
"typescript": "4.3.2"
4050
},

packages/plugins/apollo-tracing/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@
99
"directory": "packages/plugins/apollo-tracing"
1010
},
1111
"sideEffects": false,
12-
"main": "dist/index.cjs.js",
13-
"module": "dist/index.esm.js",
12+
"main": "dist/index.js",
13+
"module": "dist/index.mjs",
14+
"exports": {
15+
".": {
16+
"require": "./dist/index.js",
17+
"import": "./dist/index.mjs"
18+
},
19+
"./*": {
20+
"require": "./dist/*.js",
21+
"import": "./dist/*.mjs"
22+
}
23+
},
1424
"typings": "dist/index.d.ts",
1525
"typescript": {
1626
"definition": "dist/index.d.ts"
@@ -24,7 +34,7 @@
2434
},
2535
"devDependencies": {
2636
"@graphql-tools/schema": "7.1.5",
27-
"bob-the-bundler": "1.2.1",
37+
"bob-the-bundler": "1.4.1",
2838
"graphql": "15.5.0",
2939
"typescript": "4.3.2"
3040
},

packages/plugins/auth0/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@
99
"url": "https://github.com/dotansimha/envelop.git",
1010
"directory": "packages/plugins/auth0"
1111
},
12-
"main": "dist/index.cjs.js",
13-
"module": "dist/index.esm.js",
12+
"main": "dist/index.js",
13+
"module": "dist/index.mjs",
14+
"exports": {
15+
".": {
16+
"require": "./dist/index.js",
17+
"import": "./dist/index.mjs"
18+
},
19+
"./*": {
20+
"require": "./dist/*.js",
21+
"import": "./dist/*.mjs"
22+
}
23+
},
1424
"typings": "dist/index.d.ts",
1525
"typescript": {
1626
"definition": "dist/index.d.ts"
@@ -25,7 +35,7 @@
2535
},
2636
"devDependencies": {
2737
"@types/jsonwebtoken": "8.5.1",
28-
"bob-the-bundler": "1.2.1",
38+
"bob-the-bundler": "1.4.1",
2939
"graphql": "15.5.0",
3040
"typescript": "4.3.2"
3141
},

0 commit comments

Comments
 (0)