@webiny/handler-graphql 0.0.0-unstable.78f581c1d2 → 0.0.0-unstable.7be00a75a9

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.
Files changed (102) hide show
  1. package/README.md +9 -5
  2. package/ResolverDecoration.d.ts +6 -0
  3. package/ResolverDecoration.js +23 -0
  4. package/ResolverDecoration.js.map +1 -0
  5. package/builtInTypes/AnyScalar.d.ts +1 -1
  6. package/builtInTypes/AnyScalar.js +9 -18
  7. package/builtInTypes/AnyScalar.js.map +1 -1
  8. package/builtInTypes/DateScalar.d.ts +1 -1
  9. package/builtInTypes/DateScalar.js +16 -32
  10. package/builtInTypes/DateScalar.js.map +1 -1
  11. package/builtInTypes/DateTimeScalar.d.ts +1 -1
  12. package/builtInTypes/DateTimeScalar.js +16 -32
  13. package/builtInTypes/DateTimeScalar.js.map +1 -1
  14. package/builtInTypes/DateTimeZScalar.d.ts +1 -1
  15. package/builtInTypes/DateTimeZScalar.js +27 -58
  16. package/builtInTypes/DateTimeZScalar.js.map +1 -1
  17. package/builtInTypes/IconScalar.d.ts +10 -0
  18. package/builtInTypes/IconScalar.js +65 -0
  19. package/builtInTypes/IconScalar.js.map +1 -0
  20. package/builtInTypes/JsonScalar.d.ts +1 -1
  21. package/builtInTypes/JsonScalar.js +4 -10
  22. package/builtInTypes/JsonScalar.js.map +1 -1
  23. package/builtInTypes/LongScalar.d.ts +2 -1
  24. package/builtInTypes/LongScalar.js +38 -9
  25. package/builtInTypes/LongScalar.js.map +1 -1
  26. package/builtInTypes/NumberScalar.d.ts +1 -1
  27. package/builtInTypes/NumberScalar.js +35 -58
  28. package/builtInTypes/NumberScalar.js.map +1 -1
  29. package/builtInTypes/RefInputScalar.d.ts +1 -1
  30. package/builtInTypes/RefInputScalar.js +48 -60
  31. package/builtInTypes/RefInputScalar.js.map +1 -1
  32. package/builtInTypes/TimeScalar.d.ts +1 -1
  33. package/builtInTypes/TimeScalar.js +31 -62
  34. package/builtInTypes/TimeScalar.js.map +1 -1
  35. package/builtInTypes/index.d.ts +10 -9
  36. package/builtInTypes/index.js +10 -122
  37. package/createGraphQLHandler.d.ts +3 -3
  38. package/createGraphQLHandler.js +77 -55
  39. package/createGraphQLHandler.js.map +1 -1
  40. package/createGraphQLSchema.d.ts +4 -2
  41. package/createGraphQLSchema.js +67 -54
  42. package/createGraphQLSchema.js.map +1 -1
  43. package/createRequestBody.d.ts +2 -0
  44. package/createRequestBody.js +28 -0
  45. package/createRequestBody.js.map +1 -0
  46. package/createResolverDecorator.d.ts +2 -0
  47. package/createResolverDecorator.js +4 -0
  48. package/createResolverDecorator.js.map +1 -0
  49. package/debugPlugins.d.ts +2 -2
  50. package/debugPlugins.js +28 -45
  51. package/debugPlugins.js.map +1 -1
  52. package/errors.js +7 -17
  53. package/errors.js.map +1 -1
  54. package/exports/api/graphql.d.ts +2 -0
  55. package/exports/api/graphql.js +2 -0
  56. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.d.ts +16 -0
  57. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js +57 -0
  58. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js.map +1 -0
  59. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.d.ts +13 -0
  60. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js +35 -0
  61. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js.map +1 -0
  62. package/features/GraphQLSchemaBuilder/abstractions.d.ts +34 -0
  63. package/features/GraphQLSchemaBuilder/abstractions.js +6 -0
  64. package/features/GraphQLSchemaBuilder/abstractions.js.map +1 -0
  65. package/features/GraphQLSchemaBuilder/feature.d.ts +4 -0
  66. package/features/GraphQLSchemaBuilder/feature.js +11 -0
  67. package/features/GraphQLSchemaBuilder/feature.js.map +1 -0
  68. package/graphql/abstractions.core.d.ts +11 -0
  69. package/graphql/abstractions.core.js +5 -0
  70. package/graphql/abstractions.core.js.map +1 -0
  71. package/graphql/abstractions.d.ts +2 -0
  72. package/graphql/abstractions.js +2 -0
  73. package/graphql/abstractions.public.d.ts +18 -0
  74. package/graphql/abstractions.public.js +5 -0
  75. package/graphql/abstractions.public.js.map +1 -0
  76. package/index.d.ts +10 -5
  77. package/index.js +12 -57
  78. package/index.js.map +1 -1
  79. package/interceptConsole.js +31 -39
  80. package/interceptConsole.js.map +1 -1
  81. package/package.json +32 -29
  82. package/plugins/GraphQLSchemaPlugin.d.ts +13 -5
  83. package/plugins/GraphQLSchemaPlugin.js +23 -29
  84. package/plugins/GraphQLSchemaPlugin.js.map +1 -1
  85. package/plugins/index.d.ts +1 -1
  86. package/plugins/index.js +1 -18
  87. package/processRequestBody.d.ts +4 -5
  88. package/processRequestBody.js +35 -42
  89. package/processRequestBody.js.map +1 -1
  90. package/responses.d.ts +11 -1
  91. package/responses.js +44 -92
  92. package/responses.js.map +1 -1
  93. package/types.d.ts +20 -13
  94. package/types.js +0 -5
  95. package/utils/index.d.ts +1 -0
  96. package/utils/index.js +1 -0
  97. package/utils/resolve.d.ts +21 -0
  98. package/utils/resolve.js +20 -0
  99. package/utils/resolve.js.map +1 -0
  100. package/builtInTypes/index.js.map +0 -1
  101. package/plugins/index.js.map +0 -1
  102. package/types.js.map +0 -1
@@ -1,60 +1,82 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = void 0;
9
-
10
- var _boolean = require("boolean");
11
-
12
- var _createGraphQLSchema = require("./createGraphQLSchema");
13
-
14
- var _debugPlugins = _interopRequireDefault(require("./debugPlugins"));
15
-
16
- var _processRequestBody = _interopRequireDefault(require("./processRequestBody"));
17
-
18
- var _handler = require("@webiny/handler");
19
-
20
- const DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year
21
-
22
- /**
23
- * TODO Until we figure out how to better convert incoming body, we will leave it as any.
24
- */
25
-
26
- const createRequestBody = body => {
27
- return typeof body === "string" ? JSON.parse(body) : body;
1
+ import { toBoolean } from "@webiny/stdlib";
2
+ import { RoutePlugin } from "@webiny/handler";
3
+ import _webiny_error from "@webiny/error";
4
+ import { createGraphQLSchema, getSchemaPlugins } from "./createGraphQLSchema.js";
5
+ import debugPlugins from "./debugPlugins.js";
6
+ import { processRequestBody } from "./processRequestBody.js";
7
+ import { createRequestBody } from "./createRequestBody.js";
8
+ const DEFAULT_CACHE_MAX_AGE = 30758400;
9
+ const createCacheKey = (context)=>{
10
+ const plugins = getSchemaPlugins(context);
11
+ const tenant = context.tenancy?.getCurrentTenant();
12
+ return [
13
+ tenant ? `tenant:${tenant.id}` : null,
14
+ plugins.length.toString()
15
+ ].filter(Boolean).join("#");
28
16
  };
29
-
30
- var _default = (options = {}) => {
31
- let schema = undefined;
32
- const debug = (0, _boolean.boolean)(options.debug);
33
- const path = (options === null || options === void 0 ? void 0 : options.path) || "/graphql";
34
- const route = new _handler.RoutePlugin(async ({
35
- onPost,
36
- onOptions,
37
- context
38
- }) => {
39
- onOptions(path, async (_, reply) => {
40
- return reply.status(204).headers({
41
- "Cache-Control": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`
42
- }).send({});
17
+ const formatErrorPayload = (error)=>{
18
+ if (error instanceof _webiny_error) return JSON.stringify({
19
+ type: "CoreGraphQLWebinyError",
20
+ message: error.message,
21
+ code: error.code,
22
+ data: error.data,
23
+ stack: error.stack
43
24
  });
44
- onPost(path, async (request, reply) => {
45
- if (!schema) {
46
- schema = (0, _createGraphQLSchema.createGraphQLSchema)(context);
47
- }
48
-
49
- const body = createRequestBody(request.body);
50
- const result = await (0, _processRequestBody.default)(body, schema, context);
51
- return reply.status(200).send(result);
25
+ return JSON.stringify({
26
+ type: "Error",
27
+ name: error.name,
28
+ message: error.message,
29
+ stack: error.stack
30
+ });
31
+ };
32
+ const createGraphQLHandler = (options = {})=>{
33
+ let schema;
34
+ let cacheKey;
35
+ const debug = toBoolean(options.debug);
36
+ const path = options?.path || "/graphql";
37
+ const route = new RoutePlugin(async ({ onPost, onOptions, context })=>{
38
+ onOptions(path, async (_, reply)=>reply.status(204).headers({
39
+ "Cache-Control": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`
40
+ }).send({}).hijack());
41
+ onPost(path, async (request, reply)=>{
42
+ const contextCacheKey = createCacheKey(context);
43
+ if (!schema || cacheKey !== contextCacheKey) try {
44
+ schema = await createGraphQLSchema(context);
45
+ cacheKey = contextCacheKey;
46
+ } catch (ex) {
47
+ return reply.code(500).send(formatErrorPayload(ex));
48
+ }
49
+ let body;
50
+ try {
51
+ body = createRequestBody(request.body);
52
+ } catch (ex) {
53
+ console.error("Error while creating the body request.");
54
+ console.error(formatErrorPayload(ex));
55
+ throw ex;
56
+ }
57
+ try {
58
+ const result = await processRequestBody(body, schema, context);
59
+ if (reply.sent) {
60
+ console.warn("Reply already sent, cannot send the result (handler-graphql).");
61
+ return reply;
62
+ }
63
+ return reply.status(200).send(result);
64
+ } catch (ex) {
65
+ console.error("Error while processing the body request.");
66
+ console.error(formatErrorPayload(ex));
67
+ throw ex;
68
+ }
69
+ });
52
70
  });
53
- });
54
- route.name = "handler.graphql.route.default";
55
- return [...(debug ? (0, _debugPlugins.default)() : []), {
56
- type: "wcp-telemetry-tracker"
57
- }, route];
71
+ route.name = "handler.graphql.route.default";
72
+ return [
73
+ ...debug ? debugPlugins() : [],
74
+ {
75
+ type: "wcp-telemetry-tracker"
76
+ },
77
+ route
78
+ ];
58
79
  };
80
+ export default createGraphQLHandler;
59
81
 
60
- exports.default = _default;
82
+ //# sourceMappingURL=createGraphQLHandler.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["DEFAULT_CACHE_MAX_AGE","createRequestBody","body","JSON","parse","options","schema","undefined","debug","boolean","path","route","RoutePlugin","onPost","onOptions","context","_","reply","status","headers","send","request","createGraphQLSchema","result","processRequestBody","name","debugPlugins","type"],"sources":["createGraphQLHandler.ts"],"sourcesContent":["import { boolean } from \"boolean\";\nimport { HandlerGraphQLOptions } from \"./types\";\nimport { createGraphQLSchema } from \"./createGraphQLSchema\";\nimport { PluginCollection } from \"@webiny/plugins/types\";\nimport debugPlugins from \"./debugPlugins\";\nimport processRequestBody from \"./processRequestBody\";\nimport { GraphQLSchema } from \"graphql\";\nimport { RoutePlugin } from \"@webiny/handler\";\n\nconst DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year\n\n/**\n * TODO Until we figure out how to better convert incoming body, we will leave it as any.\n */\nconst createRequestBody = (body: any): any => {\n return typeof body === \"string\" ? JSON.parse(body) : body;\n};\n\nexport default (options: HandlerGraphQLOptions = {}): PluginCollection => {\n let schema: GraphQLSchema | undefined = undefined;\n\n const debug = boolean(options.debug);\n\n const path = options?.path || \"/graphql\";\n\n const route = new RoutePlugin(async ({ onPost, onOptions, context }) => {\n onOptions(path, async (_, reply) => {\n return reply\n .status(204)\n .headers({\n \"Cache-Control\": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`\n })\n .send({});\n });\n onPost(path, async (request, reply) => {\n if (!schema) {\n schema = createGraphQLSchema(context);\n }\n const body = createRequestBody(request.body);\n const result = await processRequestBody(body, schema, context);\n return reply.status(200).send(result);\n });\n });\n\n route.name = \"handler.graphql.route.default\";\n\n return [\n ...(debug ? debugPlugins() : []),\n {\n type: \"wcp-telemetry-tracker\"\n },\n route\n ];\n};\n"],"mappings":";;;;;;;;;AAAA;;AAEA;;AAEA;;AACA;;AAEA;;AAEA,MAAMA,qBAAqB,GAAG,QAA9B,C,CAAwC;;AAExC;AACA;AACA;;AACA,MAAMC,iBAAiB,GAAIC,IAAD,IAAoB;EAC1C,OAAO,OAAOA,IAAP,KAAgB,QAAhB,GAA2BC,IAAI,CAACC,KAAL,CAAWF,IAAX,CAA3B,GAA8CA,IAArD;AACH,CAFD;;eAIe,CAACG,OAA8B,GAAG,EAAlC,KAA2D;EACtE,IAAIC,MAAiC,GAAGC,SAAxC;EAEA,MAAMC,KAAK,GAAG,IAAAC,gBAAA,EAAQJ,OAAO,CAACG,KAAhB,CAAd;EAEA,MAAME,IAAI,GAAG,CAAAL,OAAO,SAAP,IAAAA,OAAO,WAAP,YAAAA,OAAO,CAAEK,IAAT,KAAiB,UAA9B;EAEA,MAAMC,KAAK,GAAG,IAAIC,oBAAJ,CAAgB,OAAO;IAAEC,MAAF;IAAUC,SAAV;IAAqBC;EAArB,CAAP,KAA0C;IACpED,SAAS,CAACJ,IAAD,EAAO,OAAOM,CAAP,EAAUC,KAAV,KAAoB;MAChC,OAAOA,KAAK,CACPC,MADE,CACK,GADL,EAEFC,OAFE,CAEM;QACL,iBAAkB,mBAAkBnB,qBAAsB;MADrD,CAFN,EAKFoB,IALE,CAKG,EALH,CAAP;IAMH,CAPQ,CAAT;IAQAP,MAAM,CAACH,IAAD,EAAO,OAAOW,OAAP,EAAgBJ,KAAhB,KAA0B;MACnC,IAAI,CAACX,MAAL,EAAa;QACTA,MAAM,GAAG,IAAAgB,wCAAA,EAAoBP,OAApB,CAAT;MACH;;MACD,MAAMb,IAAI,GAAGD,iBAAiB,CAACoB,OAAO,CAACnB,IAAT,CAA9B;MACA,MAAMqB,MAAM,GAAG,MAAM,IAAAC,2BAAA,EAAmBtB,IAAnB,EAAyBI,MAAzB,EAAiCS,OAAjC,CAArB;MACA,OAAOE,KAAK,CAACC,MAAN,CAAa,GAAb,EAAkBE,IAAlB,CAAuBG,MAAvB,CAAP;IACH,CAPK,CAAN;EAQH,CAjBa,CAAd;EAmBAZ,KAAK,CAACc,IAAN,GAAa,+BAAb;EAEA,OAAO,CACH,IAAIjB,KAAK,GAAG,IAAAkB,qBAAA,GAAH,GAAoB,EAA7B,CADG,EAEH;IACIC,IAAI,EAAE;EADV,CAFG,EAKHhB,KALG,CAAP;AAOH,C"}
1
+ {"version":3,"file":"createGraphQLHandler.js","sources":["../src/createGraphQLHandler.ts"],"sourcesContent":["import { toBoolean } from \"@webiny/stdlib\";\nimport type { GraphQLSchema } from \"graphql\";\nimport type { Context } from \"@webiny/handler\";\nimport { RoutePlugin } from \"@webiny/handler\";\nimport WebinyError from \"@webiny/error\";\nimport type { Plugin } from \"@webiny/plugins/types.js\";\nimport type { GraphQLRequestBody, HandlerGraphQLOptions } from \"./types.js\";\nimport { createGraphQLSchema, getSchemaPlugins } from \"./createGraphQLSchema.js\";\nimport debugPlugins from \"./debugPlugins.js\";\nimport { processRequestBody } from \"./processRequestBody.js\";\nimport { createRequestBody } from \"~/createRequestBody.js\";\n\nconst DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year\n\nconst createCacheKey = (context: Context) => {\n const plugins = getSchemaPlugins(context);\n // TODO: in the near future, we have to assign a fixed name to every\n // TODO: GraphQLSchema plugin, to be able to create a reliable cache key.\n\n // TODO: `getCurrentTenant` should be injected as a parameter.\n // @ts-expect-error TODO: We should not be accessing `context` like this here.\n const tenant = context.tenancy?.getCurrentTenant();\n\n return [tenant ? `tenant:${tenant.id}` : null, plugins.length.toString()]\n .filter(Boolean)\n .join(\"#\");\n};\n\nconst formatErrorPayload = (error: Error): string => {\n if (error instanceof WebinyError) {\n return JSON.stringify({\n type: \"CoreGraphQLWebinyError\",\n message: error.message,\n code: error.code,\n data: error.data,\n stack: error.stack\n });\n }\n\n return JSON.stringify({\n type: \"Error\",\n name: error.name,\n message: error.message,\n stack: error.stack\n });\n};\n\nexport default (options: HandlerGraphQLOptions = {}): Plugin[] => {\n let schema: GraphQLSchema | undefined = undefined;\n let cacheKey: string | undefined = undefined;\n\n const debug = toBoolean(options.debug);\n\n const path = options?.path || \"/graphql\";\n\n const route = new RoutePlugin(async ({ onPost, onOptions, context }) => {\n onOptions(path, async (_, reply) => {\n return reply\n .status(204)\n .headers({\n \"Cache-Control\": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`\n })\n .send({})\n .hijack();\n });\n onPost(path, async (request, reply) => {\n const contextCacheKey = createCacheKey(context as Context);\n if (!schema || cacheKey !== contextCacheKey) {\n try {\n schema = await createGraphQLSchema(context);\n cacheKey = contextCacheKey;\n } catch (ex) {\n return reply.code(500).send(formatErrorPayload(ex));\n }\n }\n let body: GraphQLRequestBody | GraphQLRequestBody[];\n try {\n body = createRequestBody(request.body);\n } catch (ex) {\n console.error(`Error while creating the body request.`);\n console.error(formatErrorPayload(ex));\n throw ex;\n }\n try {\n const result = await processRequestBody(body, schema, context);\n /**\n * IMPORTANT! Do not send anything if reply was already sent.\n */\n if (reply.sent) {\n console.warn(\"Reply already sent, cannot send the result (handler-graphql).\");\n return reply;\n }\n return reply.status(200).send(result);\n } catch (ex) {\n console.error(`Error while processing the body request.`);\n console.error(formatErrorPayload(ex));\n throw ex;\n }\n });\n });\n\n route.name = \"handler.graphql.route.default\";\n\n return [\n ...(debug ? debugPlugins() : []),\n {\n type: \"wcp-telemetry-tracker\"\n },\n route\n ];\n};\n"],"names":["DEFAULT_CACHE_MAX_AGE","createCacheKey","context","plugins","getSchemaPlugins","tenant","Boolean","formatErrorPayload","error","WebinyError","JSON","options","schema","cacheKey","debug","toBoolean","path","route","RoutePlugin","onPost","onOptions","_","reply","request","contextCacheKey","createGraphQLSchema","ex","body","createRequestBody","console","result","processRequestBody","debugPlugins"],"mappings":";;;;;;;AAYA,MAAMA,wBAAwB;AAE9B,MAAMC,iBAAiB,CAACC;IACpB,MAAMC,UAAUC,iBAAiBF;IAMjC,MAAMG,SAASH,QAAQ,OAAO,EAAE;IAEhC,OAAO;QAACG,SAAS,CAAC,OAAO,EAAEA,OAAO,EAAE,EAAE,GAAG;QAAMF,QAAQ,MAAM,CAAC,QAAQ;KAAG,CACpE,MAAM,CAACG,SACP,IAAI,CAAC;AACd;AAEA,MAAMC,qBAAqB,CAACC;IACxB,IAAIA,iBAAiBC,eACjB,OAAOC,KAAK,SAAS,CAAC;QAClB,MAAM;QACN,SAASF,MAAM,OAAO;QACtB,MAAMA,MAAM,IAAI;QAChB,MAAMA,MAAM,IAAI;QAChB,OAAOA,MAAM,KAAK;IACtB;IAGJ,OAAOE,KAAK,SAAS,CAAC;QAClB,MAAM;QACN,MA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,GAAG;IAEb,OAAO;WACCH,QAAQkB,iBAAiB,EAAE;QAC/B;YACI,MAAM;QACV;QACAf;KACH;AACL"}
@@ -1,2 +1,4 @@
1
- import { Context } from "@webiny/api/types";
2
- export declare const createGraphQLSchema: (context: Context) => import("graphql").GraphQLSchema;
1
+ import type { Context } from "@webiny/api/types.js";
2
+ import type { GraphQLSchemaPlugin } from "./plugins/index.js";
3
+ export declare const getSchemaPlugins: (context: Context) => import("@webiny/plugins/PluginsContainer.js").WithName<GraphQLSchemaPlugin<Context>>[];
4
+ export declare const createGraphQLSchema: (context: Context) => Promise<import("graphql").GraphQLSchema>;
@@ -1,68 +1,81 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.createGraphQLSchema = void 0;
9
-
10
- var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
-
12
- var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
13
-
14
- var _schema = require("@graphql-tools/schema");
15
-
16
- var _builtInTypes = require("./builtInTypes");
17
-
18
- const createGraphQLSchema = context => {
19
- const scalars = context.plugins.byType("graphql-scalar").map(item => item.scalar);
20
- const typeDefs = [(0, _graphqlTag.default)`
1
+ import graphql_tag from "graphql-tag";
2
+ import { makeExecutableSchema } from "@graphql-tools/schema";
3
+ import { mergeResolvers } from "@graphql-tools/merge";
4
+ import { AnyScalar, DateScalar, DateTimeScalar, IconScalar, JsonScalar, LongScalar, NumberScalar, RefInputScalar, TimeScalar } from "./builtInTypes/index.js";
5
+ import { ResolverDecoration } from "./ResolverDecoration.js";
6
+ import { GraphQLSchemaComposer } from "./features/GraphQLSchemaBuilder/abstractions.js";
7
+ import { GraphQLSchemaComposerFeature } from "./features/GraphQLSchemaBuilder/feature.js";
8
+ const getSchemaPlugins = (context)=>context.plugins.byType("graphql-schema").filter((pl)=>{
9
+ if ("function" == typeof pl.isApplicable) return pl.isApplicable(context);
10
+ return true;
11
+ });
12
+ const createGraphQLSchema = async (context)=>{
13
+ GraphQLSchemaComposerFeature.register(context.container);
14
+ const scalars = context.plugins.byType("graphql-scalar").map((item)=>item.scalar);
15
+ const typeDefs = [
16
+ graphql_tag`
21
17
  type Query
22
18
  type Mutation
23
- ${scalars.map(scalar => `scalar ${scalar.name}`).join(" ")}
19
+ ${scalars.map((scalar)=>`scalar ${scalar.name}`).join(" ")}
24
20
  scalar JSON
25
21
  scalar Long
22
+ scalar Icon
26
23
  scalar RefInput
27
24
  scalar Number
28
25
  scalar Any
29
26
  scalar Date
30
27
  scalar DateTime
31
28
  scalar Time
32
- `];
33
- const resolvers = [(0, _objectSpread2.default)((0, _objectSpread2.default)({}, scalars.reduce((acc, s) => {
34
- acc[s.name] = s;
35
- return acc;
36
- }, {})), {}, {
37
- JSON: _builtInTypes.JsonScalar,
38
- Long: _builtInTypes.LongScalar,
39
- RefInput: _builtInTypes.RefInput,
40
- Number: _builtInTypes.NumberScalar,
41
- Any: _builtInTypes.AnyScalar,
42
- DateTime: _builtInTypes.DateTimeScalar,
43
- Date: _builtInTypes.DateScalar,
44
- Time: _builtInTypes.TimeScalar
45
- })];
46
- const gqlPlugins = context.plugins.byType("graphql-schema");
47
-
48
- for (let i = 0; i < gqlPlugins.length; i++) {
49
- const plugin = gqlPlugins[i];
50
- /**
51
- * TODO @ts-refactor
52
- * Figure out correct types on typeDefs and resolvers
53
- */
54
- // @ts-ignore
55
-
56
- typeDefs.push(plugin.schema.typeDefs); // @ts-ignore
57
29
 
58
- resolvers.push(plugin.schema.resolvers);
59
- }
30
+ type Error {
31
+ code: String
32
+ message: String
33
+ data: JSON
34
+ stack: String
35
+ }
60
36
 
61
- return (0, _schema.makeExecutableSchema)({
62
- typeDefs,
63
- resolvers,
64
- inheritResolversFromInterfaces: true
65
- });
37
+ type BooleanResponse {
38
+ data: Boolean
39
+ error: Error
40
+ }
41
+ `
42
+ ];
43
+ const resolvers = [
44
+ {
45
+ ...scalars.reduce((acc, s)=>{
46
+ acc[s.name] = s;
47
+ return acc;
48
+ }, {}),
49
+ JSON: JsonScalar,
50
+ Long: LongScalar,
51
+ RefInput: RefInputScalar,
52
+ Number: NumberScalar,
53
+ Any: AnyScalar,
54
+ DateTime: DateTimeScalar,
55
+ Date: DateScalar,
56
+ Time: TimeScalar,
57
+ Icon: IconScalar
58
+ }
59
+ ];
60
+ const resolverDecoration = new ResolverDecoration();
61
+ const plugins = getSchemaPlugins(context);
62
+ for (const plugin of plugins){
63
+ const schema = plugin.schema;
64
+ if (schema.typeDefs) typeDefs.push(schema.typeDefs);
65
+ if (schema.resolvers) resolvers.push(schema.resolvers);
66
+ if (schema.resolverDecorators) resolverDecoration.addDecorators(schema.resolverDecorators);
67
+ }
68
+ const graphQLSchemaComposer = context.container.resolve(GraphQLSchemaComposer);
69
+ const schema = await graphQLSchemaComposer.build();
70
+ if (schema.typeDefs) typeDefs.push(schema.typeDefs);
71
+ if (schema.resolvers) resolvers.push(schema.resolvers);
72
+ if (schema.resolverDecorators) resolverDecoration.addDecorators(schema.resolverDecorators);
73
+ return makeExecutableSchema({
74
+ typeDefs,
75
+ resolvers: resolverDecoration.decorateResolvers(mergeResolvers(resolvers)),
76
+ inheritResolversFromInterfaces: true
77
+ });
66
78
  };
79
+ export { createGraphQLSchema, getSchemaPlugins };
67
80
 
68
- exports.createGraphQLSchema = createGraphQLSchema;
81
+ //# sourceMappingURL=createGraphQLSchema.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["createGraphQLSchema","context","scalars","plugins","byType","map","item","scalar","typeDefs","gql","name","join","resolvers","reduce","acc","s","JSON","JsonScalar","Long","LongScalar","RefInput","Number","NumberScalar","Any","AnyScalar","DateTime","DateTimeScalar","Date","DateScalar","Time","TimeScalar","gqlPlugins","i","length","plugin","push","schema","makeExecutableSchema","inheritResolversFromInterfaces"],"sources":["createGraphQLSchema.ts"],"sourcesContent":["import gql from \"graphql-tag\";\nimport { makeExecutableSchema } from \"@graphql-tools/schema\";\nimport { GraphQLScalarPlugin, GraphQLSchemaPlugin } from \"./types\";\nimport { Context } from \"@webiny/api/types\";\nimport {\n RefInput,\n NumberScalar,\n AnyScalar,\n DateScalar,\n DateTimeScalar,\n JsonScalar,\n TimeScalar,\n LongScalar\n} from \"./builtInTypes\";\nimport { GraphQLScalarType } from \"graphql/type/definition\";\n\nexport const createGraphQLSchema = (context: Context) => {\n const scalars = context.plugins\n .byType<GraphQLScalarPlugin>(\"graphql-scalar\")\n .map(item => item.scalar);\n\n const typeDefs = [\n gql`\n type Query\n type Mutation\n ${scalars.map(scalar => `scalar ${scalar.name}`).join(\" \")}\n scalar JSON\n scalar Long\n scalar RefInput\n scalar Number\n scalar Any\n scalar Date\n scalar DateTime\n scalar Time\n `\n ];\n\n const resolvers = [\n {\n ...scalars.reduce((acc, s) => {\n acc[s.name] = s;\n return acc;\n }, {} as Record<string, GraphQLScalarType>),\n JSON: JsonScalar,\n Long: LongScalar,\n RefInput,\n Number: NumberScalar,\n Any: AnyScalar,\n DateTime: DateTimeScalar,\n Date: DateScalar,\n Time: TimeScalar\n }\n ];\n\n const gqlPlugins = context.plugins.byType<GraphQLSchemaPlugin>(\"graphql-schema\");\n for (let i = 0; i < gqlPlugins.length; i++) {\n const plugin = gqlPlugins[i];\n /**\n * TODO @ts-refactor\n * Figure out correct types on typeDefs and resolvers\n */\n // @ts-ignore\n typeDefs.push(plugin.schema.typeDefs);\n // @ts-ignore\n resolvers.push(plugin.schema.resolvers);\n }\n\n return makeExecutableSchema({\n typeDefs,\n resolvers,\n inheritResolversFromInterfaces: true\n });\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAGA;;AAYO,MAAMA,mBAAmB,GAAIC,OAAD,IAAsB;EACrD,MAAMC,OAAO,GAAGD,OAAO,CAACE,OAAR,CACXC,MADW,CACiB,gBADjB,EAEXC,GAFW,CAEPC,IAAI,IAAIA,IAAI,CAACC,MAFN,CAAhB;EAIA,MAAMC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/createGraphQLSchema.ts"],"sourcesContent":["import gql from \"graphql-tag\";\nimport { makeExecutableSchema } from \"@graphql-tools/schema\";\nimport { mergeResolvers } from \"@graphql-tools/merge\";\nimport type { GraphQLScalarType } from \"graphql/type/definition.js\";\nimport type { GraphQLScalarPlugin, Resolvers, TypeDefs } from \"./types.js\";\nimport type { Context } from \"@webiny/api/types.js\";\nimport {\n RefInputScalar,\n NumberScalar,\n AnyScalar,\n DateScalar,\n DateTimeScalar,\n JsonScalar,\n TimeScalar,\n LongScalar,\n IconScalar\n} from \"./builtInTypes/index.js\";\nimport { ResolverDecoration } from \"./ResolverDecoration.js\";\nimport type { GraphQLSchemaPlugin } from \"~/plugins/index.js\";\nimport { GraphQLSchemaComposer } from \"~/features/GraphQLSchemaBuilder/abstractions.js\";\nimport { GraphQLSchemaComposerFeature } from \"~/features/GraphQLSchemaBuilder/feature.js\";\n\nexport const getSchemaPlugins = (context: Context) => {\n return context.plugins.byType<GraphQLSchemaPlugin>(\"graphql-schema\").filter(pl => {\n if (typeof pl.isApplicable === \"function\") {\n return pl.isApplicable(context);\n }\n return true;\n });\n};\n\nexport const createGraphQLSchema = async (context: Context) => {\n GraphQLSchemaComposerFeature.register(context.container);\n\n const scalars = context.plugins\n .byType<GraphQLScalarPlugin>(\"graphql-scalar\")\n .map(item => item.scalar);\n\n const typeDefs: TypeDefs[] = [\n gql`\n type Query\n type Mutation\n ${scalars.map(scalar => `scalar ${scalar.name}`).join(\" \")}\n scalar JSON\n scalar Long\n scalar Icon\n scalar RefInput\n scalar Number\n scalar Any\n scalar Date\n scalar DateTime\n scalar Time\n\n type Error {\n code: String\n message: String\n data: JSON\n stack: String\n }\n\n type BooleanResponse {\n data: Boolean\n error: Error\n }\n `\n ];\n\n const resolvers: Resolvers<any>[] = [\n {\n ...scalars.reduce<Record<string, GraphQLScalarType>>((acc, s) => {\n acc[s.name] = s;\n return acc;\n }, {}),\n JSON: JsonScalar,\n Long: LongScalar,\n RefInput: RefInputScalar,\n Number: NumberScalar,\n Any: AnyScalar,\n DateTime: DateTimeScalar,\n Date: DateScalar,\n Time: TimeScalar,\n Icon: IconScalar\n }\n ];\n\n const resolverDecoration = new ResolverDecoration();\n\n // Process legacy plugins\n const plugins = getSchemaPlugins(context);\n\n for (const plugin of plugins) {\n const schema = plugin.schema;\n if (schema.typeDefs) {\n typeDefs.push(schema.typeDefs);\n }\n if (schema.resolvers) {\n resolvers.push(schema.resolvers);\n }\n if (schema.resolverDecorators) {\n resolverDecoration.addDecorators(schema.resolverDecorators);\n }\n }\n\n // Process new DI implementations\n const graphQLSchemaComposer = context.container.resolve(GraphQLSchemaComposer);\n const schema = await graphQLSchemaComposer.build();\n\n if (schema.typeDefs) {\n typeDefs.push(schema.typeDefs);\n }\n if (schema.resolvers) {\n resolvers.push(schema.resolvers);\n }\n if (schema.resolverDecorators) {\n resolverDecoration.addDecorators(schema.resolverDecorators);\n }\n\n // Create executable schema\n return makeExecutableSchema({\n typeDefs,\n resolvers: resolverDecoration.decorateResolvers(mergeResolvers(resolvers)),\n inheritResolversFromInterfaces: true\n });\n};\n"],"names":["getSchemaPlugins","context","pl","createGraphQLSchema","GraphQLSchemaComposerFeature","scalars","item","typeDefs","gql","scalar","resolvers","acc","s","JsonScalar","LongScalar","RefInputScalar","NumberScalar","AnyScalar","DateTimeScalar","DateScalar","TimeScalar","IconScalar","resolverDecoration","ResolverDecoration","plugins","plugin","schema","graphQLSchemaComposer","GraphQLSchemaComposer","makeExecutableSchema","mergeResolvers"],"mappings":";;;;;;;AAsBO,MAAMA,mBAAmB,CAACC,UACtBA,QAAQ,OAAO,CAAC,MAAM,CAAsB,kBAAkB,MAAM,CAACC,CAAAA;QACxE,IAAI,AAA2B,cAA3B,OAAOA,GAAG,YAAY,EACtB,OAAOA,GAAG,YAAY,CAACD;QAE3B,OAAO;IACX;AAGG,MAAME,sBAAsB,OAAOF;IACtCG,6BAA6B,QAAQ,CAACH,QAAQ,SAAS;IAEvD,MAAMI,UAAUJ,QAAQ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,iBAAiB,CAACQ,eAAepB;QAC/D,gCAAgC;IACpC;AACJ"}
@@ -0,0 +1,2 @@
1
+ import type { GraphQLRequestBody } from "./types.js";
2
+ export declare const createRequestBody: (input: unknown) => GraphQLRequestBody | GraphQLRequestBody[];
@@ -0,0 +1,28 @@
1
+ import zod from "zod";
2
+ import { WebinyError } from "@webiny/error";
3
+ import { createZodError } from "@webiny/utils/createZodError.js";
4
+ const requestBodySchema = zod.looseObject({
5
+ query: zod.string(),
6
+ variables: zod.record(zod.string(), zod.any()).nullish().optional(),
7
+ operationName: zod.string().nullish().optional().transform((value)=>value || void 0)
8
+ });
9
+ const schema = zod.union([
10
+ requestBodySchema,
11
+ zod.array(requestBodySchema)
12
+ ]);
13
+ const createRequestBody = (input)=>{
14
+ const body = "string" == typeof input ? JSON.parse(input) : input;
15
+ const result = schema.safeParse(body);
16
+ if (!result.success) {
17
+ const error = createZodError(result.error);
18
+ throw new WebinyError({
19
+ message: "Invalid GraphQL request! Check your query and variables.",
20
+ code: "GRAPHQL_REQUEST_INVALID",
21
+ data: error.data
22
+ });
23
+ }
24
+ return result.data;
25
+ };
26
+ export { createRequestBody };
27
+
28
+ //# sourceMappingURL=createRequestBody.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRequestBody.js","sources":["../src/createRequestBody.ts"],"sourcesContent":["import zod from \"zod\";\nimport { WebinyError } from \"@webiny/error\";\nimport { createZodError } from \"@webiny/utils/createZodError.js\";\nimport type { GraphQLRequestBody } from \"~/types.js\";\n\nconst requestBodySchema = zod.looseObject({\n query: zod.string(),\n variables: zod.record(zod.string(), zod.any()).nullish().optional(),\n operationName: zod\n .string()\n .nullish()\n .optional()\n .transform(value => {\n return value || undefined;\n })\n});\n\nconst schema = zod.union([requestBodySchema, zod.array(requestBodySchema)]);\n\nexport const createRequestBody = (input: unknown): GraphQLRequestBody | GraphQLRequestBody[] => {\n const body = typeof input === \"string\" ? JSON.parse(input) : input;\n\n const result = schema.safeParse(body);\n if (!result.success) {\n const error = createZodError(result.error);\n throw new WebinyError({\n message: \"Invalid GraphQL request! Check your query and variables.\",\n code: \"GRAPHQL_REQUEST_INVALID\",\n data: error.data\n });\n }\n return result.data;\n};\n"],"names":["requestBodySchema","zod","value","undefined","schema","createRequestBody","input","body","JSON","result","error","createZodError","WebinyError"],"mappings":";;;AAKA,MAAMA,oBAAoBC,IAAI,WAAW,CAAC;IACtC,OAAOA,IAAI,MAAM;IACjB,WAAWA,IAAI,MAAM,CAACA,IAAI,MAAM,IAAIA,IAAI,GAAG,IAAI,OAAO,GAAG,QAAQ;IACjE,eAAeA,IAAAA,MACJ,GACN,OAAO,GACP,QAAQ,GACR,SAAS,CAACC,CAAAA,QACAA,SAASC;AAE5B;AAEA,MAAMC,SAASH,IAAI,KAAK,CAAC;IAACD;IAAmBC,IAAI,KAAK,CAACD;CAAmB;AAEnE,MAAMK,oBAAoB,CAACC;IAC9B,MAAMC,OAAO,AAAiB,YAAjB,OAAOD,QAAqBE,KAAK,KAAK,CAACF,SAASA;IAE7D,MAAMG,SAASL,OAAO,SAAS,CAACG;IAChC,IAAI,CAACE,OAAO,OAAO,EAAE;QACjB,MAAMC,QAAQC,eAAeF,OAAO,KAAK;QACzC,MAAM,IAAIG,YAAY;YAClB,SAAS;YACT,MAAM;YACN,MAAMF,MAAM,IAAI;QACpB;IACJ;IACA,OAAOD,OAAO,IAAI;AACtB"}
@@ -0,0 +1,2 @@
1
+ import type { ResolverDecorator } from "./types.js";
2
+ export declare const createResolverDecorator: <TSource = any, TContext = any, TArgs = any>(decorator: ResolverDecorator<TSource, TContext, TArgs>) => ResolverDecorator<TSource, TContext, TArgs>;
@@ -0,0 +1,4 @@
1
+ const createResolverDecorator = (decorator)=>decorator;
2
+ export { createResolverDecorator };
3
+
4
+ //# sourceMappingURL=createResolverDecorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createResolverDecorator.js","sources":["../src/createResolverDecorator.ts"],"sourcesContent":["import type { ResolverDecorator } from \"./types.js\";\n\nexport const createResolverDecorator = <TSource = any, TContext = any, TArgs = any>(\n decorator: ResolverDecorator<TSource, TContext, TArgs>\n) => {\n return decorator;\n};\n"],"names":["createResolverDecorator","decorator"],"mappings":"AAEO,MAAMA,0BAA0B,CACnCC,YAEOA"}
package/debugPlugins.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { GraphQLAfterQueryPlugin } from "./types";
2
- import { Context } from "@webiny/api/types";
1
+ import type { GraphQLAfterQueryPlugin } from "./types.js";
2
+ import type { Context } from "@webiny/api/types.js";
3
3
  import { ContextPlugin } from "@webiny/api";
4
4
  interface Log {
5
5
  method: string;
package/debugPlugins.js CHANGED
@@ -1,45 +1,28 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _interceptConsole = require("./interceptConsole");
9
-
10
- var _api = require("@webiny/api");
11
-
12
- var _default = () => [new _api.ContextPlugin(async context => {
13
- if (!context.debug) {
14
- context.debug = {};
15
- }
16
-
17
- if (!context.debug.logs) {
18
- context.debug.logs = [];
19
- }
20
-
21
- (0, _interceptConsole.interceptConsole)((method, args) => {
22
- context.debug.logs.push({
23
- method,
24
- args
25
- });
26
- });
27
- }), {
28
- type: "graphql-after-query",
29
-
30
- apply({
31
- result,
32
- context
33
- }) {
34
- result["extensions"] = {
35
- console: [...(context.debug.logs || [])]
36
- };
37
-
38
- if (context.debug.logs) {
39
- context.debug.logs.length = 0;
40
- }
41
- }
42
-
43
- }];
44
-
45
- exports.default = _default;
1
+ import { interceptConsole } from "./interceptConsole.js";
2
+ import { ContextPlugin } from "@webiny/api";
3
+ const debugPlugins = ()=>[
4
+ new ContextPlugin(async (context)=>{
5
+ if (!context.debug) context.debug = {};
6
+ if (!context.debug.logs) context.debug.logs = [];
7
+ interceptConsole((method, args)=>{
8
+ context.debug.logs.push({
9
+ method,
10
+ args
11
+ });
12
+ });
13
+ }),
14
+ {
15
+ type: "graphql-after-query",
16
+ apply ({ result, context }) {
17
+ result["extensions"] = {
18
+ console: [
19
+ ...context.debug.logs || []
20
+ ]
21
+ };
22
+ if (context.debug.logs) context.debug.logs.length = 0;
23
+ }
24
+ }
25
+ ];
26
+ export default debugPlugins;
27
+
28
+ //# sourceMappingURL=debugPlugins.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["ContextPlugin","context","debug","logs","interceptConsole","method","args","push","type","apply","result","console","length"],"sources":["debugPlugins.ts"],"sourcesContent":["import { interceptConsole } from \"./interceptConsole\";\nimport { GraphQLAfterQueryPlugin } from \"./types\";\nimport { Context } from \"@webiny/api/types\";\nimport { ContextPlugin } from \"@webiny/api\";\n\ninterface Log {\n method: string;\n args: any;\n}\ninterface DebugContext extends Context {\n debug: {\n logs?: Log[];\n };\n}\n\nexport default () => [\n new ContextPlugin<DebugContext>(async context => {\n if (!context.debug) {\n context.debug = {};\n }\n\n if (!context.debug.logs) {\n context.debug.logs = [];\n }\n\n interceptConsole((method, args) => {\n (context.debug.logs as Log[]).push({ method, args });\n });\n }),\n {\n type: \"graphql-after-query\",\n apply({ result, context }) {\n result[\"extensions\"] = { console: [...(context.debug.logs || [])] };\n if (context.debug.logs) {\n context.debug.logs.length = 0;\n }\n }\n } as GraphQLAfterQueryPlugin<DebugContext>\n];\n"],"mappings":";;;;;;;AAAA;;AAGA;;eAYe,MAAM,CACjB,IAAIA,kBAAJ,CAAgC,MAAMC,OAAN,IAAiB;EAC7C,IAAI,CAACA,OAAO,CAACC,KAAb,EAAoB;IAChBD,OAAO,CAACC,KAAR,GAAgB,EAAhB;EACH;;EAED,IAAI,CAACD,OAAO,CAACC,KAAR,CAAcC,IAAnB,EAAyB;IACrBF,OAAO,CAACC,KAAR,CAAcC,IAAd,GAAqB,EAArB;EACH;;EAED,IAAAC,kCAAA,EAAiB,CAACC,MAAD,EAASC,IAAT,KAAkB;IAC9BL,OAAO,CAACC,KAAR,CAAcC,IAAf,CAA8BI,IAA9B,CAAmC;MAAEF,MAAF;MAAUC;IAAV,CAAnC;EACH,CAFD;AAGH,CAZD,CADiB,EAcjB;EACIE,IAAI,EAAE,qBADV;;EAEIC,KAAK,CAAC;IAAEC,MAAF;IAAUT;EAAV,CAAD,EAAsB;IACvBS,MAAM,CAAC,YAAD,CAAN,GAAuB;MAAEC,OAAO,EAAE,CAAC,IAAIV,OAAO,CAACC,KAAR,CAAcC,IAAd,IAAsB,EAA1B,CAAD;IAAX,CAAvB;;IACA,IAAIF,OAAO,CAACC,KAAR,CAAcC,IAAlB,EAAwB;MACpBF,OAAO,CAACC,KAAR,CAAcC,IAAd,CAAmBS,MAAnB,GAA4B,CAA5B;IACH;EACJ;;AAPL,CAdiB,C"}
1
+ {"version":3,"file":"debugPlugins.js","sources":["../src/debugPlugins.ts"],"sourcesContent":["import { interceptConsole } from \"./interceptConsole.js\";\nimport type { GraphQLAfterQueryPlugin } from \"./types.js\";\nimport type { Context } from \"@webiny/api/types.js\";\nimport { ContextPlugin } from \"@webiny/api\";\n\ninterface Log {\n method: string;\n args: any;\n}\ninterface DebugContext extends Context {\n debug: {\n logs?: Log[];\n };\n}\n\nexport default () => [\n new ContextPlugin<DebugContext>(async context => {\n if (!context.debug) {\n context.debug = {};\n }\n\n if (!context.debug.logs) {\n context.debug.logs = [];\n }\n\n interceptConsole((method, args) => {\n (context.debug.logs as Log[]).push({ method, args });\n });\n }),\n {\n type: \"graphql-after-query\",\n apply({ result, context }) {\n result[\"extensions\"] = { console: [...(context.debug.logs || [])] };\n if (context.debug.logs) {\n context.debug.logs.length = 0;\n }\n }\n } as GraphQLAfterQueryPlugin<DebugContext>\n];\n"],"names":["ContextPlugin","context","interceptConsole","method","args","result"],"mappings":";;AAeA,qBAAgB,IAAK;QACjB,IAAIA,cAA4B,OAAMC;YAClC,IAAI,CAACA,QAAQ,KAAK,EACdA,QAAQ,KAAK,GAAG,CAAC;YAGrB,IAAI,CAACA,QAAQ,KAAK,CAAC,IAAI,EACnBA,QAAQ,KAAK,CAAC,IAAI,GAAG,EAAE;YAG3BC,iBAAiB,CAACC,QAAQC;gBACrBH,QAAQ,KAAK,CAAC,IAAI,CAAW,IAAI,CAAC;oBAAEE;oBAAQC;gBAAK;YACtD;QACJ;QACA;YACI,MAAM;YACN,OAAM,EAAEC,MAAM,EAAEJ,OAAO,EAAE;gBACrBI,MAAM,CAAC,aAAa,GAAG;oBAAE,SAAS;2BAAKJ,QAAQ,KAAK,CAAC,IAAI,IAAI,EAAE;qBAAE;gBAAC;gBAClE,IAAIA,QAAQ,KAAK,CAAC,IAAI,EAClBA,QAAQ,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG;YAEpC;QACJ;KACJ"}
package/errors.js CHANGED
@@ -1,19 +1,9 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.NotFoundError = void 0;
9
-
10
- var _error = _interopRequireDefault(require("@webiny/error"));
11
-
12
- class NotFoundError extends _error.default {
13
- constructor(message = "Not found.") {
14
- super(message, "NOT_FOUND");
15
- }
16
-
1
+ import error from "@webiny/error";
2
+ class NotFoundError extends error {
3
+ constructor(message = "Not found."){
4
+ super(message, "NOT_FOUND");
5
+ }
17
6
  }
7
+ export { NotFoundError };
18
8
 
19
- exports.NotFoundError = NotFoundError;
9
+ //# sourceMappingURL=errors.js.map
package/errors.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["NotFoundError","WebinyError","constructor","message"],"sources":["errors.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\n\nexport class NotFoundError extends WebinyError {\n constructor(message = \"Not found.\") {\n super(message, \"NOT_FOUND\");\n }\n}\n"],"mappings":";;;;;;;;;AAAA;;AAEO,MAAMA,aAAN,SAA4BC,cAA5B,CAAwC;EAC3CC,WAAW,CAACC,OAAO,GAAG,YAAX,EAAyB;IAChC,MAAMA,OAAN,EAAe,WAAf;EACH;;AAH0C"}
1
+ {"version":3,"file":"errors.js","sources":["../src/errors.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\n\nexport class NotFoundError extends WebinyError {\n constructor(message = \"Not found.\") {\n super(message, \"NOT_FOUND\");\n }\n}\n"],"names":["NotFoundError","WebinyError","message"],"mappings":";AAEO,MAAMA,sBAAsBC;IAC/B,YAAYC,UAAU,YAAY,CAAE;QAChC,KAAK,CAACA,SAAS;IACnB;AACJ"}
@@ -0,0 +1,2 @@
1
+ export { GraphQLSchemaFactory } from "../../graphql/abstractions.js";
2
+ export { ErrorResponse, ListResponse, Response, NotFoundResponse, ListErrorResponse } from "../../responses.js";
@@ -0,0 +1,2 @@
1
+ export { GraphQLSchemaFactory } from "../../graphql/abstractions.js";
2
+ export { ErrorResponse, ListErrorResponse, ListResponse, NotFoundResponse, Response } from "../../responses.js";
@@ -0,0 +1,16 @@
1
+ import type { GraphQLSchemaBuilder as Abstraction } from "./abstractions.js";
2
+ import type { IGraphQLSchema } from "../../graphql/abstractions.public.js";
3
+ import type { ResolverDecorator } from "../../types.js";
4
+ export declare class GraphQLSchemaBuilder implements Abstraction.Interface {
5
+ private readonly typeDefsArray;
6
+ private readonly resolvers;
7
+ private readonly resolverDecorators;
8
+ addTypeDefs(typeDefs: string): this;
9
+ addResolver<TArgs = any>(config: Abstraction.Config<TArgs>): this;
10
+ /**
11
+ * @internal This method needs revisiting, to align with DI concepts.
12
+ */
13
+ addResolverDecorator(path: string, decorator: ResolverDecorator): this;
14
+ build(): IGraphQLSchema;
15
+ private setResolverAtPath;
16
+ }
@@ -0,0 +1,57 @@
1
+ class GraphQLSchemaBuilder {
2
+ addTypeDefs(typeDefs) {
3
+ this.typeDefsArray.push(typeDefs);
4
+ return this;
5
+ }
6
+ addResolver(config) {
7
+ const { path, dependencies = [], resolver } = config;
8
+ const pathParts = path.split(".");
9
+ const graphqlResolver = (parent, args, context, info)=>{
10
+ const resolvedDeps = dependencies.map((dep)=>{
11
+ const [abstraction] = Array.isArray(dep) ? dep : [
12
+ dep
13
+ ];
14
+ return context.container.resolve(abstraction);
15
+ });
16
+ const actualResolver = resolver(...resolvedDeps);
17
+ return actualResolver({
18
+ parent,
19
+ args,
20
+ context,
21
+ info
22
+ });
23
+ };
24
+ this.setResolverAtPath(pathParts, graphqlResolver);
25
+ return this;
26
+ }
27
+ addResolverDecorator(path, decorator) {
28
+ if (!this.resolverDecorators[path]) this.resolverDecorators[path] = [];
29
+ this.resolverDecorators[path].push(decorator);
30
+ return this;
31
+ }
32
+ build() {
33
+ return {
34
+ typeDefs: this.typeDefsArray.join("\n"),
35
+ resolvers: this.resolvers,
36
+ resolverDecorators: this.resolverDecorators
37
+ };
38
+ }
39
+ setResolverAtPath(pathParts, resolver) {
40
+ let current = this.resolvers;
41
+ for(let i = 0; i < pathParts.length - 1; i++){
42
+ const part = pathParts[i];
43
+ if (!current[part]) current[part] = {};
44
+ current = current[part];
45
+ }
46
+ const finalKey = pathParts[pathParts.length - 1];
47
+ current[finalKey] = resolver;
48
+ }
49
+ constructor(){
50
+ this.typeDefsArray = [];
51
+ this.resolvers = {};
52
+ this.resolverDecorators = {};
53
+ }
54
+ }
55
+ export { GraphQLSchemaBuilder };
56
+
57
+ //# sourceMappingURL=GraphQLSchemaBuilder.js.map