@ttoss/graphql-api-server 0.1.0 → 0.2.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/README.md CHANGED
@@ -30,8 +30,13 @@ server.listen(3000, () => {
30
30
 
31
31
  The server supports the following authentication types:
32
32
 
33
+ - No authentication
33
34
  - [`AWS_COGNITO_USER_POOLS`](#aws_cognito_user_pools)
34
35
 
36
+ ### No authentication
37
+
38
+ You can disable authentication by not setting the `authenticationType` option.
39
+
35
40
  ### AWS_COGNITO_USER_POOLS
36
41
 
37
42
  You need to set Cognito user pool [ID token](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-the-id-token.html) or [access token](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-the-access-token.html) to the `Authorization` header.
@@ -54,3 +59,41 @@ server.listen(3000, () => {
54
59
  console.log('Server listening on port 3000');
55
60
  });
56
61
  ```
62
+
63
+ ### Middlewares
64
+
65
+ You can add middlewares compatible with [`graphql-middleware`](https://github.com/dimatill/graphql-middleware) to the server using the `middlewares` option.
66
+
67
+ ```ts
68
+ import { createServer } from '@ttoss/graphql-api-server';
69
+ import { schemaComposer } from './schemaComposer';
70
+ import { allow, deny, shield } from 'graphql-shield';
71
+
72
+ const NotAuthorizedError = new Error('Not authorized!');
73
+ /**
74
+ * The error name is the same value `errorType` on GraphQL errors response.
75
+ */
76
+ NotAuthorizedError.name = 'NotAuthorizedError';
77
+
78
+ const permissions = shield(
79
+ {
80
+ Query: {
81
+ '*': deny,
82
+ author: allow,
83
+ },
84
+ Author: {
85
+ id: allow,
86
+ name: allow,
87
+ },
88
+ },
89
+ {
90
+ fallbackRule: deny,
91
+ fallbackError: NotAuthorizedError,
92
+ }
93
+ );
94
+
95
+ const server = createServer({
96
+ schemaComposer,
97
+ middlewares: [permissions],
98
+ });
99
+ ```
package/dist/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
 
3
3
  // src/index.ts
4
+ import { buildSchema } from "@ttoss/graphql-api";
4
5
  import { CognitoJwtVerifier } from "aws-jwt-verify";
5
6
  import { getGraphQLParameters, processRequest, renderGraphiQL, sendResult, shouldRenderGraphiQL } from "graphql-helix";
6
7
  import Koa from "koa";
@@ -8,10 +9,10 @@ import Router from "@koa/router";
8
9
  import bodyParser from "koa-bodyparser";
9
10
  import cors from "@koa/cors";
10
11
  var createServer = ({
11
- schemaComposer,
12
12
  graphiql = false,
13
13
  authenticationType,
14
- userPoolConfig
14
+ userPoolConfig,
15
+ ...buildSchemaInput
15
16
  }) => {
16
17
  const server = new Koa();
17
18
  const router = new Router();
@@ -61,7 +62,7 @@ var createServer = ({
61
62
  query,
62
63
  variables,
63
64
  request,
64
- schema: schemaComposer.buildSchema(),
65
+ schema: buildSchema(buildSchemaInput),
65
66
  contextFactory: () => {
66
67
  return {
67
68
  identity: ctx.identity
package/dist/index.d.ts CHANGED
@@ -1,17 +1,17 @@
1
- import { SchemaComposer } from '@ttoss/graphql-api';
1
+ import { BuildSchemaInput } from '@ttoss/graphql-api';
2
2
  import Koa from 'koa';
3
3
  export { default as Router } from '@koa/router';
4
4
 
5
5
  type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
6
- declare const createServer: ({ schemaComposer, graphiql, authenticationType, userPoolConfig, }: {
7
- schemaComposer: SchemaComposer<any>;
8
- graphiql?: boolean | undefined;
9
- authenticationType?: "AMAZON_COGNITO_USER_POOLS" | undefined;
6
+ type CreateServerInput = {
7
+ graphiql?: boolean;
8
+ authenticationType?: AuthenticationType;
10
9
  userPoolConfig?: {
11
10
  userPoolId: string;
12
- tokenUse?: "access" | "id" | undefined;
11
+ tokenUse?: 'access' | 'id';
13
12
  clientId: string;
14
- } | undefined;
15
- }) => Koa;
13
+ };
14
+ } & BuildSchemaInput;
15
+ declare const createServer: ({ graphiql, authenticationType, userPoolConfig, ...buildSchemaInput }: CreateServerInput) => Koa;
16
16
 
17
- export { AuthenticationType, createServer };
17
+ export { AuthenticationType, CreateServerInput, createServer };
package/dist/index.js CHANGED
@@ -42,6 +42,7 @@ __export(src_exports, {
42
42
  createServer: () => createServer
43
43
  });
44
44
  module.exports = __toCommonJS(src_exports);
45
+ var import_graphql_api = require("@ttoss/graphql-api");
45
46
  var import_aws_jwt_verify = require("aws-jwt-verify");
46
47
  var import_graphql_helix = require("graphql-helix");
47
48
  var import_koa = __toESM(require("koa"));
@@ -49,10 +50,10 @@ var import_router = __toESM(require("@koa/router"));
49
50
  var import_koa_bodyparser = __toESM(require("koa-bodyparser"));
50
51
  var import_cors = __toESM(require("@koa/cors"));
51
52
  var createServer = ({
52
- schemaComposer,
53
53
  graphiql = false,
54
54
  authenticationType,
55
- userPoolConfig
55
+ userPoolConfig,
56
+ ...buildSchemaInput
56
57
  }) => {
57
58
  const server = new import_koa.default();
58
59
  const router = new import_router.default();
@@ -102,7 +103,7 @@ var createServer = ({
102
103
  query,
103
104
  variables,
104
105
  request,
105
- schema: schemaComposer.buildSchema(),
106
+ schema: (0, import_graphql_api.buildSchema)(buildSchemaInput),
106
107
  contextFactory: () => {
107
108
  return {
108
109
  identity: ctx.identity
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/graphql-api-server",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "GraphQL API Server",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -24,8 +24,7 @@
24
24
  },
25
25
  "peerDependencies": {
26
26
  "graphql": "^16.6.0",
27
- "graphql-compose": "^9.0.10",
28
- "@ttoss/graphql-api": "^0.2.0"
27
+ "@ttoss/graphql-api": "^0.3.0"
29
28
  },
30
29
  "devDependencies": {
31
30
  "@types/koa": "^2.13.6",
@@ -33,11 +32,10 @@
33
32
  "@types/koa__router": "^12.0.0",
34
33
  "@types/koa-bodyparser": "^4.3.10",
35
34
  "graphql": "^16.6.0",
36
- "graphql-compose": "^9.0.10",
37
35
  "jest": "^29.5.0",
38
36
  "tsup": "^6.7.0",
39
37
  "@ttoss/config": "^1.30.0",
40
- "@ttoss/graphql-api": "^0.2.0"
38
+ "@ttoss/graphql-api": "^0.3.0"
41
39
  },
42
40
  "keywords": [
43
41
  "api",
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
+ import { type BuildSchemaInput, buildSchema } from '@ttoss/graphql-api';
1
2
  import { CognitoJwtVerifier } from 'aws-jwt-verify';
2
- import { type SchemaComposer } from '@ttoss/graphql-api';
3
3
  import {
4
4
  getGraphQLParameters,
5
5
  processRequest,
@@ -16,13 +16,7 @@ export { Router };
16
16
 
17
17
  export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
18
18
 
19
- export const createServer = ({
20
- schemaComposer,
21
- graphiql = false,
22
- authenticationType,
23
- userPoolConfig,
24
- }: {
25
- schemaComposer: SchemaComposer<any>;
19
+ export type CreateServerInput = {
26
20
  graphiql?: boolean;
27
21
  authenticationType?: AuthenticationType;
28
22
  userPoolConfig?: {
@@ -30,7 +24,14 @@ export const createServer = ({
30
24
  tokenUse?: 'access' | 'id';
31
25
  clientId: string;
32
26
  };
33
- }): Koa => {
27
+ } & BuildSchemaInput;
28
+
29
+ export const createServer = ({
30
+ graphiql = false,
31
+ authenticationType,
32
+ userPoolConfig,
33
+ ...buildSchemaInput
34
+ }: CreateServerInput): Koa => {
34
35
  const server = new Koa();
35
36
 
36
37
  const router = new Router();
@@ -91,7 +92,7 @@ export const createServer = ({
91
92
  query,
92
93
  variables,
93
94
  request,
94
- schema: schemaComposer.buildSchema(),
95
+ schema: buildSchema(buildSchemaInput),
95
96
  contextFactory: () => {
96
97
  return {
97
98
  identity: ctx.identity,