@ttoss/graphql-api 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -11
- package/dist/esm/index.js +22 -1
- package/dist/index.d.ts +10 -2
- package/dist/index.js +25 -1
- package/package.json +5 -31
- package/src/buildSchema.ts +43 -0
- package/src/index.ts +1 -0
- package/dist/esm/server.js +0 -46
- package/dist/server.d.ts +0 -10
- package/dist/server.js +0 -90
- package/src/server.ts +0 -60
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ This package provides an opinionated way to create an GraphQL API using ttoss ec
|
|
|
13
13
|
pnpm add @ttoss/graphql-api graphql
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
##
|
|
16
|
+
## Quickstart
|
|
17
17
|
|
|
18
18
|
This library uses [`graphql-compose`](https://graphql-compose.github.io/) to create the GraphQL schema. It re-export all the [`graphql-compose`](https://graphql-compose.github.io/) types and methods, so you can use it directly from this package.
|
|
19
19
|
|
|
@@ -134,6 +134,73 @@ composeWithRelay(UserTC);
|
|
|
134
134
|
|
|
135
135
|
_We inspired ourselves on [graphql-compose-relay](https://graphql-compose.github.io/docs/plugins/plugin-relay.html) to create `composeWithRelay`._
|
|
136
136
|
|
|
137
|
+
### Connections
|
|
138
|
+
|
|
139
|
+
This packages provides the method `composeWithConnection` to create a connection type and queries for a given type, based on [graphql-compose-connection](https://graphql-compose.github.io/docs/plugins/plugin-connection.html) plugin and following the [Relay Connection Specification](https://facebook.github.io/relay/graphql/connections.htm).
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { composeWithConnection } from '@ttoss/appsync-api';
|
|
143
|
+
|
|
144
|
+
composeWithConnection(AuthorTC, {
|
|
145
|
+
findManyResolver: AuthorTC.getResolver('findMany'),
|
|
146
|
+
countResolver: AuthorTC.getResolver('count'),
|
|
147
|
+
sort: {
|
|
148
|
+
ASC: {
|
|
149
|
+
value: {
|
|
150
|
+
scanIndexForward: true,
|
|
151
|
+
},
|
|
152
|
+
cursorFields: ['id'],
|
|
153
|
+
beforeCursorQuery: (rawQuery, cursorData, resolveParams) => {
|
|
154
|
+
if (!rawQuery.id) rawQuery.id = {};
|
|
155
|
+
rawQuery.id.$lt = cursorData.id;
|
|
156
|
+
},
|
|
157
|
+
afterCursorQuery: (rawQuery, cursorData, resolveParams) => {
|
|
158
|
+
if (!rawQuery.id) rawQuery.id = {};
|
|
159
|
+
rawQuery.id.$gt = cursorData.id;
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Middlewares
|
|
167
|
+
|
|
168
|
+
This package provides a way to add middlewares to your final schema. You can add middlewares compatible with [`graphql-middleware`](https://github.com/dimatill/graphql-middleware) by passing them to the `middlewares` option on `buildSchema` method. For example, you can use [GraphQL Shield](https://the-guild.dev/graphql/shield) to add authorization to your API:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { buildSchema } from '@ttoss/graphql-api';
|
|
172
|
+
import { allow, deny, shield } from 'graphql-shield';
|
|
173
|
+
import { schemaComposer } from './schemaComposer';
|
|
174
|
+
|
|
175
|
+
const NotAuthorizedError = new Error('Not authorized!');
|
|
176
|
+
/**
|
|
177
|
+
* The error name is the same value `errorType` on GraphQL errors response.
|
|
178
|
+
*/
|
|
179
|
+
NotAuthorizedError.name = 'NotAuthorizedError';
|
|
180
|
+
|
|
181
|
+
const permissions = shield(
|
|
182
|
+
{
|
|
183
|
+
Query: {
|
|
184
|
+
'*': deny,
|
|
185
|
+
author: allow,
|
|
186
|
+
},
|
|
187
|
+
Author: {
|
|
188
|
+
id: allow,
|
|
189
|
+
name: allow,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
fallbackRule: deny,
|
|
194
|
+
fallbackError: NotAuthorizedError,
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const schema = buildSchema({
|
|
199
|
+
schemaComposer,
|
|
200
|
+
middlewares; [permissions],
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
137
204
|
## Building Schema
|
|
138
205
|
|
|
139
206
|
As Relay needs an introspection query to work, this package provides a way to build the GraphQL schema by running `ttoss-graphl-api build-schema`.
|
|
@@ -142,19 +209,54 @@ As Relay needs an introspection query to work, this package provides a way to bu
|
|
|
142
209
|
ttoss-graphl-api build-schema
|
|
143
210
|
```
|
|
144
211
|
|
|
145
|
-
|
|
212
|
+
You can add the `schema` script to your `package.json`:
|
|
146
213
|
|
|
147
|
-
|
|
214
|
+
```json
|
|
215
|
+
{
|
|
216
|
+
"scripts": {
|
|
217
|
+
"schema": "ttoss-graphl-api build-schema"
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
```
|
|
148
221
|
|
|
149
|
-
|
|
150
|
-
import { createServer } from '@ttoss/graphql-api/server';
|
|
222
|
+
## How to Create Tests
|
|
151
223
|
|
|
152
|
-
|
|
153
|
-
schemaComposer,
|
|
154
|
-
graphiql: true,
|
|
155
|
-
});
|
|
224
|
+
We recommend testing the whole GraphQL API using the `graphql` object and the schema composer to provide the schema. For example:
|
|
156
225
|
|
|
157
|
-
|
|
158
|
-
|
|
226
|
+
```ts
|
|
227
|
+
import { graphql } from 'graphql';
|
|
228
|
+
import { schemaComposer } from './schemaComposer';
|
|
229
|
+
|
|
230
|
+
test('testing my query', () => {
|
|
231
|
+
const author = {
|
|
232
|
+
id: '1',
|
|
233
|
+
name: 'John Doe',
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const response = await graphql({
|
|
237
|
+
schema: schemaComposer.buildSchema(),
|
|
238
|
+
source: /* GraphQL */ `
|
|
239
|
+
query ($id: ID!) {
|
|
240
|
+
node(id: $id) {
|
|
241
|
+
id
|
|
242
|
+
... on Author {
|
|
243
|
+
name
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
`,
|
|
248
|
+
variableValues: {
|
|
249
|
+
id: author.id,
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
expect(response).toEqual({
|
|
254
|
+
data: {
|
|
255
|
+
node: {
|
|
256
|
+
id: author.id,
|
|
257
|
+
name: author.name,
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
});
|
|
159
261
|
});
|
|
160
262
|
```
|
package/dist/esm/index.js
CHANGED
|
@@ -153,4 +153,25 @@ composeWithRelay(schemaComposer.Query);
|
|
|
153
153
|
// src/index.ts
|
|
154
154
|
import { default as default2 } from "graphql-compose-connection";
|
|
155
155
|
export * from "graphql-compose";
|
|
156
|
-
|
|
156
|
+
|
|
157
|
+
// src/buildSchema.ts
|
|
158
|
+
import { applyMiddleware } from "graphql-middleware";
|
|
159
|
+
var buildSchema = ({
|
|
160
|
+
schemaComposer: schemaComposer2,
|
|
161
|
+
middlewares
|
|
162
|
+
}) => {
|
|
163
|
+
if (!schemaComposer2) {
|
|
164
|
+
throw new Error("No schemaComposer provided");
|
|
165
|
+
}
|
|
166
|
+
const schema = schemaComposer2.buildSchema();
|
|
167
|
+
if (middlewares) {
|
|
168
|
+
return applyMiddleware(schema, ...middlewares.map(middleware => {
|
|
169
|
+
if (middleware.generate) {
|
|
170
|
+
return middleware.generate(schema);
|
|
171
|
+
}
|
|
172
|
+
return middleware;
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
return schema;
|
|
176
|
+
};
|
|
177
|
+
export { buildSchema, default2 as composeWithConnection, composeWithRelay, fromGlobalId, toGlobalId };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { ObjectTypeComposer } from 'graphql-compose';
|
|
1
|
+
import { ObjectTypeComposer, SchemaComposer } from 'graphql-compose';
|
|
2
2
|
export * from 'graphql-compose';
|
|
3
3
|
export { default as composeWithConnection } from 'graphql-compose-connection';
|
|
4
|
+
import { GraphQLSchema } from 'graphql';
|
|
5
|
+
import { IMiddleware, IMiddlewareGenerator } from 'graphql-middleware';
|
|
4
6
|
|
|
5
7
|
declare const composeWithRelay: <TContext>(tc: ObjectTypeComposer<any, TContext>) => ObjectTypeComposer<any, TContext>;
|
|
6
8
|
|
|
@@ -19,4 +21,10 @@ declare const toGlobalId: (type: string, recordId: string | number) => string;
|
|
|
19
21
|
*/
|
|
20
22
|
declare const fromGlobalId: (globalId: string) => ResolvedGlobalId;
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
type BuildSchemaInput<TContext = any> = {
|
|
25
|
+
schemaComposer: SchemaComposer<TContext>;
|
|
26
|
+
middlewares?: (IMiddleware | IMiddlewareGenerator<any, TContext, any>)[];
|
|
27
|
+
};
|
|
28
|
+
declare const buildSchema: ({ schemaComposer, middlewares, }: BuildSchemaInput) => GraphQLSchema;
|
|
29
|
+
|
|
30
|
+
export { BuildSchemaInput, buildSchema, composeWithRelay, fromGlobalId, toGlobalId };
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,7 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
|
39
39
|
// src/index.ts
|
|
40
40
|
var src_exports = {};
|
|
41
41
|
__export(src_exports, {
|
|
42
|
+
buildSchema: () => buildSchema,
|
|
42
43
|
composeWithConnection: () => import_graphql_compose_connection.default,
|
|
43
44
|
composeWithRelay: () => composeWithRelay,
|
|
44
45
|
fromGlobalId: () => fromGlobalId,
|
|
@@ -198,10 +199,33 @@ composeWithRelay(import_graphql_compose4.schemaComposer.Query);
|
|
|
198
199
|
// src/index.ts
|
|
199
200
|
var import_graphql_compose_connection = __toESM(require("graphql-compose-connection"));
|
|
200
201
|
__reExport(src_exports, require("graphql-compose"), module.exports);
|
|
202
|
+
|
|
203
|
+
// src/buildSchema.ts
|
|
204
|
+
var import_graphql_middleware = require("graphql-middleware");
|
|
205
|
+
var buildSchema = ({
|
|
206
|
+
schemaComposer: schemaComposer2,
|
|
207
|
+
middlewares
|
|
208
|
+
}) => {
|
|
209
|
+
if (!schemaComposer2) {
|
|
210
|
+
throw new Error("No schemaComposer provided");
|
|
211
|
+
}
|
|
212
|
+
const schema = schemaComposer2.buildSchema();
|
|
213
|
+
if (middlewares) {
|
|
214
|
+
return (0, import_graphql_middleware.applyMiddleware)(schema, ...middlewares.map(middleware => {
|
|
215
|
+
if (middleware.generate) {
|
|
216
|
+
return middleware.generate(schema);
|
|
217
|
+
}
|
|
218
|
+
return middleware;
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
return schema;
|
|
222
|
+
};
|
|
201
223
|
// Annotate the CommonJS export names for ESM import in node:
|
|
202
224
|
0 && (module.exports = {
|
|
225
|
+
buildSchema,
|
|
203
226
|
composeWithConnection,
|
|
204
227
|
composeWithRelay,
|
|
205
228
|
fromGlobalId,
|
|
206
|
-
toGlobalId
|
|
229
|
+
toGlobalId,
|
|
230
|
+
...require("graphql-compose")
|
|
207
231
|
});
|
package/package.json
CHANGED
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/graphql-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A library for building GraphQL APIs.",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Pedro Arantes <pedro@arantespp.com> (https://arantespp.com)"
|
|
8
8
|
],
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"import": "./dist/esm/index.js",
|
|
12
|
-
"require": "./dist/index.js"
|
|
13
|
-
},
|
|
14
|
-
"./server": {
|
|
15
|
-
"import": "./dist/esm/server.js",
|
|
16
|
-
"require": "./dist/server.js"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
9
|
"main": "dist/index.js",
|
|
20
10
|
"module": "dist/esm/index.js",
|
|
21
11
|
"bin": {
|
|
@@ -28,26 +18,20 @@
|
|
|
28
18
|
"sideEffects": false,
|
|
29
19
|
"typings": "dist/index.d.ts",
|
|
30
20
|
"dependencies": {
|
|
31
|
-
"
|
|
21
|
+
"graphql-compose": "^9.0.10",
|
|
32
22
|
"graphql-compose-connection": "^8.2.1",
|
|
33
|
-
"graphql-
|
|
34
|
-
"koa": "^2.14.2",
|
|
35
|
-
"koa-bodyparser": "^4.4.0",
|
|
23
|
+
"graphql-middleware": "^6.1.34",
|
|
36
24
|
"npmlog": "^7.0.1",
|
|
37
25
|
"ts-node": "^10.9.1",
|
|
38
26
|
"yargs": "^17.7.2"
|
|
39
27
|
},
|
|
40
28
|
"peerDependencies": {
|
|
41
|
-
"graphql": "^16.6.0"
|
|
42
|
-
"graphql-compose": "^9.0.10"
|
|
29
|
+
"graphql": "^16.6.0"
|
|
43
30
|
},
|
|
44
31
|
"devDependencies": {
|
|
45
|
-
"@types/koa": "^2.13.6",
|
|
46
|
-
"@types/koa__router": "^12.0.0",
|
|
47
|
-
"@types/koa-bodyparser": "^4.3.10",
|
|
48
32
|
"@types/yargs": "^17.0.24",
|
|
49
33
|
"graphql": "^16.6.0",
|
|
50
|
-
"graphql-
|
|
34
|
+
"graphql-shield": "^7.6.5",
|
|
51
35
|
"jest": "^29.5.0",
|
|
52
36
|
"tsup": "^6.7.0",
|
|
53
37
|
"@ttoss/config": "^1.30.0"
|
|
@@ -59,16 +43,6 @@
|
|
|
59
43
|
"publishConfig": {
|
|
60
44
|
"access": "public"
|
|
61
45
|
},
|
|
62
|
-
"typesVersions": {
|
|
63
|
-
"*": {
|
|
64
|
-
".": [
|
|
65
|
-
"./dist/index.d.ts"
|
|
66
|
-
],
|
|
67
|
-
"server": [
|
|
68
|
-
"./dist/server.d.ts"
|
|
69
|
-
]
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
46
|
"scripts": {
|
|
73
47
|
"build": "tsup",
|
|
74
48
|
"test": "jest"
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type GraphQLSchema } from 'graphql';
|
|
2
|
+
import {
|
|
3
|
+
type IMiddleware,
|
|
4
|
+
type IMiddlewareGenerator,
|
|
5
|
+
applyMiddleware,
|
|
6
|
+
} from 'graphql-middleware';
|
|
7
|
+
import { type SchemaComposer } from 'graphql-compose';
|
|
8
|
+
|
|
9
|
+
export type BuildSchemaInput<TContext = any> = {
|
|
10
|
+
schemaComposer: SchemaComposer<TContext>;
|
|
11
|
+
middlewares?: (IMiddleware | IMiddlewareGenerator<any, TContext, any>)[];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const buildSchema = ({
|
|
15
|
+
schemaComposer,
|
|
16
|
+
middlewares,
|
|
17
|
+
}: BuildSchemaInput): GraphQLSchema => {
|
|
18
|
+
if (!schemaComposer) {
|
|
19
|
+
throw new Error('No schemaComposer provided');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const schema = schemaComposer.buildSchema();
|
|
23
|
+
|
|
24
|
+
if (middlewares) {
|
|
25
|
+
return applyMiddleware(
|
|
26
|
+
schema,
|
|
27
|
+
...middlewares.map((middleware) => {
|
|
28
|
+
/**
|
|
29
|
+
* https://github.com/dimatill/graphql-middleware/issues/433#issuecomment-1170187160
|
|
30
|
+
*/
|
|
31
|
+
if ((middleware as IMiddlewareGenerator<any, any, any>).generate) {
|
|
32
|
+
return (middleware as IMiddlewareGenerator<any, any, any>).generate(
|
|
33
|
+
schema
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return middleware;
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return schema;
|
|
43
|
+
};
|
package/src/index.ts
CHANGED
package/dist/esm/server.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
import "./chunk-SAO2ACBY.js";
|
|
3
|
-
|
|
4
|
-
// src/server.ts
|
|
5
|
-
import { getGraphQLParameters, processRequest, renderGraphiQL, sendResult, shouldRenderGraphiQL } from "graphql-helix";
|
|
6
|
-
import Koa from "koa";
|
|
7
|
-
import Router from "@koa/router";
|
|
8
|
-
import bodyParser from "koa-bodyparser";
|
|
9
|
-
var createServer = ({
|
|
10
|
-
schemaComposer,
|
|
11
|
-
graphiql = false
|
|
12
|
-
}) => {
|
|
13
|
-
const server = new Koa();
|
|
14
|
-
const router = new Router();
|
|
15
|
-
server.use(bodyParser());
|
|
16
|
-
router.all("/graphql", async ctx => {
|
|
17
|
-
const request = {
|
|
18
|
-
body: ctx.request.body,
|
|
19
|
-
headers: ctx.headers,
|
|
20
|
-
method: ctx.method,
|
|
21
|
-
query: ctx.request.query
|
|
22
|
-
};
|
|
23
|
-
if (shouldRenderGraphiQL(request)) {
|
|
24
|
-
if (graphiql) {
|
|
25
|
-
ctx.body = renderGraphiQL({});
|
|
26
|
-
}
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const {
|
|
30
|
-
operationName,
|
|
31
|
-
query,
|
|
32
|
-
variables
|
|
33
|
-
} = getGraphQLParameters(request);
|
|
34
|
-
const result = await processRequest({
|
|
35
|
-
operationName,
|
|
36
|
-
query,
|
|
37
|
-
variables,
|
|
38
|
-
request,
|
|
39
|
-
schema: schemaComposer.buildSchema()
|
|
40
|
-
});
|
|
41
|
-
sendResult(result, ctx.res);
|
|
42
|
-
});
|
|
43
|
-
server.use(router.routes()).use(router.allowedMethods());
|
|
44
|
-
return server;
|
|
45
|
-
};
|
|
46
|
-
export { Router, createServer };
|
package/dist/server.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { SchemaComposer } from 'graphql-compose';
|
|
2
|
-
import Koa from 'koa';
|
|
3
|
-
export { default as Router } from '@koa/router';
|
|
4
|
-
|
|
5
|
-
declare const createServer: ({ schemaComposer, graphiql, }: {
|
|
6
|
-
schemaComposer: SchemaComposer<any>;
|
|
7
|
-
graphiql?: boolean | undefined;
|
|
8
|
-
}) => Koa;
|
|
9
|
-
|
|
10
|
-
export { createServer };
|
package/dist/server.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
var __create = Object.create;
|
|
5
|
-
var __defProp = Object.defineProperty;
|
|
6
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __export = (target, all) => {
|
|
11
|
-
for (var name in all) __defProp(target, name, {
|
|
12
|
-
get: all[name],
|
|
13
|
-
enumerable: true
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
var __copyProps = (to, from, except, desc) => {
|
|
17
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
-
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
19
|
-
get: () => from[key],
|
|
20
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return to;
|
|
24
|
-
};
|
|
25
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
31
|
-
value: mod,
|
|
32
|
-
enumerable: true
|
|
33
|
-
}) : target, mod));
|
|
34
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
35
|
-
value: true
|
|
36
|
-
}), mod);
|
|
37
|
-
|
|
38
|
-
// src/server.ts
|
|
39
|
-
var server_exports = {};
|
|
40
|
-
__export(server_exports, {
|
|
41
|
-
Router: () => import_router.default,
|
|
42
|
-
createServer: () => createServer
|
|
43
|
-
});
|
|
44
|
-
module.exports = __toCommonJS(server_exports);
|
|
45
|
-
var import_graphql_helix = require("graphql-helix");
|
|
46
|
-
var import_koa = __toESM(require("koa"));
|
|
47
|
-
var import_router = __toESM(require("@koa/router"));
|
|
48
|
-
var import_koa_bodyparser = __toESM(require("koa-bodyparser"));
|
|
49
|
-
var createServer = ({
|
|
50
|
-
schemaComposer,
|
|
51
|
-
graphiql = false
|
|
52
|
-
}) => {
|
|
53
|
-
const server = new import_koa.default();
|
|
54
|
-
const router = new import_router.default();
|
|
55
|
-
server.use((0, import_koa_bodyparser.default)());
|
|
56
|
-
router.all("/graphql", async ctx => {
|
|
57
|
-
const request = {
|
|
58
|
-
body: ctx.request.body,
|
|
59
|
-
headers: ctx.headers,
|
|
60
|
-
method: ctx.method,
|
|
61
|
-
query: ctx.request.query
|
|
62
|
-
};
|
|
63
|
-
if ((0, import_graphql_helix.shouldRenderGraphiQL)(request)) {
|
|
64
|
-
if (graphiql) {
|
|
65
|
-
ctx.body = (0, import_graphql_helix.renderGraphiQL)({});
|
|
66
|
-
}
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const {
|
|
70
|
-
operationName,
|
|
71
|
-
query,
|
|
72
|
-
variables
|
|
73
|
-
} = (0, import_graphql_helix.getGraphQLParameters)(request);
|
|
74
|
-
const result = await (0, import_graphql_helix.processRequest)({
|
|
75
|
-
operationName,
|
|
76
|
-
query,
|
|
77
|
-
variables,
|
|
78
|
-
request,
|
|
79
|
-
schema: schemaComposer.buildSchema()
|
|
80
|
-
});
|
|
81
|
-
(0, import_graphql_helix.sendResult)(result, ctx.res);
|
|
82
|
-
});
|
|
83
|
-
server.use(router.routes()).use(router.allowedMethods());
|
|
84
|
-
return server;
|
|
85
|
-
};
|
|
86
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
-
0 && (module.exports = {
|
|
88
|
-
Router,
|
|
89
|
-
createServer
|
|
90
|
-
});
|
package/src/server.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { type SchemaComposer } from 'graphql-compose';
|
|
2
|
-
import {
|
|
3
|
-
getGraphQLParameters,
|
|
4
|
-
processRequest,
|
|
5
|
-
renderGraphiQL,
|
|
6
|
-
sendResult,
|
|
7
|
-
shouldRenderGraphiQL,
|
|
8
|
-
} from 'graphql-helix';
|
|
9
|
-
import Koa from 'koa';
|
|
10
|
-
import Router from '@koa/router';
|
|
11
|
-
import bodyParser from 'koa-bodyparser';
|
|
12
|
-
|
|
13
|
-
export { Router };
|
|
14
|
-
|
|
15
|
-
export const createServer = ({
|
|
16
|
-
schemaComposer,
|
|
17
|
-
graphiql = false,
|
|
18
|
-
}: {
|
|
19
|
-
schemaComposer: SchemaComposer<any>;
|
|
20
|
-
graphiql?: boolean;
|
|
21
|
-
}): Koa => {
|
|
22
|
-
const server = new Koa();
|
|
23
|
-
|
|
24
|
-
const router = new Router();
|
|
25
|
-
|
|
26
|
-
server.use(bodyParser());
|
|
27
|
-
|
|
28
|
-
router.all('/graphql', async (ctx) => {
|
|
29
|
-
const request = {
|
|
30
|
-
body: ctx.request.body,
|
|
31
|
-
headers: ctx.headers,
|
|
32
|
-
method: ctx.method,
|
|
33
|
-
query: ctx.request.query,
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
if (shouldRenderGraphiQL(request)) {
|
|
37
|
-
if (graphiql) {
|
|
38
|
-
ctx.body = renderGraphiQL({});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const { operationName, query, variables } = getGraphQLParameters(request);
|
|
45
|
-
|
|
46
|
-
const result = await processRequest({
|
|
47
|
-
operationName,
|
|
48
|
-
query,
|
|
49
|
-
variables,
|
|
50
|
-
request,
|
|
51
|
-
schema: schemaComposer.buildSchema(),
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
sendResult(result, ctx.res);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
server.use(router.routes()).use(router.allowedMethods());
|
|
58
|
-
|
|
59
|
-
return server;
|
|
60
|
-
};
|