@ttoss/graphql-api 0.5.1 → 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
@@ -369,9 +369,28 @@ const permissions = shield(
369
369
  }
370
370
  );
371
371
 
372
+ /**
373
+ * Apply middlewares to all resolvers.
374
+ */
375
+ const logInput = async (resolve, source, args, context, info) => {
376
+ console.log(`1. logInput: ${JSON.stringify(args)}`)
377
+ const result = await resolve(source, args, context, info)
378
+ console.log(`5. logInput`)
379
+ return result
380
+ }
381
+
382
+ /**
383
+ * Apply middlewares only to a specific resolver.
384
+ */
385
+ const logOnQueryMe = {
386
+ Query: {
387
+ me: logInput
388
+ }
389
+ }
390
+
372
391
  const schema = buildSchema({
373
392
  schemaComposer,
374
- middlewares; [permissions],
393
+ middlewares; [permissions, logInput, logOnQueryMe],
375
394
  })
376
395
  ```
377
396
 
package/dist/esm/index.js CHANGED
@@ -6,27 +6,7 @@ import { ObjectTypeComposer } from "graphql-compose";
6
6
 
7
7
  // src/composeWithRelay/nodeFieldConfig.ts
8
8
  import { getProjectionFromAST } from "graphql-compose";
9
-
10
- // src/composeWithRelay/globalId.ts
11
- var base64 = i => {
12
- return Buffer.from(i, "ascii").toString("base64");
13
- };
14
- var unbase64 = i => {
15
- return Buffer.from(i, "base64").toString("ascii");
16
- };
17
- var toGlobalId = (type, recordId) => {
18
- return base64([type, recordId].join(":"));
19
- };
20
- var fromGlobalId = globalId => {
21
- const unbasedGlobalId = unbase64(globalId);
22
- const [type, recordId] = unbasedGlobalId.split(":");
23
- return {
24
- type,
25
- recordId
26
- };
27
- };
28
-
29
- // src/composeWithRelay/nodeFieldConfig.ts
9
+ import { fromGlobalId } from "@ttoss/ids";
30
10
  var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
31
11
  return {
32
12
  description: "Fetches an object that has globally unique ID among all types",
@@ -37,6 +17,7 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
37
17
  description: "The globally unique ID among all types"
38
18
  }
39
19
  },
20
+ // eslint-disable-next-line max-params
40
21
  resolve: (source, args, context, info) => {
41
22
  if (!args.id || !(typeof args.id === "string")) {
42
23
  return null;
@@ -73,7 +54,9 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
73
54
  info,
74
55
  projection
75
56
  }).then(res => {
76
- if (!res) return res;
57
+ if (!res) {
58
+ return res;
59
+ }
77
60
  res.__nodeType = graphqlType;
78
61
  return res;
79
62
  });
@@ -108,6 +91,7 @@ var getNodeInterface = sc => {
108
91
  };
109
92
 
110
93
  // src/composeWithRelay/composeWithRelay.ts
94
+ import { toGlobalId } from "@ttoss/ids";
111
95
  var TypeMapForRelayNode = {};
112
96
  var composeWithRelay = tc => {
113
97
  if (!(tc instanceof ObjectTypeComposer)) {
@@ -138,7 +122,7 @@ var composeWithRelay = tc => {
138
122
  type: "ID!",
139
123
  description: "The globally unique ID among all types",
140
124
  resolve: source => {
141
- return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
125
+ return toGlobalId(tc.getTypeName(), tc.getRecordId(source).toString());
142
126
  }
143
127
  }
144
128
  });
@@ -174,4 +158,4 @@ var buildSchema = ({
174
158
  }
175
159
  return schema;
176
160
  };
177
- export { buildSchema, default2 as composeWithConnection, composeWithRelay, fromGlobalId, toGlobalId };
161
+ export { buildSchema, default2 as composeWithConnection, composeWithRelay };
package/dist/index.d.mts CHANGED
@@ -3,28 +3,14 @@ export * from 'graphql-compose';
3
3
  export { default as composeWithConnection } from 'graphql-compose-connection';
4
4
  import { GraphQLSchema } from 'graphql';
5
5
  import { IMiddleware, IMiddlewareGenerator } from 'graphql-middleware';
6
+ export { IMiddleware as Middleware } from 'graphql-middleware';
6
7
 
7
8
  declare const composeWithRelay: <TContext>(tc: ObjectTypeComposer<any, TContext>) => ObjectTypeComposer<any, TContext>;
8
9
 
9
- type ResolvedGlobalId = {
10
- type: string;
11
- recordId: string;
12
- };
13
- /**
14
- * Takes a type name and an ID specific to that type name, and returns a
15
- * "global ID" that is unique among all types.
16
- */
17
- declare const toGlobalId: (type: string, recordId: string | number) => string;
18
- /**
19
- * Takes the "global ID" created by toGlobalID, and returns the type name and ID
20
- * used to create it.
21
- */
22
- declare const fromGlobalId: (globalId: string) => ResolvedGlobalId;
23
-
24
- type BuildSchemaInput<TContext = any> = {
10
+ type BuildSchemaInput<TContext = unknown> = {
25
11
  schemaComposer: SchemaComposer<TContext>;
26
- middlewares?: (IMiddleware | IMiddlewareGenerator<any, TContext, any>)[];
12
+ middlewares?: (IMiddleware | IMiddlewareGenerator<unknown, TContext, unknown>)[];
27
13
  };
28
14
  declare const buildSchema: ({ schemaComposer, middlewares, }: BuildSchemaInput) => GraphQLSchema;
29
15
 
30
- export { type BuildSchemaInput, buildSchema, composeWithRelay, fromGlobalId, toGlobalId };
16
+ export { type BuildSchemaInput, buildSchema, composeWithRelay };
package/dist/index.d.ts CHANGED
@@ -3,28 +3,14 @@ export * from 'graphql-compose';
3
3
  export { default as composeWithConnection } from 'graphql-compose-connection';
4
4
  import { GraphQLSchema } from 'graphql';
5
5
  import { IMiddleware, IMiddlewareGenerator } from 'graphql-middleware';
6
+ export { IMiddleware as Middleware } from 'graphql-middleware';
6
7
 
7
8
  declare const composeWithRelay: <TContext>(tc: ObjectTypeComposer<any, TContext>) => ObjectTypeComposer<any, TContext>;
8
9
 
9
- type ResolvedGlobalId = {
10
- type: string;
11
- recordId: string;
12
- };
13
- /**
14
- * Takes a type name and an ID specific to that type name, and returns a
15
- * "global ID" that is unique among all types.
16
- */
17
- declare const toGlobalId: (type: string, recordId: string | number) => string;
18
- /**
19
- * Takes the "global ID" created by toGlobalID, and returns the type name and ID
20
- * used to create it.
21
- */
22
- declare const fromGlobalId: (globalId: string) => ResolvedGlobalId;
23
-
24
- type BuildSchemaInput<TContext = any> = {
10
+ type BuildSchemaInput<TContext = unknown> = {
25
11
  schemaComposer: SchemaComposer<TContext>;
26
- middlewares?: (IMiddleware | IMiddlewareGenerator<any, TContext, any>)[];
12
+ middlewares?: (IMiddleware | IMiddlewareGenerator<unknown, TContext, unknown>)[];
27
13
  };
28
14
  declare const buildSchema: ({ schemaComposer, middlewares, }: BuildSchemaInput) => GraphQLSchema;
29
15
 
30
- export { type BuildSchemaInput, buildSchema, composeWithRelay, fromGlobalId, toGlobalId };
16
+ export { type BuildSchemaInput, buildSchema, composeWithRelay };
package/dist/index.js CHANGED
@@ -41,9 +41,7 @@ var src_exports = {};
41
41
  __export(src_exports, {
42
42
  buildSchema: () => buildSchema,
43
43
  composeWithConnection: () => import_graphql_compose_connection.default,
44
- composeWithRelay: () => composeWithRelay,
45
- fromGlobalId: () => fromGlobalId,
46
- toGlobalId: () => toGlobalId
44
+ composeWithRelay: () => composeWithRelay
47
45
  });
48
46
  module.exports = __toCommonJS(src_exports);
49
47
 
@@ -52,27 +50,7 @@ var import_graphql_compose3 = require("graphql-compose");
52
50
 
53
51
  // src/composeWithRelay/nodeFieldConfig.ts
54
52
  var import_graphql_compose = require("graphql-compose");
55
-
56
- // src/composeWithRelay/globalId.ts
57
- var base64 = i => {
58
- return Buffer.from(i, "ascii").toString("base64");
59
- };
60
- var unbase64 = i => {
61
- return Buffer.from(i, "base64").toString("ascii");
62
- };
63
- var toGlobalId = (type, recordId) => {
64
- return base64([type, recordId].join(":"));
65
- };
66
- var fromGlobalId = globalId => {
67
- const unbasedGlobalId = unbase64(globalId);
68
- const [type, recordId] = unbasedGlobalId.split(":");
69
- return {
70
- type,
71
- recordId
72
- };
73
- };
74
-
75
- // src/composeWithRelay/nodeFieldConfig.ts
53
+ var import_ids = require("@ttoss/ids");
76
54
  var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
77
55
  return {
78
56
  description: "Fetches an object that has globally unique ID among all types",
@@ -83,13 +61,14 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
83
61
  description: "The globally unique ID among all types"
84
62
  }
85
63
  },
64
+ // eslint-disable-next-line max-params
86
65
  resolve: (source, args, context, info) => {
87
66
  if (!args.id || !(typeof args.id === "string")) {
88
67
  return null;
89
68
  }
90
69
  const {
91
70
  type
92
- } = fromGlobalId(args.id);
71
+ } = (0, import_ids.fromGlobalId)(args.id);
93
72
  if (!typeMapForRelayNode[type]) {
94
73
  return null;
95
74
  }
@@ -119,7 +98,9 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
119
98
  info,
120
99
  projection
121
100
  }).then(res => {
122
- if (!res) return res;
101
+ if (!res) {
102
+ return res;
103
+ }
123
104
  res.__nodeType = graphqlType;
124
105
  return res;
125
106
  });
@@ -154,6 +135,7 @@ var getNodeInterface = sc => {
154
135
  };
155
136
 
156
137
  // src/composeWithRelay/composeWithRelay.ts
138
+ var import_ids2 = require("@ttoss/ids");
157
139
  var TypeMapForRelayNode = {};
158
140
  var composeWithRelay = tc => {
159
141
  if (!(tc instanceof import_graphql_compose3.ObjectTypeComposer)) {
@@ -184,7 +166,7 @@ var composeWithRelay = tc => {
184
166
  type: "ID!",
185
167
  description: "The globally unique ID among all types",
186
168
  resolve: source => {
187
- return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
169
+ return (0, import_ids2.toGlobalId)(tc.getTypeName(), tc.getRecordId(source).toString());
188
170
  }
189
171
  }
190
172
  });
@@ -225,7 +207,5 @@ var buildSchema = ({
225
207
  buildSchema,
226
208
  composeWithConnection,
227
209
  composeWithRelay,
228
- fromGlobalId,
229
- toGlobalId,
230
210
  ...require("graphql-compose")
231
211
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/graphql-api",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "A library for building GraphQL APIs using ttoss ecosystem.",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -32,15 +32,16 @@
32
32
  ],
33
33
  "sideEffects": false,
34
34
  "dependencies": {
35
- "@graphql-codegen/core": "^4.0.0",
36
- "@graphql-codegen/typescript": "^4.0.1",
35
+ "@graphql-codegen/core": "^4.0.2",
36
+ "@graphql-codegen/typescript": "^4.0.6",
37
37
  "graphql-compose": "^9.0.10",
38
38
  "graphql-compose-connection": "^8.2.1",
39
39
  "graphql-middleware": "^6.1.35",
40
40
  "graphql-shield": "^7.6.5",
41
41
  "npmlog": "^7.0.1",
42
42
  "ts-node": "^10.9.2",
43
- "yargs": "^17.7.2"
43
+ "yargs": "^17.7.2",
44
+ "@ttoss/ids": "^0.1.1"
44
45
  },
45
46
  "peerDependencies": {
46
47
  "graphql": "^16.6.0"
@@ -1,16 +1,19 @@
1
1
  import { type GraphQLSchema } from 'graphql';
2
2
  import {
3
- type IMiddleware,
4
- type IMiddlewareGenerator,
3
+ type IMiddleware as Middleware,
4
+ type IMiddlewareGenerator as MiddlewareGenerator,
5
5
  applyMiddleware,
6
6
  } from 'graphql-middleware';
7
7
  import { type SchemaComposer } from 'graphql-compose';
8
8
 
9
- export type { IMiddleware, IMiddlewareGenerator };
9
+ export type { Middleware, MiddlewareGenerator };
10
10
 
11
- export type BuildSchemaInput<TContext = any> = {
11
+ export type BuildSchemaInput<TContext = unknown> = {
12
12
  schemaComposer: SchemaComposer<TContext>;
13
- middlewares?: (IMiddleware | IMiddlewareGenerator<any, TContext, any>)[];
13
+ middlewares?: (
14
+ | Middleware
15
+ | MiddlewareGenerator<unknown, TContext, unknown>
16
+ )[];
14
17
  };
15
18
 
16
19
  export const buildSchema = ({
@@ -30,10 +33,13 @@ export const buildSchema = ({
30
33
  /**
31
34
  * https://github.com/dimatill/graphql-middleware/issues/433#issuecomment-1170187160
32
35
  */
33
- if ((middleware as IMiddlewareGenerator<any, any, any>).generate) {
34
- return (middleware as IMiddlewareGenerator<any, any, any>).generate(
35
- schema
36
- );
36
+ if (
37
+ (middleware as MiddlewareGenerator<unknown, unknown, unknown>)
38
+ .generate
39
+ ) {
40
+ return (
41
+ middleware as MiddlewareGenerator<unknown, unknown, unknown>
42
+ ).generate(schema);
37
43
  }
38
44
 
39
45
  return middleware;
@@ -1,7 +1,8 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  import { ObjectTypeComposer } from 'graphql-compose';
2
3
  import { getNodeFieldConfig } from './nodeFieldConfig';
3
4
  import { getNodeInterface } from './nodeInterface';
4
- import { toGlobalId } from './globalId';
5
+ import { toGlobalId } from '@ttoss/ids';
5
6
 
6
7
  // all wrapped typeComposers with Relay, stored in this variable
7
8
  // for futher type resolving via NodeInterface.resolveType method
@@ -56,7 +57,7 @@ export const composeWithRelay = <TContext>(
56
57
  type: 'ID!',
57
58
  description: 'The globally unique ID among all types',
58
59
  resolve: (source) => {
59
- return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
60
+ return toGlobalId(tc.getTypeName(), tc.getRecordId(source).toString());
60
61
  },
61
62
  },
62
63
  });
@@ -4,4 +4,3 @@ import { schemaComposer } from 'graphql-compose';
4
4
  composeWithRelay(schemaComposer.Query);
5
5
 
6
6
  export { composeWithRelay };
7
- export { fromGlobalId, toGlobalId } from './globalId';
@@ -1,9 +1,10 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  import {
2
3
  type InterfaceTypeComposer,
3
4
  type ObjectTypeComposerFieldConfigDefinition,
4
5
  getProjectionFromAST,
5
6
  } from 'graphql-compose';
6
- import { fromGlobalId } from './globalId';
7
+ import { fromGlobalId } from '@ttoss/ids';
7
8
  import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql';
8
9
  import type { ObjectTypeComposer, Resolver } from 'graphql-compose';
9
10
 
@@ -29,6 +30,7 @@ export const getNodeFieldConfig = (
29
30
  description: 'The globally unique ID among all types',
30
31
  },
31
32
  },
33
+ // eslint-disable-next-line max-params
32
34
  resolve: (
33
35
  source: any,
34
36
  args: { [argName: string]: any },
@@ -70,7 +72,9 @@ export const getNodeFieldConfig = (
70
72
  projection,
71
73
  })
72
74
  .then((res: any) => {
73
- if (!res) return res;
75
+ if (!res) {
76
+ return res;
77
+ }
74
78
  res.__nodeType = graphqlType;
75
79
  return res;
76
80
  });
package/src/index.ts CHANGED
@@ -1,4 +1,8 @@
1
- export { composeWithRelay, toGlobalId, fromGlobalId } from './composeWithRelay';
1
+ export { composeWithRelay } from './composeWithRelay';
2
2
  export { default as composeWithConnection } from 'graphql-compose-connection';
3
3
  export * from 'graphql-compose';
4
- export { buildSchema, type BuildSchemaInput } from './buildSchema';
4
+ export {
5
+ buildSchema,
6
+ type BuildSchemaInput,
7
+ type Middleware,
8
+ } from './buildSchema';
@@ -1,32 +0,0 @@
1
- export type Base64String = string;
2
-
3
- export type ResolvedGlobalId = {
4
- type: string;
5
- recordId: string;
6
- };
7
-
8
- export const base64 = (i: string): Base64String => {
9
- return Buffer.from(i, 'ascii').toString('base64');
10
- };
11
-
12
- export const unbase64 = (i: Base64String): string => {
13
- return Buffer.from(i, 'base64').toString('ascii');
14
- };
15
-
16
- /**
17
- * Takes a type name and an ID specific to that type name, and returns a
18
- * "global ID" that is unique among all types.
19
- */
20
- export const toGlobalId = (type: string, recordId: string | number): string => {
21
- return base64([type, recordId].join(':'));
22
- };
23
-
24
- /**
25
- * Takes the "global ID" created by toGlobalID, and returns the type name and ID
26
- * used to create it.
27
- */
28
- export const fromGlobalId = (globalId: string): ResolvedGlobalId => {
29
- const unbasedGlobalId = unbase64(globalId);
30
- const [type, recordId] = unbasedGlobalId.split(':');
31
- return { type, recordId };
32
- };