@ttoss/graphql-api-server 0.4.0 → 0.5.1
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 +1 -1
- package/dist/esm/index.js +17 -12
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +19 -12
- package/package.json +10 -10
- package/src/index.ts +23 -14
- package/src/indexGraphqlHelix.ts +0 -113
package/README.md
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
4
|
import { buildSchema } from "@ttoss/graphql-api";
|
|
5
|
-
import { CognitoJwtVerifier } from "
|
|
5
|
+
import { CognitoJwtVerifier } from "@ttoss/auth-core/amazon-cognito";
|
|
6
6
|
import { createYoga } from "graphql-yoga";
|
|
7
7
|
import Koa from "koa";
|
|
8
|
+
import Router from "@koa/router";
|
|
8
9
|
var createServer = ({
|
|
9
10
|
authenticationType,
|
|
10
11
|
userPoolConfig,
|
|
@@ -30,16 +31,18 @@ var createServer = ({
|
|
|
30
31
|
method: ctx.method,
|
|
31
32
|
query: ctx.request.query
|
|
32
33
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
if (request.method !== "GET" && request.headers.referer !== "http://localhost:4000/graphql") {
|
|
35
|
+
try {
|
|
36
|
+
if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
|
|
37
|
+
const token = request.headers.authorization?.replace("Bearer ", "");
|
|
38
|
+
const identity = await jwtVerifier.verify(token || "");
|
|
39
|
+
ctx.identity = identity;
|
|
40
|
+
}
|
|
41
|
+
} catch {
|
|
42
|
+
ctx.status = 401;
|
|
43
|
+
ctx.body = "Unauthorized";
|
|
44
|
+
return;
|
|
38
45
|
}
|
|
39
|
-
} catch {
|
|
40
|
-
ctx.status = 401;
|
|
41
|
-
ctx.body = "Unauthorized";
|
|
42
|
-
return;
|
|
43
46
|
}
|
|
44
47
|
const operationName = request.body;
|
|
45
48
|
const query = request.headers;
|
|
@@ -51,10 +54,12 @@ var createServer = ({
|
|
|
51
54
|
const response = await yoga.handleNodeRequest(ctx.req, ctx);
|
|
52
55
|
ctx.status = response.status;
|
|
53
56
|
for (const [key, value] of response.headers.entries()) {
|
|
54
|
-
ctx.
|
|
57
|
+
if (ctx.status != 401) {
|
|
58
|
+
ctx.append(key, value);
|
|
59
|
+
}
|
|
55
60
|
}
|
|
56
61
|
ctx.body = response.body;
|
|
57
62
|
});
|
|
58
63
|
return app;
|
|
59
64
|
};
|
|
60
|
-
export { createServer };
|
|
65
|
+
export { Router, createServer };
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -38,13 +38,15 @@ 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,
|
|
41
42
|
createServer: () => createServer
|
|
42
43
|
});
|
|
43
44
|
module.exports = __toCommonJS(src_exports);
|
|
44
45
|
var import_graphql_api = require("@ttoss/graphql-api");
|
|
45
|
-
var
|
|
46
|
+
var import_amazon_cognito = require("@ttoss/auth-core/amazon-cognito");
|
|
46
47
|
var import_graphql_yoga = require("graphql-yoga");
|
|
47
48
|
var import_koa = __toESM(require("koa"));
|
|
49
|
+
var import_router = __toESM(require("@koa/router"));
|
|
48
50
|
var createServer = ({
|
|
49
51
|
authenticationType,
|
|
50
52
|
userPoolConfig,
|
|
@@ -56,7 +58,7 @@ var createServer = ({
|
|
|
56
58
|
if (!userPoolConfig) {
|
|
57
59
|
throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
|
|
58
60
|
}
|
|
59
|
-
return
|
|
61
|
+
return import_amazon_cognito.CognitoJwtVerifier.create({
|
|
60
62
|
tokenUse: "access",
|
|
61
63
|
...userPoolConfig
|
|
62
64
|
});
|
|
@@ -70,16 +72,18 @@ var createServer = ({
|
|
|
70
72
|
method: ctx.method,
|
|
71
73
|
query: ctx.request.query
|
|
72
74
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
if (request.method !== "GET" && request.headers.referer !== "http://localhost:4000/graphql") {
|
|
76
|
+
try {
|
|
77
|
+
if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
|
|
78
|
+
const token = request.headers.authorization?.replace("Bearer ", "");
|
|
79
|
+
const identity = await jwtVerifier.verify(token || "");
|
|
80
|
+
ctx.identity = identity;
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
ctx.status = 401;
|
|
84
|
+
ctx.body = "Unauthorized";
|
|
85
|
+
return;
|
|
78
86
|
}
|
|
79
|
-
} catch {
|
|
80
|
-
ctx.status = 401;
|
|
81
|
-
ctx.body = "Unauthorized";
|
|
82
|
-
return;
|
|
83
87
|
}
|
|
84
88
|
const operationName = request.body;
|
|
85
89
|
const query = request.headers;
|
|
@@ -91,7 +95,9 @@ var createServer = ({
|
|
|
91
95
|
const response = await yoga.handleNodeRequest(ctx.req, ctx);
|
|
92
96
|
ctx.status = response.status;
|
|
93
97
|
for (const [key, value] of response.headers.entries()) {
|
|
94
|
-
ctx.
|
|
98
|
+
if (ctx.status != 401) {
|
|
99
|
+
ctx.append(key, value);
|
|
100
|
+
}
|
|
95
101
|
}
|
|
96
102
|
ctx.body = response.body;
|
|
97
103
|
});
|
|
@@ -99,5 +105,6 @@ var createServer = ({
|
|
|
99
105
|
};
|
|
100
106
|
// Annotate the CommonJS export names for ESM import in node:
|
|
101
107
|
0 && (module.exports = {
|
|
108
|
+
Router,
|
|
102
109
|
createServer
|
|
103
110
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/graphql-api-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "GraphQL API Server",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -20,26 +20,26 @@
|
|
|
20
20
|
"sideEffects": false,
|
|
21
21
|
"typings": "dist/index.d.ts",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@koa/cors": "^
|
|
23
|
+
"@koa/cors": "^5.0.0",
|
|
24
24
|
"@koa/router": "^12.0.1",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"koa-bodyparser": "^4.4.1"
|
|
25
|
+
"graphql-yoga": "^5.1.1",
|
|
26
|
+
"koa": "^2.15.0",
|
|
27
|
+
"koa-bodyparser": "^4.4.1",
|
|
28
|
+
"@ttoss/auth-core": "^0.0.2"
|
|
30
29
|
},
|
|
31
30
|
"peerDependencies": {
|
|
32
31
|
"graphql": "^16.6.0",
|
|
33
32
|
"@ttoss/graphql-api": "^0.5.0"
|
|
34
33
|
},
|
|
35
34
|
"devDependencies": {
|
|
36
|
-
"@types/koa": "^2.
|
|
37
|
-
"@types/koa__cors": "^
|
|
35
|
+
"@types/koa": "^2.14.0",
|
|
36
|
+
"@types/koa__cors": "^5.0.0",
|
|
38
37
|
"@types/koa__router": "^12.0.4",
|
|
39
38
|
"@types/koa-bodyparser": "^4.3.12",
|
|
39
|
+
"@types/supertest": "^6.0.2",
|
|
40
40
|
"graphql": "^16.8.1",
|
|
41
41
|
"jest": "^29.7.0",
|
|
42
|
-
"supertest": "^6.3.
|
|
42
|
+
"supertest": "^6.3.4",
|
|
43
43
|
"tsup": "^8.0.1",
|
|
44
44
|
"@ttoss/config": "^1.31.4",
|
|
45
45
|
"@ttoss/graphql-api": "^0.5.0"
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { BuildSchemaInput, buildSchema } from '@ttoss/graphql-api';
|
|
2
|
-
import { CognitoJwtVerifier } from '
|
|
2
|
+
import { CognitoJwtVerifier } from '@ttoss/auth-core/amazon-cognito';
|
|
3
3
|
import { createYoga } from 'graphql-yoga';
|
|
4
4
|
import Koa from 'koa';
|
|
5
|
+
import Router from '@koa/router';
|
|
6
|
+
|
|
7
|
+
export { Router };
|
|
5
8
|
|
|
6
9
|
export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
7
10
|
|
|
@@ -29,7 +32,6 @@ export const createServer = ({
|
|
|
29
32
|
'userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType'
|
|
30
33
|
);
|
|
31
34
|
}
|
|
32
|
-
|
|
33
35
|
return CognitoJwtVerifier.create({
|
|
34
36
|
tokenUse: 'access',
|
|
35
37
|
...userPoolConfig,
|
|
@@ -47,20 +49,25 @@ export const createServer = ({
|
|
|
47
49
|
query: ctx.request.query,
|
|
48
50
|
};
|
|
49
51
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
if (
|
|
53
|
+
request.method !== 'GET' &&
|
|
54
|
+
request.headers.referer !== 'http://localhost:4000/graphql'
|
|
55
|
+
) {
|
|
56
|
+
try {
|
|
57
|
+
if (authenticationType === 'AMAZON_COGNITO_USER_POOLS' && jwtVerifier) {
|
|
58
|
+
const token = request.headers.authorization?.replace('Bearer ', '');
|
|
59
|
+
const identity = await jwtVerifier.verify(token || '');
|
|
60
|
+
|
|
61
|
+
ctx.identity = identity;
|
|
62
|
+
}
|
|
63
|
+
} catch {
|
|
64
|
+
ctx.status = 401;
|
|
65
|
+
ctx.body = 'Unauthorized';
|
|
66
|
+
return;
|
|
57
67
|
}
|
|
58
|
-
} catch {
|
|
59
|
-
ctx.status = 401;
|
|
60
|
-
ctx.body = 'Unauthorized';
|
|
61
|
-
return;
|
|
62
68
|
}
|
|
63
69
|
|
|
70
|
+
//console.log(ctx.identity);
|
|
64
71
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
65
72
|
const operationName = request.body;
|
|
66
73
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -80,7 +87,9 @@ export const createServer = ({
|
|
|
80
87
|
|
|
81
88
|
// Set headers
|
|
82
89
|
for (const [key, value] of response.headers.entries()) {
|
|
83
|
-
ctx.
|
|
90
|
+
if (ctx.status != 401) {
|
|
91
|
+
ctx.append(key, value);
|
|
92
|
+
}
|
|
84
93
|
}
|
|
85
94
|
|
|
86
95
|
ctx.body = response.body;
|
package/src/indexGraphqlHelix.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
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
|
-
};
|