@ttoss/graphql-api-server 0.5.0 → 0.5.2

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 CHANGED
@@ -5,10 +5,10 @@ This package provides a Koa server to run your [`@ttoss/graphql-api` API](https:
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- yarn add @ttoss/graphql-api-server @ttoss/graphql-api graphql
8
+ pnpm add @ttoss/graphql-api-server @ttoss/graphql-api graphql
9
9
  ```
10
10
 
11
- ## Quickstart
11
+ ## Getting Started
12
12
 
13
13
  You can use the `createServer` method to create your server.
14
14
 
@@ -60,7 +60,7 @@ server.listen(3000, () => {
60
60
  });
61
61
  ```
62
62
 
63
- ### Middlewares
63
+ ## Middlewares
64
64
 
65
65
  You can add middlewares compatible with [`graphql-middleware`](https://github.com/dimatill/graphql-middleware) to the server using the `middlewares` option.
66
66
 
@@ -97,3 +97,61 @@ const server = createServer({
97
97
  middlewares: [permissions],
98
98
  });
99
99
  ```
100
+
101
+ ## Handling Other Routes
102
+
103
+ If you want to handle other routes than `/graphql`, you can use the `Router` class from `koa` and add it to the server.
104
+
105
+ ### Serving a SPA
106
+
107
+ ```ts
108
+ import { createServer } from '@ttoss/graphql-api-server';
109
+ import { schemaComposer } from './schemaComposer';
110
+ import mount from 'koa-mount';
111
+ import * as path from 'path';
112
+
113
+ const server = createServer({
114
+ schemaComposer,
115
+ graphiql: true,
116
+ });
117
+
118
+ const APP_DIR = path.resolve(__dirname, '../../app/dist');
119
+
120
+ server.use(mount('/', serve(APP_DIR)));
121
+
122
+ /**
123
+ * Serve a SPA—redirect all requests to index.html
124
+ * https://dejanvasic.wordpress.com/2020/08/22/serving-react-spa-in-koa/
125
+ */
126
+ server.use(async (ctx, next) => {
127
+ return await serve(APP_DIR)(Object.assign(ctx, { path: 'index.html' }), next);
128
+ });
129
+
130
+ server.listen(3000, () => {
131
+ console.log('Server listening on port 3000');
132
+ });
133
+ ```
134
+
135
+ ### Serving Another Endpoint
136
+
137
+ ```ts
138
+ import { Router, createServer } from '@ttoss/graphql-api-server';
139
+ import { schemaComposer } from './schemaComposer';
140
+
141
+ const server = createServer({
142
+ schemaComposer,
143
+ graphiql: true,
144
+ });
145
+
146
+ const router = new Router();
147
+
148
+ router.get('/health', (ctx: any) => {
149
+ ctx.body = 'OK';
150
+ });
151
+
152
+ server.use(router.routes()).use(router.allowedMethods());
153
+
154
+ server.listen(3000, () => {
155
+ console.log('Server listening on port 3000');
156
+ });
157
+ ```
package/dist/esm/index.js CHANGED
@@ -2,15 +2,23 @@
2
2
 
3
3
  // src/index.ts
4
4
  import { buildSchema } from "@ttoss/graphql-api";
5
- import { CognitoJwtVerifier } from "aws-jwt-verify";
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,
12
+ graphiql,
11
13
  ...buildSchemaInput
12
14
  }) => {
13
15
  const app = new Koa();
16
+ const yoga = createYoga({
17
+ schema: buildSchema(buildSchemaInput),
18
+ graphiql,
19
+ landingPage: false,
20
+ logging: false
21
+ });
14
22
  const jwtVerifier = (() => {
15
23
  if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
16
24
  if (!userPoolConfig) {
@@ -23,17 +31,15 @@ var createServer = ({
23
31
  }
24
32
  return null;
25
33
  })();
26
- app.use(async ctx => {
27
- const request = {
28
- body: ctx.request.body,
29
- headers: ctx.headers,
30
- method: ctx.method,
31
- query: ctx.request.query
32
- };
33
- if (request.method !== "GET" && request.headers.referer !== "http://localhost:4000/graphql") {
34
+ app.use(async (ctx, next) => {
35
+ if (ctx.path !== "/graphql") {
36
+ return next();
37
+ }
38
+ const isGraphiqlRequest = ctx.headers.accept?.includes("text/html") && graphiql;
39
+ if (!isGraphiqlRequest) {
34
40
  try {
35
41
  if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
36
- const token = request.headers.authorization?.replace("Bearer ", "");
42
+ const token = ctx.headers.authorization?.replace("Bearer ", "");
37
43
  const identity = await jwtVerifier.verify(token || "");
38
44
  ctx.identity = identity;
39
45
  }
@@ -43,13 +49,6 @@ var createServer = ({
43
49
  return;
44
50
  }
45
51
  }
46
- const operationName = request.body;
47
- const query = request.headers;
48
- const variables = request.method;
49
- const yoga = createYoga({
50
- schema: buildSchema(buildSchemaInput),
51
- logging: false
52
- });
53
52
  const response = await yoga.handleNodeRequest(ctx.req, ctx);
54
53
  ctx.status = response.status;
55
54
  for (const [key, value] of response.headers.entries()) {
@@ -61,4 +60,4 @@ var createServer = ({
61
60
  });
62
61
  return app;
63
62
  };
64
- export { createServer };
63
+ export { Router, createServer };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { BuildSchemaInput } from '@ttoss/graphql-api';
2
2
  import Koa from 'koa';
3
+ export { default as Router } from '@koa/router';
3
4
 
4
5
  type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
5
6
  type CreateServerInput = {
@@ -11,6 +12,6 @@ type CreateServerInput = {
11
12
  clientId: string;
12
13
  };
13
14
  } & BuildSchemaInput;
14
- declare const createServer: ({ authenticationType, userPoolConfig, ...buildSchemaInput }: CreateServerInput) => Koa;
15
+ declare const createServer: ({ authenticationType, userPoolConfig, graphiql, ...buildSchemaInput }: CreateServerInput) => Koa;
15
16
 
16
17
  export { type AuthenticationType, type CreateServerInput, createServer };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { BuildSchemaInput } from '@ttoss/graphql-api';
2
2
  import Koa from 'koa';
3
+ export { default as Router } from '@koa/router';
3
4
 
4
5
  type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
5
6
  type CreateServerInput = {
@@ -11,6 +12,6 @@ type CreateServerInput = {
11
12
  clientId: string;
12
13
  };
13
14
  } & BuildSchemaInput;
14
- declare const createServer: ({ authenticationType, userPoolConfig, ...buildSchemaInput }: CreateServerInput) => Koa;
15
+ declare const createServer: ({ authenticationType, userPoolConfig, graphiql, ...buildSchemaInput }: CreateServerInput) => Koa;
15
16
 
16
17
  export { type AuthenticationType, type CreateServerInput, createServer };
package/dist/index.js CHANGED
@@ -38,42 +38,49 @@ 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 import_aws_jwt_verify = require("aws-jwt-verify");
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,
53
+ graphiql,
51
54
  ...buildSchemaInput
52
55
  }) => {
53
56
  const app = new import_koa.default();
57
+ const yoga = (0, import_graphql_yoga.createYoga)({
58
+ schema: (0, import_graphql_api.buildSchema)(buildSchemaInput),
59
+ graphiql,
60
+ landingPage: false,
61
+ logging: false
62
+ });
54
63
  const jwtVerifier = (() => {
55
64
  if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
56
65
  if (!userPoolConfig) {
57
66
  throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
58
67
  }
59
- return import_aws_jwt_verify.CognitoJwtVerifier.create({
68
+ return import_amazon_cognito.CognitoJwtVerifier.create({
60
69
  tokenUse: "access",
61
70
  ...userPoolConfig
62
71
  });
63
72
  }
64
73
  return null;
65
74
  })();
66
- app.use(async ctx => {
67
- const request = {
68
- body: ctx.request.body,
69
- headers: ctx.headers,
70
- method: ctx.method,
71
- query: ctx.request.query
72
- };
73
- if (request.method !== "GET" && request.headers.referer !== "http://localhost:4000/graphql") {
75
+ app.use(async (ctx, next) => {
76
+ if (ctx.path !== "/graphql") {
77
+ return next();
78
+ }
79
+ const isGraphiqlRequest = ctx.headers.accept?.includes("text/html") && graphiql;
80
+ if (!isGraphiqlRequest) {
74
81
  try {
75
82
  if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
76
- const token = request.headers.authorization?.replace("Bearer ", "");
83
+ const token = ctx.headers.authorization?.replace("Bearer ", "");
77
84
  const identity = await jwtVerifier.verify(token || "");
78
85
  ctx.identity = identity;
79
86
  }
@@ -83,13 +90,6 @@ var createServer = ({
83
90
  return;
84
91
  }
85
92
  }
86
- const operationName = request.body;
87
- const query = request.headers;
88
- const variables = request.method;
89
- const yoga = (0, import_graphql_yoga.createYoga)({
90
- schema: (0, import_graphql_api.buildSchema)(buildSchemaInput),
91
- logging: false
92
- });
93
93
  const response = await yoga.handleNodeRequest(ctx.req, ctx);
94
94
  ctx.status = response.status;
95
95
  for (const [key, value] of response.headers.entries()) {
@@ -103,5 +103,6 @@ var createServer = ({
103
103
  };
104
104
  // Annotate the CommonJS export names for ESM import in node:
105
105
  0 && (module.exports = {
106
+ Router,
106
107
  createServer
107
108
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/graphql-api-server",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "GraphQL API Server",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -20,27 +20,22 @@
20
20
  "sideEffects": false,
21
21
  "typings": "dist/index.d.ts",
22
22
  "dependencies": {
23
- "@koa/cors": "^4.0.0",
24
23
  "@koa/router": "^12.0.1",
25
- "@types/supertest": "^6.0.2",
26
- "aws-jwt-verify": "^4.0.0",
27
- "graphql-helix": "^1.13.0",
28
- "graphql-yoga": "^5.0.2",
29
- "koa": "^2.14.2",
30
- "koa-bodyparser": "^4.4.1"
24
+ "graphql-yoga": "^5.1.1",
25
+ "koa": "^2.15.0",
26
+ "@ttoss/auth-core": "^0.0.2"
31
27
  },
32
28
  "peerDependencies": {
33
29
  "graphql": "^16.6.0",
34
30
  "@ttoss/graphql-api": "^0.5.0"
35
31
  },
36
32
  "devDependencies": {
37
- "@types/koa": "^2.13.12",
38
- "@types/koa__cors": "^4.0.3",
33
+ "@types/koa": "^2.14.0",
39
34
  "@types/koa__router": "^12.0.4",
40
- "@types/koa-bodyparser": "^4.3.12",
35
+ "@types/supertest": "^6.0.2",
41
36
  "graphql": "^16.8.1",
42
37
  "jest": "^29.7.0",
43
- "supertest": "^6.3.3",
38
+ "supertest": "^6.3.4",
44
39
  "tsup": "^8.0.1",
45
40
  "@ttoss/config": "^1.31.4",
46
41
  "@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 'aws-jwt-verify';
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
 
@@ -18,10 +21,21 @@ export type CreateServerInput = {
18
21
  export const createServer = ({
19
22
  authenticationType,
20
23
  userPoolConfig,
24
+ graphiql,
21
25
  ...buildSchemaInput
22
26
  }: CreateServerInput): Koa => {
23
27
  const app = new Koa();
24
28
 
29
+ /**
30
+ * https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-koa
31
+ */
32
+ const yoga = createYoga<Koa.ParameterizedContext>({
33
+ schema: buildSchema(buildSchemaInput),
34
+ graphiql,
35
+ landingPage: false,
36
+ logging: false,
37
+ });
38
+
25
39
  const jwtVerifier = (() => {
26
40
  if (authenticationType === 'AMAZON_COGNITO_USER_POOLS') {
27
41
  if (!userPoolConfig) {
@@ -38,23 +52,27 @@ export const createServer = ({
38
52
  return null;
39
53
  })();
40
54
 
41
- app.use(async (ctx) => {
42
- const request = {
43
- body: ctx.request.body,
44
- headers: ctx.headers,
45
- method: ctx.method,
46
- query: ctx.request.query,
47
- };
55
+ app.use(async (ctx, next) => {
56
+ /**
57
+ * Check if the request is for the GraphQL endpoint.
58
+ * If not, pass it to the next middleware.
59
+ */
60
+ if (ctx.path !== '/graphql') {
61
+ return next();
62
+ }
63
+
64
+ const isGraphiqlRequest =
65
+ ctx.headers.accept?.includes('text/html') && graphiql;
48
66
 
49
- if (
50
- request.method !== 'GET' &&
51
- request.headers.referer !== 'http://localhost:4000/graphql'
52
- ) {
67
+ /**
68
+ * If the request is not a GraphiQL request, verify the JWT token, else
69
+ * set Unauthorized status code and return.
70
+ */
71
+ if (!isGraphiqlRequest) {
53
72
  try {
54
73
  if (authenticationType === 'AMAZON_COGNITO_USER_POOLS' && jwtVerifier) {
55
- const token = request.headers.authorization?.replace('Bearer ', '');
74
+ const token = ctx.headers.authorization?.replace('Bearer ', '');
56
75
  const identity = await jwtVerifier.verify(token || '');
57
-
58
76
  ctx.identity = identity;
59
77
  }
60
78
  } catch {
@@ -64,25 +82,16 @@ export const createServer = ({
64
82
  }
65
83
  }
66
84
 
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;
74
-
75
- const yoga = createYoga<Koa.ParameterizedContext>({
76
- schema: buildSchema(buildSchemaInput),
77
- logging: false,
78
- });
79
-
80
85
  const response = await yoga.handleNodeRequest(ctx.req, ctx);
81
86
 
82
- // Set status code
87
+ /**
88
+ * Set status code
89
+ */
83
90
  ctx.status = response.status;
84
91
 
85
- // Set headers
92
+ /**
93
+ * Set headers
94
+ */
86
95
  for (const [key, value] of response.headers.entries()) {
87
96
  if (ctx.status != 401) {
88
97
  ctx.append(key, value);
@@ -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
- };
package/src/typings.d.ts DELETED
@@ -1 +0,0 @@
1
- declare module 'graphql-helix';