@ttoss/graphql-api 0.1.0 → 0.2.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 +66 -12
- package/dist/index.js +2 -1
- package/package.json +3 -31
- 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,35 @@ 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
|
+
|
|
137
166
|
## Building Schema
|
|
138
167
|
|
|
139
168
|
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 +171,44 @@ As Relay needs an introspection query to work, this package provides a way to bu
|
|
|
142
171
|
ttoss-graphl-api build-schema
|
|
143
172
|
```
|
|
144
173
|
|
|
145
|
-
##
|
|
174
|
+
## How to Create Tests
|
|
146
175
|
|
|
147
|
-
|
|
176
|
+
We recommend testing the whole GraphQL API using the `graphql` object and the schema composer to provide the schema. For example:
|
|
148
177
|
|
|
149
178
|
```ts
|
|
150
|
-
import {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
179
|
+
import { graphql } from 'graphql';
|
|
180
|
+
import { schemaComposer } from './schemaComposer';
|
|
181
|
+
|
|
182
|
+
test('testing my query', () => {
|
|
183
|
+
const author = {
|
|
184
|
+
id: '1',
|
|
185
|
+
name: 'John Doe',
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const response = await graphql({
|
|
189
|
+
schema: schemaComposer.buildSchema(),
|
|
190
|
+
source: /* GraphQL */ `
|
|
191
|
+
query ($id: ID!) {
|
|
192
|
+
node(id: $id) {
|
|
193
|
+
id
|
|
194
|
+
... on Author {
|
|
195
|
+
name
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
`,
|
|
200
|
+
variableValues: {
|
|
201
|
+
id: author.id,
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
expect(response).toEqual({
|
|
206
|
+
data: {
|
|
207
|
+
node: {
|
|
208
|
+
id: author.id,
|
|
209
|
+
name: author.name,
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
});
|
|
159
213
|
});
|
|
160
214
|
```
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/graphql-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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,18 @@
|
|
|
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-helix": "^1.13.0",
|
|
34
|
-
"koa": "^2.14.2",
|
|
35
|
-
"koa-bodyparser": "^4.4.0",
|
|
36
23
|
"npmlog": "^7.0.1",
|
|
37
24
|
"ts-node": "^10.9.1",
|
|
38
25
|
"yargs": "^17.7.2"
|
|
39
26
|
},
|
|
40
27
|
"peerDependencies": {
|
|
41
|
-
"graphql": "^16.6.0"
|
|
42
|
-
"graphql-compose": "^9.0.10"
|
|
28
|
+
"graphql": "^16.6.0"
|
|
43
29
|
},
|
|
44
30
|
"devDependencies": {
|
|
45
|
-
"@types/koa": "^2.13.6",
|
|
46
|
-
"@types/koa__router": "^12.0.0",
|
|
47
|
-
"@types/koa-bodyparser": "^4.3.10",
|
|
48
31
|
"@types/yargs": "^17.0.24",
|
|
49
32
|
"graphql": "^16.6.0",
|
|
50
|
-
"graphql-compose": "^9.0.10",
|
|
51
33
|
"jest": "^29.5.0",
|
|
52
34
|
"tsup": "^6.7.0",
|
|
53
35
|
"@ttoss/config": "^1.30.0"
|
|
@@ -59,16 +41,6 @@
|
|
|
59
41
|
"publishConfig": {
|
|
60
42
|
"access": "public"
|
|
61
43
|
},
|
|
62
|
-
"typesVersions": {
|
|
63
|
-
"*": {
|
|
64
|
-
".": [
|
|
65
|
-
"./dist/index.d.ts"
|
|
66
|
-
],
|
|
67
|
-
"server": [
|
|
68
|
-
"./dist/server.d.ts"
|
|
69
|
-
]
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
44
|
"scripts": {
|
|
73
45
|
"build": "tsup",
|
|
74
46
|
"test": "jest"
|
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
|
-
};
|