Skip to content

Commit 4757465

Browse files
PabloSzxdotansimha
authored andcommitted
add with-esm example
1 parent 118a757 commit 4757465

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

examples/with-esm/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
"dev:cjs": "tsup-node src/index.ts --format cjs --watch src --onSuccess \"node dist/index.js\"",
7+
"start": "tsup-node src/index.ts --format esm --onSuccess \"node dist/index.mjs\"",
8+
"start:cjs": "tsup-node src/index.ts --format cjs --onSuccess \"node dist/index.js\""
9+
},
10+
"dependencies": {
11+
"@envelop/core": "*",
12+
"@graphql-tools/schema": "7.1.5",
13+
"fastify": "3.14.0",
14+
"graphql-helix": "1.6.1"
15+
},
16+
"devDependencies": {
17+
"@types/node": "15.12.2",
18+
"tsup": "4.11.2",
19+
"typescript": "4.3.2"
20+
}
21+
}

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();
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
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",

0 commit comments

Comments
 (0)