@ttoss/graphql-api-server 0.5.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 CHANGED
@@ -5,7 +5,7 @@ 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
11
  ## Quickstart
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 "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,
@@ -61,4 +62,4 @@ var createServer = ({
61
62
  });
62
63
  return app;
63
64
  };
64
- export { createServer };
65
+ 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 = {
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 = {
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 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,
@@ -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 import_aws_jwt_verify.CognitoJwtVerifier.create({
61
+ return import_amazon_cognito.CognitoJwtVerifier.create({
60
62
  tokenUse: "access",
61
63
  ...userPoolConfig
62
64
  });
@@ -103,5 +105,6 @@ var createServer = ({
103
105
  };
104
106
  // Annotate the CommonJS export names for ESM import in node:
105
107
  0 && (module.exports = {
108
+ Router,
106
109
  createServer
107
110
  });
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.1",
4
4
  "description": "GraphQL API Server",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -20,27 +20,26 @@
20
20
  "sideEffects": false,
21
21
  "typings": "dist/index.d.ts",
22
22
  "dependencies": {
23
- "@koa/cors": "^4.0.0",
23
+ "@koa/cors": "^5.0.0",
24
24
  "@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"
25
+ "graphql-yoga": "^5.1.1",
26
+ "koa": "^2.15.0",
27
+ "koa-bodyparser": "^4.4.1",
28
+ "@ttoss/auth-core": "^0.0.2"
31
29
  },
32
30
  "peerDependencies": {
33
31
  "graphql": "^16.6.0",
34
32
  "@ttoss/graphql-api": "^0.5.0"
35
33
  },
36
34
  "devDependencies": {
37
- "@types/koa": "^2.13.12",
38
- "@types/koa__cors": "^4.0.3",
35
+ "@types/koa": "^2.14.0",
36
+ "@types/koa__cors": "^5.0.0",
39
37
  "@types/koa__router": "^12.0.4",
40
38
  "@types/koa-bodyparser": "^4.3.12",
39
+ "@types/supertest": "^6.0.2",
41
40
  "graphql": "^16.8.1",
42
41
  "jest": "^29.7.0",
43
- "supertest": "^6.3.3",
42
+ "supertest": "^6.3.4",
44
43
  "tsup": "^8.0.1",
45
44
  "@ttoss/config": "^1.31.4",
46
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 '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
 
@@ -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
- };