@ttoss/graphql-api-server 0.3.7 → 0.4.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/dist/esm/index.js +16 -33
- package/dist/index.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +15 -34
- package/package.json +6 -4
- package/src/index.ts +26 -49
- package/src/indexGraphqlHelix.ts +113 -0
package/dist/esm/index.js
CHANGED
|
@@ -3,19 +3,14 @@
|
|
|
3
3
|
// src/index.ts
|
|
4
4
|
import { buildSchema } from "@ttoss/graphql-api";
|
|
5
5
|
import { CognitoJwtVerifier } from "aws-jwt-verify";
|
|
6
|
-
import {
|
|
6
|
+
import { createYoga } from "graphql-yoga";
|
|
7
7
|
import Koa from "koa";
|
|
8
|
-
import Router from "@koa/router";
|
|
9
|
-
import bodyParser from "koa-bodyparser";
|
|
10
|
-
import cors from "@koa/cors";
|
|
11
8
|
var createServer = ({
|
|
12
|
-
graphiql = false,
|
|
13
9
|
authenticationType,
|
|
14
10
|
userPoolConfig,
|
|
15
11
|
...buildSchemaInput
|
|
16
12
|
}) => {
|
|
17
|
-
const
|
|
18
|
-
const router = new Router();
|
|
13
|
+
const app = new Koa();
|
|
19
14
|
const jwtVerifier = (() => {
|
|
20
15
|
if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
|
|
21
16
|
if (!userPoolConfig) {
|
|
@@ -28,7 +23,7 @@ var createServer = ({
|
|
|
28
23
|
}
|
|
29
24
|
return null;
|
|
30
25
|
})();
|
|
31
|
-
|
|
26
|
+
app.use(async ctx => {
|
|
32
27
|
const request = {
|
|
33
28
|
body: ctx.request.body,
|
|
34
29
|
headers: ctx.headers,
|
|
@@ -46,32 +41,20 @@ var createServer = ({
|
|
|
46
41
|
ctx.body = "Unauthorized";
|
|
47
42
|
return;
|
|
48
43
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const {
|
|
56
|
-
operationName,
|
|
57
|
-
query,
|
|
58
|
-
variables
|
|
59
|
-
} = getGraphQLParameters(request);
|
|
60
|
-
const result = await processRequest({
|
|
61
|
-
operationName,
|
|
62
|
-
query,
|
|
63
|
-
variables,
|
|
64
|
-
request,
|
|
44
|
+
const operationName = request.body;
|
|
45
|
+
const query = request.headers;
|
|
46
|
+
const variables = request.method;
|
|
47
|
+
const yoga = createYoga({
|
|
65
48
|
schema: buildSchema(buildSchemaInput),
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
identity: ctx.identity
|
|
69
|
-
};
|
|
70
|
-
}
|
|
49
|
+
logging: false
|
|
71
50
|
});
|
|
72
|
-
|
|
51
|
+
const response = await yoga.handleNodeRequest(ctx.req, ctx);
|
|
52
|
+
ctx.status = response.status;
|
|
53
|
+
for (const [key, value] of response.headers.entries()) {
|
|
54
|
+
ctx.append(key, value);
|
|
55
|
+
}
|
|
56
|
+
ctx.body = response.body;
|
|
73
57
|
});
|
|
74
|
-
|
|
75
|
-
return server;
|
|
58
|
+
return app;
|
|
76
59
|
};
|
|
77
|
-
export {
|
|
60
|
+
export { createServer };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { BuildSchemaInput } from '@ttoss/graphql-api';
|
|
2
2
|
import Koa from 'koa';
|
|
3
|
-
export { default as Router } from '@koa/router';
|
|
4
3
|
|
|
5
4
|
type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
6
5
|
type CreateServerInput = {
|
|
@@ -12,6 +11,6 @@ type CreateServerInput = {
|
|
|
12
11
|
clientId: string;
|
|
13
12
|
};
|
|
14
13
|
} & BuildSchemaInput;
|
|
15
|
-
declare const createServer: ({
|
|
14
|
+
declare const createServer: ({ authenticationType, userPoolConfig, ...buildSchemaInput }: CreateServerInput) => Koa;
|
|
16
15
|
|
|
17
16
|
export { type AuthenticationType, type CreateServerInput, createServer };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { BuildSchemaInput } from '@ttoss/graphql-api';
|
|
2
2
|
import Koa from 'koa';
|
|
3
|
-
export { default as Router } from '@koa/router';
|
|
4
3
|
|
|
5
4
|
type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
6
5
|
type CreateServerInput = {
|
|
@@ -12,6 +11,6 @@ type CreateServerInput = {
|
|
|
12
11
|
clientId: string;
|
|
13
12
|
};
|
|
14
13
|
} & BuildSchemaInput;
|
|
15
|
-
declare const createServer: ({
|
|
14
|
+
declare const createServer: ({ authenticationType, userPoolConfig, ...buildSchemaInput }: CreateServerInput) => Koa;
|
|
16
15
|
|
|
17
16
|
export { type AuthenticationType, type CreateServerInput, createServer };
|
package/dist/index.js
CHANGED
|
@@ -38,25 +38,19 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
|
38
38
|
// src/index.ts
|
|
39
39
|
var src_exports = {};
|
|
40
40
|
__export(src_exports, {
|
|
41
|
-
Router: () => import_router.default,
|
|
42
41
|
createServer: () => createServer
|
|
43
42
|
});
|
|
44
43
|
module.exports = __toCommonJS(src_exports);
|
|
45
44
|
var import_graphql_api = require("@ttoss/graphql-api");
|
|
46
45
|
var import_aws_jwt_verify = require("aws-jwt-verify");
|
|
47
|
-
var
|
|
46
|
+
var import_graphql_yoga = require("graphql-yoga");
|
|
48
47
|
var import_koa = __toESM(require("koa"));
|
|
49
|
-
var import_router = __toESM(require("@koa/router"));
|
|
50
|
-
var import_koa_bodyparser = __toESM(require("koa-bodyparser"));
|
|
51
|
-
var import_cors = __toESM(require("@koa/cors"));
|
|
52
48
|
var createServer = ({
|
|
53
|
-
graphiql = false,
|
|
54
49
|
authenticationType,
|
|
55
50
|
userPoolConfig,
|
|
56
51
|
...buildSchemaInput
|
|
57
52
|
}) => {
|
|
58
|
-
const
|
|
59
|
-
const router = new import_router.default();
|
|
53
|
+
const app = new import_koa.default();
|
|
60
54
|
const jwtVerifier = (() => {
|
|
61
55
|
if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
|
|
62
56
|
if (!userPoolConfig) {
|
|
@@ -69,7 +63,7 @@ var createServer = ({
|
|
|
69
63
|
}
|
|
70
64
|
return null;
|
|
71
65
|
})();
|
|
72
|
-
|
|
66
|
+
app.use(async ctx => {
|
|
73
67
|
const request = {
|
|
74
68
|
body: ctx.request.body,
|
|
75
69
|
headers: ctx.headers,
|
|
@@ -87,36 +81,23 @@ var createServer = ({
|
|
|
87
81
|
ctx.body = "Unauthorized";
|
|
88
82
|
return;
|
|
89
83
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
const {
|
|
97
|
-
operationName,
|
|
98
|
-
query,
|
|
99
|
-
variables
|
|
100
|
-
} = (0, import_graphql_helix.getGraphQLParameters)(request);
|
|
101
|
-
const result = await (0, import_graphql_helix.processRequest)({
|
|
102
|
-
operationName,
|
|
103
|
-
query,
|
|
104
|
-
variables,
|
|
105
|
-
request,
|
|
84
|
+
const operationName = request.body;
|
|
85
|
+
const query = request.headers;
|
|
86
|
+
const variables = request.method;
|
|
87
|
+
const yoga = (0, import_graphql_yoga.createYoga)({
|
|
106
88
|
schema: (0, import_graphql_api.buildSchema)(buildSchemaInput),
|
|
107
|
-
|
|
108
|
-
return {
|
|
109
|
-
identity: ctx.identity
|
|
110
|
-
};
|
|
111
|
-
}
|
|
89
|
+
logging: false
|
|
112
90
|
});
|
|
113
|
-
|
|
91
|
+
const response = await yoga.handleNodeRequest(ctx.req, ctx);
|
|
92
|
+
ctx.status = response.status;
|
|
93
|
+
for (const [key, value] of response.headers.entries()) {
|
|
94
|
+
ctx.append(key, value);
|
|
95
|
+
}
|
|
96
|
+
ctx.body = response.body;
|
|
114
97
|
});
|
|
115
|
-
|
|
116
|
-
return server;
|
|
98
|
+
return app;
|
|
117
99
|
};
|
|
118
100
|
// Annotate the CommonJS export names for ESM import in node:
|
|
119
101
|
0 && (module.exports = {
|
|
120
|
-
Router,
|
|
121
102
|
createServer
|
|
122
103
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/graphql-api-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "GraphQL API Server",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -24,12 +24,13 @@
|
|
|
24
24
|
"@koa/router": "^12.0.1",
|
|
25
25
|
"aws-jwt-verify": "^4.0.0",
|
|
26
26
|
"graphql-helix": "^1.13.0",
|
|
27
|
+
"graphql-yoga": "^5.0.2",
|
|
27
28
|
"koa": "^2.14.2",
|
|
28
29
|
"koa-bodyparser": "^4.4.1"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
31
32
|
"graphql": "^16.6.0",
|
|
32
|
-
"@ttoss/graphql-api": "^0.
|
|
33
|
+
"@ttoss/graphql-api": "^0.5.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
36
|
"@types/koa": "^2.13.12",
|
|
@@ -38,9 +39,10 @@
|
|
|
38
39
|
"@types/koa-bodyparser": "^4.3.12",
|
|
39
40
|
"graphql": "^16.8.1",
|
|
40
41
|
"jest": "^29.7.0",
|
|
42
|
+
"supertest": "^6.3.3",
|
|
41
43
|
"tsup": "^8.0.1",
|
|
42
44
|
"@ttoss/config": "^1.31.4",
|
|
43
|
-
"@ttoss/graphql-api": "^0.
|
|
45
|
+
"@ttoss/graphql-api": "^0.5.0"
|
|
44
46
|
},
|
|
45
47
|
"keywords": [
|
|
46
48
|
"api",
|
|
@@ -53,6 +55,6 @@
|
|
|
53
55
|
},
|
|
54
56
|
"scripts": {
|
|
55
57
|
"build": "tsup",
|
|
56
|
-
"test": "
|
|
58
|
+
"test": "jest"
|
|
57
59
|
}
|
|
58
60
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BuildSchemaInput, buildSchema } from '@ttoss/graphql-api';
|
|
2
2
|
import { CognitoJwtVerifier } from 'aws-jwt-verify';
|
|
3
|
-
import {
|
|
4
|
-
getGraphQLParameters,
|
|
5
|
-
processRequest,
|
|
6
|
-
renderGraphiQL,
|
|
7
|
-
sendResult,
|
|
8
|
-
shouldRenderGraphiQL,
|
|
9
|
-
} from 'graphql-helix';
|
|
3
|
+
import { createYoga } from 'graphql-yoga';
|
|
10
4
|
import Koa from 'koa';
|
|
11
|
-
import Router from '@koa/router';
|
|
12
|
-
import bodyParser from 'koa-bodyparser';
|
|
13
|
-
import cors from '@koa/cors';
|
|
14
|
-
|
|
15
|
-
export { Router };
|
|
16
5
|
|
|
17
6
|
export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
18
7
|
|
|
@@ -27,19 +16,12 @@ export type CreateServerInput = {
|
|
|
27
16
|
} & BuildSchemaInput;
|
|
28
17
|
|
|
29
18
|
export const createServer = ({
|
|
30
|
-
graphiql = false,
|
|
31
19
|
authenticationType,
|
|
32
20
|
userPoolConfig,
|
|
33
21
|
...buildSchemaInput
|
|
34
22
|
}: CreateServerInput): Koa => {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
const router = new Router();
|
|
23
|
+
const app = new Koa();
|
|
38
24
|
|
|
39
|
-
/**
|
|
40
|
-
* Create the verifier outside your route handlers,
|
|
41
|
-
* so the cache is persisted and can be shared amongst them.
|
|
42
|
-
*/
|
|
43
25
|
const jwtVerifier = (() => {
|
|
44
26
|
if (authenticationType === 'AMAZON_COGNITO_USER_POOLS') {
|
|
45
27
|
if (!userPoolConfig) {
|
|
@@ -57,7 +39,7 @@ export const createServer = ({
|
|
|
57
39
|
return null;
|
|
58
40
|
})();
|
|
59
41
|
|
|
60
|
-
|
|
42
|
+
app.use(async (ctx) => {
|
|
61
43
|
const request = {
|
|
62
44
|
body: ctx.request.body,
|
|
63
45
|
headers: ctx.headers,
|
|
@@ -65,6 +47,8 @@ export const createServer = ({
|
|
|
65
47
|
query: ctx.request.query,
|
|
66
48
|
};
|
|
67
49
|
|
|
50
|
+
//console.log(request);
|
|
51
|
+
|
|
68
52
|
try {
|
|
69
53
|
if (authenticationType === 'AMAZON_COGNITO_USER_POOLS' && jwtVerifier) {
|
|
70
54
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
|
@@ -77,37 +61,30 @@ export const createServer = ({
|
|
|
77
61
|
return;
|
|
78
62
|
}
|
|
79
63
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
65
|
+
const operationName = request.body;
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
67
|
+
const query = request.headers;
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
69
|
+
const variables = request.method;
|
|
87
70
|
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
const result = await processRequest({
|
|
91
|
-
operationName,
|
|
92
|
-
query,
|
|
93
|
-
variables,
|
|
94
|
-
request,
|
|
71
|
+
const yoga = createYoga<Koa.ParameterizedContext>({
|
|
95
72
|
schema: buildSchema(buildSchemaInput),
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
identity: ctx.identity,
|
|
99
|
-
};
|
|
100
|
-
},
|
|
73
|
+
logging: false,
|
|
101
74
|
});
|
|
102
75
|
|
|
103
|
-
|
|
104
|
-
|
|
76
|
+
const response = await yoga.handleNodeRequest(ctx.req, ctx);
|
|
77
|
+
|
|
78
|
+
// Set status code
|
|
79
|
+
ctx.status = response.status;
|
|
105
80
|
|
|
106
|
-
|
|
107
|
-
.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
81
|
+
// Set headers
|
|
82
|
+
for (const [key, value] of response.headers.entries()) {
|
|
83
|
+
ctx.append(key, value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
ctx.body = response.body;
|
|
87
|
+
});
|
|
111
88
|
|
|
112
|
-
return
|
|
89
|
+
return app;
|
|
113
90
|
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type BuildSchemaInput, buildSchema } from '@ttoss/graphql-api';
|
|
2
|
+
import { CognitoJwtVerifier } from 'aws-jwt-verify';
|
|
3
|
+
import {
|
|
4
|
+
getGraphQLParameters,
|
|
5
|
+
processRequest,
|
|
6
|
+
renderGraphiQL,
|
|
7
|
+
sendResult,
|
|
8
|
+
shouldRenderGraphiQL,
|
|
9
|
+
} from 'graphql-helix';
|
|
10
|
+
import Koa from 'koa';
|
|
11
|
+
import Router from '@koa/router';
|
|
12
|
+
import bodyParser from 'koa-bodyparser';
|
|
13
|
+
import cors from '@koa/cors';
|
|
14
|
+
|
|
15
|
+
export { Router };
|
|
16
|
+
|
|
17
|
+
export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
18
|
+
|
|
19
|
+
export type CreateServerInput = {
|
|
20
|
+
graphiql?: boolean;
|
|
21
|
+
authenticationType?: AuthenticationType;
|
|
22
|
+
userPoolConfig?: {
|
|
23
|
+
userPoolId: string;
|
|
24
|
+
tokenUse?: 'access' | 'id';
|
|
25
|
+
clientId: string;
|
|
26
|
+
};
|
|
27
|
+
} & BuildSchemaInput;
|
|
28
|
+
|
|
29
|
+
export const createServerNew = ({
|
|
30
|
+
graphiql = false,
|
|
31
|
+
authenticationType,
|
|
32
|
+
userPoolConfig,
|
|
33
|
+
...buildSchemaInput
|
|
34
|
+
}: CreateServerInput): Koa => {
|
|
35
|
+
const server = new Koa();
|
|
36
|
+
|
|
37
|
+
const router = new Router();
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create the verifier outside your route handlers,
|
|
41
|
+
* so the cache is persisted and can be shared amongst them.
|
|
42
|
+
*/
|
|
43
|
+
const jwtVerifier = (() => {
|
|
44
|
+
if (authenticationType === 'AMAZON_COGNITO_USER_POOLS') {
|
|
45
|
+
if (!userPoolConfig) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
'userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType'
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return CognitoJwtVerifier.create({
|
|
52
|
+
tokenUse: 'access',
|
|
53
|
+
...userPoolConfig,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return null;
|
|
58
|
+
})();
|
|
59
|
+
|
|
60
|
+
router.all('/graphql', async (ctx) => {
|
|
61
|
+
const request = {
|
|
62
|
+
body: ctx.request.body,
|
|
63
|
+
headers: ctx.headers,
|
|
64
|
+
method: ctx.method,
|
|
65
|
+
query: ctx.request.query,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
if (authenticationType === 'AMAZON_COGNITO_USER_POOLS' && jwtVerifier) {
|
|
70
|
+
const token = request.headers.authorization?.replace('Bearer ', '');
|
|
71
|
+
const identity = await jwtVerifier.verify(token || '');
|
|
72
|
+
ctx.identity = identity;
|
|
73
|
+
}
|
|
74
|
+
} catch {
|
|
75
|
+
ctx.status = 401;
|
|
76
|
+
ctx.body = 'Unauthorized';
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (shouldRenderGraphiQL(request)) {
|
|
81
|
+
if (graphiql) {
|
|
82
|
+
ctx.body = renderGraphiQL({});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const { operationName, query, variables } = getGraphQLParameters(request);
|
|
89
|
+
|
|
90
|
+
const result = await processRequest({
|
|
91
|
+
operationName,
|
|
92
|
+
query,
|
|
93
|
+
variables,
|
|
94
|
+
request,
|
|
95
|
+
schema: buildSchema(buildSchemaInput),
|
|
96
|
+
contextFactory: () => {
|
|
97
|
+
return {
|
|
98
|
+
identity: ctx.identity,
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
sendResult(result, ctx.res);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
server
|
|
107
|
+
.use(cors())
|
|
108
|
+
.use(bodyParser())
|
|
109
|
+
.use(router.routes())
|
|
110
|
+
.use(router.allowedMethods());
|
|
111
|
+
|
|
112
|
+
return server;
|
|
113
|
+
};
|