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