@ttoss/graphql-api-server 0.6.12 → 0.7.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/dist/esm/index.js +68 -0
- package/dist/index.d.ts +18 -0
- package/package.json +8 -14
- package/src/index.ts +4 -6
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { App, Router, cors } from "@ttoss/http-server";
|
|
5
|
+
import { buildSchema } from "@ttoss/graphql-api";
|
|
6
|
+
import { CognitoJwtVerifier } from "@ttoss/auth-core/amazon-cognito";
|
|
7
|
+
import { createYoga } from "graphql-yoga";
|
|
8
|
+
var createServer = ({
|
|
9
|
+
authenticationType,
|
|
10
|
+
userPoolConfig,
|
|
11
|
+
graphiql,
|
|
12
|
+
cors: corsOptions,
|
|
13
|
+
...buildSchemaInput
|
|
14
|
+
}) => {
|
|
15
|
+
const app = new App();
|
|
16
|
+
app.use(cors(corsOptions));
|
|
17
|
+
const yoga = createYoga({
|
|
18
|
+
schema: buildSchema(buildSchemaInput),
|
|
19
|
+
graphiql,
|
|
20
|
+
landingPage: false,
|
|
21
|
+
logging: false,
|
|
22
|
+
/**
|
|
23
|
+
* Disable CORS, as it's handled by Koa middleware
|
|
24
|
+
*/
|
|
25
|
+
cors: false
|
|
26
|
+
});
|
|
27
|
+
const jwtVerifier = (() => {
|
|
28
|
+
if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
|
|
29
|
+
if (!userPoolConfig) {
|
|
30
|
+
throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
|
|
31
|
+
}
|
|
32
|
+
return CognitoJwtVerifier.create({
|
|
33
|
+
tokenUse: "access",
|
|
34
|
+
...userPoolConfig
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
})();
|
|
39
|
+
app.use(async (ctx, next) => {
|
|
40
|
+
if (ctx.path !== "/graphql") {
|
|
41
|
+
return next();
|
|
42
|
+
}
|
|
43
|
+
const isGraphiqlRequest = ctx.headers.accept?.includes("text/html") && graphiql;
|
|
44
|
+
if (!isGraphiqlRequest) {
|
|
45
|
+
try {
|
|
46
|
+
if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
|
|
47
|
+
const token = ctx.headers.authorization?.replace("Bearer ", "");
|
|
48
|
+
const identity = await jwtVerifier.verify(token || "");
|
|
49
|
+
ctx.identity = identity;
|
|
50
|
+
}
|
|
51
|
+
} catch {
|
|
52
|
+
ctx.status = 401;
|
|
53
|
+
ctx.body = "Unauthorized";
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const response = await yoga.handleNodeRequest(ctx.req, ctx);
|
|
58
|
+
ctx.status = response.status;
|
|
59
|
+
for (const [key, value] of response.headers.entries()) {
|
|
60
|
+
if (ctx.status != 401) {
|
|
61
|
+
ctx.append(key, value);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
ctx.body = response.body;
|
|
65
|
+
});
|
|
66
|
+
return app;
|
|
67
|
+
};
|
|
68
|
+
export { Router, createServer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { cors, App } from '@ttoss/http-server';
|
|
2
|
+
export { Router } from '@ttoss/http-server';
|
|
3
|
+
import { BuildSchemaInput } from '@ttoss/graphql-api';
|
|
4
|
+
|
|
5
|
+
type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
6
|
+
type CreateServerInput = {
|
|
7
|
+
graphiql?: boolean;
|
|
8
|
+
authenticationType?: AuthenticationType;
|
|
9
|
+
userPoolConfig?: {
|
|
10
|
+
userPoolId: string;
|
|
11
|
+
tokenUse?: 'access' | 'id';
|
|
12
|
+
clientId: string;
|
|
13
|
+
};
|
|
14
|
+
cors?: cors.Options;
|
|
15
|
+
} & BuildSchemaInput;
|
|
16
|
+
declare const createServer: ({ authenticationType, userPoolConfig, graphiql, cors: corsOptions, ...buildSchemaInput }: CreateServerInput) => App;
|
|
17
|
+
|
|
18
|
+
export { type AuthenticationType, type CreateServerInput, createServer };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/graphql-api-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "GraphQL API Server",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"url": "https://github.com/ttoss/ttoss.git",
|
|
12
12
|
"directory": "packages/graphql-api-server"
|
|
13
13
|
},
|
|
14
|
+
"type": "module",
|
|
14
15
|
"exports": {
|
|
15
16
|
".": {
|
|
16
17
|
"import": "./dist/esm/index.js",
|
|
17
|
-
"require": "./dist/index.js",
|
|
18
18
|
"types": "./dist/index.d.ts"
|
|
19
19
|
}
|
|
20
20
|
},
|
|
@@ -24,27 +24,21 @@
|
|
|
24
24
|
],
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@koa/cors": "^5.0.0",
|
|
28
|
-
"@koa/router": "^12.0.1",
|
|
29
27
|
"graphql-yoga": "^5.1.1",
|
|
30
|
-
"
|
|
31
|
-
"@ttoss/
|
|
28
|
+
"@ttoss/auth-core": "^0.2.0",
|
|
29
|
+
"@ttoss/graphql-api": "^0.7.6",
|
|
30
|
+
"@ttoss/http-server": "^0.1.0"
|
|
32
31
|
},
|
|
33
32
|
"peerDependencies": {
|
|
34
|
-
"graphql": "^16.6.0"
|
|
35
|
-
"@ttoss/graphql-api": "^0.7.5"
|
|
33
|
+
"graphql": "^16.6.0"
|
|
36
34
|
},
|
|
37
35
|
"devDependencies": {
|
|
38
|
-
"@types/koa": "^2.14.0",
|
|
39
|
-
"@types/koa__cors": "^5.0.0",
|
|
40
|
-
"@types/koa__router": "^12.0.4",
|
|
41
36
|
"@types/supertest": "^6.0.2",
|
|
42
37
|
"graphql": "^16.8.1",
|
|
43
38
|
"jest": "^29.7.0",
|
|
44
39
|
"supertest": "^6.3.4",
|
|
45
40
|
"tsup": "^8.3.0",
|
|
46
|
-
"@ttoss/config": "^1.
|
|
47
|
-
"@ttoss/graphql-api": "^0.7.5"
|
|
41
|
+
"@ttoss/config": "^1.34.0"
|
|
48
42
|
},
|
|
49
43
|
"keywords": [
|
|
50
44
|
"api",
|
|
@@ -57,6 +51,6 @@
|
|
|
57
51
|
},
|
|
58
52
|
"scripts": {
|
|
59
53
|
"build": "tsup",
|
|
60
|
-
"test": "jest"
|
|
54
|
+
"test": "jest --projects tests/unit"
|
|
61
55
|
}
|
|
62
56
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
+
import { App, Router, cors } from '@ttoss/http-server';
|
|
1
2
|
import { BuildSchemaInput, buildSchema } from '@ttoss/graphql-api';
|
|
2
3
|
import { CognitoJwtVerifier } from '@ttoss/auth-core/amazon-cognito';
|
|
3
4
|
import { createYoga } from 'graphql-yoga';
|
|
4
|
-
import Koa from 'koa';
|
|
5
|
-
import Router from '@koa/router';
|
|
6
|
-
import cors from '@koa/cors';
|
|
7
5
|
|
|
8
6
|
export { Router };
|
|
9
7
|
|
|
@@ -26,15 +24,15 @@ export const createServer = ({
|
|
|
26
24
|
graphiql,
|
|
27
25
|
cors: corsOptions,
|
|
28
26
|
...buildSchemaInput
|
|
29
|
-
}: CreateServerInput):
|
|
30
|
-
const app = new
|
|
27
|
+
}: CreateServerInput): App => {
|
|
28
|
+
const app = new App();
|
|
31
29
|
|
|
32
30
|
app.use(cors(corsOptions));
|
|
33
31
|
|
|
34
32
|
/**
|
|
35
33
|
* https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-koa
|
|
36
34
|
*/
|
|
37
|
-
const yoga = createYoga<
|
|
35
|
+
const yoga = createYoga<App.ParameterizedContext>({
|
|
38
36
|
schema: buildSchema(buildSchemaInput),
|
|
39
37
|
graphiql,
|
|
40
38
|
landingPage: false,
|