@webiny/handler-graphql 0.0.0-unstable.9bd236cf5e → 0.0.0-unstable.9f53ea597d

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 (103) hide show
  1. package/README.md +9 -5
  2. package/ResolverDecoration.d.ts +1 -1
  3. package/ResolverDecoration.js +18 -21
  4. package/ResolverDecoration.js.map +1 -1
  5. package/builtInTypes/AnyScalar.d.ts +1 -1
  6. package/builtInTypes/AnyScalar.js +8 -15
  7. package/builtInTypes/AnyScalar.js.map +1 -1
  8. package/builtInTypes/DateScalar.d.ts +1 -1
  9. package/builtInTypes/DateScalar.js +19 -23
  10. package/builtInTypes/DateScalar.js.map +1 -1
  11. package/builtInTypes/DateTimeScalar.d.ts +1 -1
  12. package/builtInTypes/DateTimeScalar.js +14 -23
  13. package/builtInTypes/DateTimeScalar.js.map +1 -1
  14. package/builtInTypes/DateTimeZScalar.d.ts +1 -1
  15. package/builtInTypes/DateTimeZScalar.js +25 -44
  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 +3 -8
  22. package/builtInTypes/JsonScalar.js.map +1 -1
  23. package/builtInTypes/LongScalar.d.ts +1 -1
  24. package/builtInTypes/LongScalar.js +32 -48
  25. package/builtInTypes/LongScalar.js.map +1 -1
  26. package/builtInTypes/NumberScalar.d.ts +1 -1
  27. package/builtInTypes/NumberScalar.js +33 -47
  28. package/builtInTypes/NumberScalar.js.map +1 -1
  29. package/builtInTypes/RefInputScalar.d.ts +1 -1
  30. package/builtInTypes/RefInputScalar.js +46 -69
  31. package/builtInTypes/RefInputScalar.js.map +1 -1
  32. package/builtInTypes/TimeScalar.d.ts +1 -1
  33. package/builtInTypes/TimeScalar.js +29 -49
  34. package/builtInTypes/TimeScalar.js.map +1 -1
  35. package/builtInTypes/index.d.ts +10 -9
  36. package/builtInTypes/index.js +10 -106
  37. package/createGraphQLHandler.d.ts +3 -3
  38. package/createGraphQLHandler.js +74 -102
  39. package/createGraphQLHandler.js.map +1 -1
  40. package/createGraphQLSchema.d.ts +4 -4
  41. package/createGraphQLSchema.js +55 -67
  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 +1 -1
  47. package/createResolverDecorator.js +2 -10
  48. package/createResolverDecorator.js.map +1 -1
  49. package/debugPlugins.d.ts +3 -3
  50. package/debugPlugins.js +26 -36
  51. package/debugPlugins.js.map +1 -1
  52. package/errors.js +6 -13
  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 -10
  77. package/index.js +10 -97
  78. package/index.js.map +1 -1
  79. package/interceptConsole.js +30 -34
  80. package/interceptConsole.js.map +1 -1
  81. package/package.json +32 -27
  82. package/plugins/GraphQLSchemaPlugin.d.ts +2 -2
  83. package/plugins/GraphQLSchemaPlugin.js +21 -30
  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 +3 -3
  88. package/processRequestBody.js +34 -44
  89. package/processRequestBody.js.map +1 -1
  90. package/responses.d.ts +11 -1
  91. package/responses.js +43 -61
  92. package/responses.js.map +1 -1
  93. package/types.d.ts +6 -6
  94. package/types.js +0 -7
  95. package/utils/index.d.ts +1 -1
  96. package/utils/index.js +1 -18
  97. package/utils/resolve.d.ts +2 -2
  98. package/utils/resolve.js +16 -24
  99. package/utils/resolve.js.map +1 -1
  100. package/builtInTypes/index.js.map +0 -1
  101. package/plugins/index.js.map +0 -1
  102. package/types.js.map +0 -1
  103. package/utils/index.js.map +0 -1
@@ -1,110 +1,82 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.default = void 0;
8
- var _boolean = require("boolean");
9
- var _handler = require("@webiny/handler");
10
- var _error = _interopRequireDefault(require("@webiny/error"));
11
- var _createGraphQLSchema = require("./createGraphQLSchema");
12
- var _debugPlugins = _interopRequireDefault(require("./debugPlugins"));
13
- var _processRequestBody = require("./processRequestBody");
14
- const DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year
15
-
16
- const createCacheKey = context => {
17
- const plugins = (0, _createGraphQLSchema.getSchemaPlugins)(context);
18
- // TODO: in the near future, we have to assign a fixed name to every
19
- // TODO: GraphQLSchema plugin, to be able to create a reliable cache key.
20
-
21
- // TODO: `getCurrentTenant` should be injected as a parameter.
22
- // @ts-expect-error TODO: We should not be accessing `context` like this here.
23
- const tenant = context.tenancy?.getCurrentTenant();
24
-
25
- // TODO: `getContentLocale` should be injected as a parameter.
26
- // @ts-expect-error TODO: We should not be accessing `context` like this here.
27
- const contentLocale = context.i18n?.getContentLocale();
28
- return [tenant ? `tenant:${tenant.id}` : null, contentLocale ? `locale:${contentLocale.code}` : null, plugins.length.toString()].filter(Boolean).join("#");
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("#");
29
16
  };
30
- const createRequestBody = body => {
31
- /**
32
- * We are trusting that the body payload is correct.
33
- * The `processRequestBody` will fail if it is not.
34
- */
35
- return typeof body === "string" ? JSON.parse(body) : body;
36
- };
37
- const formatErrorPayload = error => {
38
- if (error instanceof _error.default) {
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
24
+ });
39
25
  return JSON.stringify({
40
- type: "CoreGraphQLWebinyError",
41
- message: error.message,
42
- code: error.code,
43
- data: error.data
26
+ type: "Error",
27
+ name: error.name,
28
+ message: error.message,
29
+ stack: error.stack
44
30
  });
45
- }
46
- return JSON.stringify({
47
- type: "Error",
48
- name: error.name,
49
- message: error.message,
50
- stack: error.stack
51
- });
52
31
  };
53
- var _default = (options = {}) => {
54
- let schema = undefined;
55
- let cacheKey = undefined;
56
- const debug = (0, _boolean.boolean)(options.debug);
57
- const path = options?.path || "/graphql";
58
- const route = new _handler.RoutePlugin(async ({
59
- onPost,
60
- onOptions,
61
- context
62
- }) => {
63
- onOptions(path, async (_, reply) => {
64
- return reply.status(204).headers({
65
- "Cache-Control": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`
66
- }).send({}).hijack();
67
- });
68
- onPost(path, async (request, reply) => {
69
- const contextCacheKey = createCacheKey(context);
70
- if (!schema || cacheKey !== contextCacheKey) {
71
- try {
72
- schema = (0, _createGraphQLSchema.createGraphQLSchema)(context);
73
- cacheKey = contextCacheKey;
74
- } catch (ex) {
75
- return reply.code(500).send(formatErrorPayload(ex));
76
- }
77
- }
78
- let body;
79
- try {
80
- body = createRequestBody(request.body);
81
- } catch (ex) {
82
- console.error(`Error while creating the body request.`);
83
- console.error(formatErrorPayload(ex));
84
- throw ex;
85
- }
86
- try {
87
- const result = await (0, _processRequestBody.processRequestBody)(body, schema, context);
88
- /**
89
- * IMPORTANT! Do not send anything if reply was already sent.
90
- */
91
- if (reply.sent) {
92
- console.warn("Reply already sent, cannot send the result (handler-graphql).");
93
- return reply;
94
- }
95
- return reply.status(200).send(result);
96
- } catch (ex) {
97
- console.error(`Error while processing the body request.`);
98
- console.error(formatErrorPayload(ex));
99
- throw ex;
100
- }
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
+ });
101
70
  });
102
- });
103
- route.name = "handler.graphql.route.default";
104
- return [...(debug ? (0, _debugPlugins.default)() : []), {
105
- type: "wcp-telemetry-tracker"
106
- }, route];
71
+ route.name = "handler.graphql.route.default";
72
+ return [
73
+ ...debug ? debugPlugins() : [],
74
+ {
75
+ type: "wcp-telemetry-tracker"
76
+ },
77
+ route
78
+ ];
107
79
  };
108
- exports.default = _default;
80
+ export default createGraphQLHandler;
109
81
 
110
82
  //# sourceMappingURL=createGraphQLHandler.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_boolean","require","_handler","_error","_interopRequireDefault","_createGraphQLSchema","_debugPlugins","_processRequestBody","DEFAULT_CACHE_MAX_AGE","createCacheKey","context","plugins","getSchemaPlugins","tenant","tenancy","getCurrentTenant","contentLocale","i18n","getContentLocale","id","code","length","toString","filter","Boolean","join","createRequestBody","body","JSON","parse","formatErrorPayload","error","WebinyError","stringify","type","message","data","name","stack","_default","options","schema","undefined","cacheKey","debug","boolean","path","route","RoutePlugin","onPost","onOptions","_","reply","status","headers","send","hijack","request","contextCacheKey","createGraphQLSchema","ex","console","result","processRequestBody","sent","warn","debugPlugins","exports","default"],"sources":["createGraphQLHandler.ts"],"sourcesContent":["import { boolean } from \"boolean\";\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\";\nimport type { GraphQLRequestBody, HandlerGraphQLOptions } from \"./types\";\nimport { createGraphQLSchema, getSchemaPlugins } from \"./createGraphQLSchema\";\nimport debugPlugins from \"./debugPlugins\";\nimport { processRequestBody } from \"./processRequestBody\";\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 // TODO: `getContentLocale` should be injected as a parameter.\n // @ts-expect-error TODO: We should not be accessing `context` like this here.\n const contentLocale = context.i18n?.getContentLocale();\n\n return [\n tenant ? `tenant:${tenant.id}` : null,\n contentLocale ? `locale:${contentLocale.code}` : null,\n plugins.length.toString()\n ]\n .filter(Boolean)\n .join(\"#\");\n};\n\nconst createRequestBody = (body: unknown): GraphQLRequestBody | GraphQLRequestBody[] => {\n /**\n * We are trusting that the body payload is correct.\n * The `processRequestBody` will fail if it is not.\n */\n return typeof body === \"string\" ? JSON.parse(body) : body;\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 });\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 = 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 .hijack();\n });\n onPost(path, async (request, reply) => {\n const contextCacheKey = createCacheKey(context as Context);\n if (!schema || cacheKey !== contextCacheKey) {\n try {\n schema = 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"],"mappings":";;;;;;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AAGA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAC,sBAAA,CAAAH,OAAA;AAGA,IAAAI,oBAAA,GAAAJ,OAAA;AACA,IAAAK,aAAA,GAAAF,sBAAA,CAAAH,OAAA;AACA,IAAAM,mBAAA,GAAAN,OAAA;AAEA,MAAMO,qBAAqB,GAAG,QAAQ,CAAC,CAAC;;AAExC,MAAMC,cAAc,GAAIC,OAAgB,IAAK;EACzC,MAAMC,OAAO,GAAG,IAAAC,qCAAgB,EAACF,OAAO,CAAC;EACzC;EACA;;EAEA;EACA;EACA,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ACpC,IAAI,EAAEc,MAAM,EAAE/B,OAAO,CAAC;QAC9D;AAChB;AACA;QACgB,IAAI0C,KAAK,CAACY,IAAI,EAAE;UACZH,OAAO,CAACI,IAAI,CAAC,+DAA+D,CAAC;UAC7E,OAAOb,KAAK;QAChB;QACA,OAAOA,KAAK,CAACC,MAAM,CAAC,GAAG,CAAC,CAACE,IAAI,CAACO,MAAM,CAAC;MACzC,CAAC,CAAC,OAAOF,EAAE,EAAE;QACTC,OAAO,CAAC9B,KAAK,CAAC,0CAA0C,CAAC;QACzD8B,OAAO,CAAC9B,KAAK,CAACD,kBAAkB,CAAC8B,EAAE,CAAC,CAAC;QACrC,MAAMA,EAAE;MACZ;IACJ,CAAC,CAAC;EACN,CAAC,CAAC;EAEFb,KAAK,CAACV,IAAI,GAAG,+BAA+B;EAE5C,OAAO,CACH,IAAIO,KAAK,GAAG,IAAAsB,qBAAY,EAAC,CAAC,GAAG,EAAE,CAAC,EAChC;IACIhC,IAAI,EAAE;EACV,CAAC,EACDa,KAAK,CACR;AACL,CAAC;AAAAoB,OAAA,CAAAC,OAAA,GAAA7B,QAAA","ignoreList":[]}
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,MAAMF,MAAM,IAAI;QAChB,SAASA,MAAM,OAAO;QACtB,OAAOA,MAAM,KAAK;IACtB;AACJ;AAEA,6BAAgB,CAAAG,UAAiC,CAAC,CAAC;IAC/C,IAAIC;IACJ,IAAIC;IAEJ,MAAMC,QAAQC,UAAUJ,QAAQ,KAAK;IAErC,MAAMK,OAAOL,SAAS,QAAQ;IAE9B,MAAMM,QAAQ,IAAIC,YAAY,OAAO,EAAEC,MAAM,EAAEC,SAAS,EAAElB,OAAO,EAAE;QAC/DkB,UAAUJ,MAAM,OAAOK,GAAGC,QACfA,MACF,MAAM,CAAC,KACP,OAAO,CAAC;gBACL,iBAAiB,CAAC,gBAAgB,EAAEtB,uBAAuB;YAC/D,GACC,IAAI,CAAC,CAAC,GACN,MAAM;QAEfmB,OAAOH,MAAM,OAAOO,SAASD;YACzB,MAAME,kBAAkBvB,eAAeC;YACvC,IAAI,CAACU,UAAUC,aAAaW,iBACxB,IAAI;gBACAZ,SAAS,MAAMa,oBAAoBvB;gBACnCW,WAAWW;YACf,EAAE,OAAOE,IAAI;gBACT,OAAOJ,MAAM,IAAI,CAAC,KAAK,IAAI,CAACf,mBAAmBmB;YACnD;YAEJ,IAAIC;YACJ,IAAI;gBACAA,OAAOC,kBAAkBL,QAAQ,IAAI;YACzC,EAAE,OAAOG,IAAI;gBACTG,QAAQ,KAAK,CAAC;gBACdA,QAAQ,KAAK,CAACtB,mBAAmBmB;gBACjC,MAAMA;YACV;YACA,IAAI;gBACA,MAAMI,SAAS,MAAMC,mBAAmBJ,MAAMf,QAAQV;gBAItD,IAAIoB,MAAM,IAAI,EAAE;oBACZO,QAAQ,IAAI,CAAC;oBACb,OAAOP;gBACX;gBACA,OAAOA,MAAM,MAAM,CAAC,KAAK,IAAI,CAACQ;YAClC,EAAE,OAAOJ,IAAI;gBACTG,QAAQ,KAAK,CAAC;gBACdA,QAAQ,KAAK,CAACtB,mBAAmBmB;gBACjC,MAAMA;YACV;QACJ;IACJ;IAEAT,MAAM,IAAI,GAAG;IAEb,OAAO;WACCH,QAAQkB,iBAAiB,EAAE;QAC/B;YACI,MAAM;QACV;QACAf;KACH;AACL"}
@@ -1,4 +1,4 @@
1
- import type { Context } from "@webiny/api/types";
2
- import type { GraphQLSchemaPlugin } from "./plugins";
3
- export declare const getSchemaPlugins: (context: Context) => import("@webiny/plugins/PluginsContainer").WithName<GraphQLSchemaPlugin<Context>>[];
4
- 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,35 +1,25 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.getSchemaPlugins = exports.createGraphQLSchema = void 0;
8
- var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
9
- var _schema = require("@graphql-tools/schema");
10
- var _merge = require("@graphql-tools/merge");
11
- var _builtInTypes = require("./builtInTypes");
12
- var _ResolverDecoration = require("./ResolverDecoration");
13
- const getSchemaPlugins = context => {
14
- return context.plugins.byType("graphql-schema").filter(pl => {
15
- if (typeof pl.isApplicable === "function") {
16
- return pl.isApplicable(context);
17
- }
18
- return true;
19
- });
20
- };
21
- exports.getSchemaPlugins = getSchemaPlugins;
22
- const createGraphQLSchema = context => {
23
- const scalars = context.plugins.byType("graphql-scalar").map(item => item.scalar);
24
-
25
- // TODO: once the API packages are more closed, we'll have the opportunity
26
- // TODO: to maybe import the @ps directive from `api-prerendering-service` package.
27
- 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`
28
17
  type Query
29
18
  type Mutation
30
- ${scalars.map(scalar => `scalar ${scalar.name}`).join(" ")}
19
+ ${scalars.map((scalar)=>`scalar ${scalar.name}`).join(" ")}
31
20
  scalar JSON
32
21
  scalar Long
22
+ scalar Icon
33
23
  scalar RefInput
34
24
  scalar Number
35
25
  scalar Any
@@ -37,11 +27,6 @@ const createGraphQLSchema = context => {
37
27
  scalar DateTime
38
28
  scalar Time
39
29
 
40
- # This directive doesn't do anything on the GraphQL resolution level. It just serves
41
- # as a way to tell the Prerendering Service whether the GraphQL query needs to be
42
- # cached or not.
43
- directive @ps(cache: Boolean) on QUERY
44
-
45
30
  type Error {
46
31
  code: String
47
32
  message: String
@@ -53,41 +38,44 @@ const createGraphQLSchema = context => {
53
38
  data: Boolean
54
39
  error: Error
55
40
  }
56
- `];
57
- const resolvers = [{
58
- ...scalars.reduce((acc, s) => {
59
- acc[s.name] = s;
60
- return acc;
61
- }, {}),
62
- JSON: _builtInTypes.JsonScalar,
63
- Long: _builtInTypes.LongScalar,
64
- RefInput: _builtInTypes.RefInputScalar,
65
- Number: _builtInTypes.NumberScalar,
66
- Any: _builtInTypes.AnyScalar,
67
- DateTime: _builtInTypes.DateTimeScalar,
68
- Date: _builtInTypes.DateScalar,
69
- Time: _builtInTypes.TimeScalar
70
- }];
71
- const resolverDecoration = new _ResolverDecoration.ResolverDecoration();
72
- const plugins = getSchemaPlugins(context);
73
- for (const plugin of plugins) {
74
- const schema = plugin.schema;
75
- if (schema.typeDefs) {
76
- typeDefs.push(schema.typeDefs);
77
- }
78
- if (schema.resolvers) {
79
- resolvers.push(schema.resolvers);
80
- }
81
- if (schema.resolverDecorators) {
82
- resolverDecoration.addDecorators(schema.resolverDecorators);
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);
83
67
  }
84
- }
85
- return (0, _schema.makeExecutableSchema)({
86
- typeDefs,
87
- resolvers: resolverDecoration.decorateResolvers((0, _merge.mergeResolvers)(resolvers)),
88
- inheritResolversFromInterfaces: true
89
- });
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
+ });
90
78
  };
91
- exports.createGraphQLSchema = createGraphQLSchema;
79
+ export { createGraphQLSchema, getSchemaPlugins };
92
80
 
93
81
  //# sourceMappingURL=createGraphQLSchema.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_graphqlTag","_interopRequireDefault","require","_schema","_merge","_builtInTypes","_ResolverDecoration","getSchemaPlugins","context","plugins","byType","filter","pl","isApplicable","exports","createGraphQLSchema","scalars","map","item","scalar","typeDefs","gql","name","join","resolvers","reduce","acc","s","JSON","JsonScalar","Long","LongScalar","RefInput","RefInputScalar","Number","NumberScalar","Any","AnyScalar","DateTime","DateTimeScalar","Date","DateScalar","Time","TimeScalar","resolverDecoration","ResolverDecoration","plugin","schema","push","resolverDecorators","addDecorators","makeExecutableSchema","decorateResolvers","mergeResolvers","inheritResolversFromInterfaces"],"sources":["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\";\nimport type { GraphQLScalarPlugin, Resolvers, TypeDefs } from \"./types\";\nimport type { Context } from \"@webiny/api/types\";\nimport {\n RefInputScalar,\n NumberScalar,\n AnyScalar,\n DateScalar,\n DateTimeScalar,\n JsonScalar,\n TimeScalar,\n LongScalar\n} from \"./builtInTypes\";\nimport { ResolverDecoration } from \"./ResolverDecoration\";\nimport type { GraphQLSchemaPlugin } from \"~/plugins\";\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 = (context: Context) => {\n const scalars = context.plugins\n .byType<GraphQLScalarPlugin>(\"graphql-scalar\")\n .map(item => item.scalar);\n\n // TODO: once the API packages are more closed, we'll have the opportunity\n // TODO: to maybe import the @ps directive from `api-prerendering-service` package.\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 RefInput\n scalar Number\n scalar Any\n scalar Date\n scalar DateTime\n scalar Time\n\n # This directive doesn't do anything on the GraphQL resolution level. It just serves\n # as a way to tell the Prerendering Service whether the GraphQL query needs to be\n # cached or not.\n directive @ps(cache: Boolean) on QUERY\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 }\n ];\n\n const resolverDecoration = new ResolverDecoration();\n\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 return makeExecutableSchema({\n typeDefs,\n resolvers: resolverDecoration.decorateResolvers(mergeResolvers(resolvers)),\n inheritResolversFromInterfaces: true\n });\n};\n"],"mappings":";;;;;;;AAAA,IAAAA,WAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AAIA,IAAAG,aAAA,GAAAH,OAAA;AAUA,IAAAI,mBAAA,GAAAJ,OAAA;AAGO,MAAMK,gBAAgB,GAAIC,OAAgB,IAAK;EAClD,OAAOA,OAAO,CAACC,OAAO,CAACC,MAAM,CAAsB,gBAAgB,CAAC,CAACC,MAAM,CAACC,EAAE,IAAI;IAC9E,IAAI,OAAOA,EAAE,CAACC,YAAY,KAAK,UAAU,EAAE;MACvC,OAAOD,EAAE,CAACC,YAAY,CAACL,OAAO,CAAC;IACnC;IACA,OAAO,IAAI;EACf,CAAC,CAAC;AACN,CAAC;AAACM,OAAA,CAAAP,gBAAA,GAAAA,gBAAA;AAEK,MAAMQ,mBAAmB,GAAIP,OAAgB,IAAK;EACrD,MAAMQ,OAAO,GAAGR,OAAO,CAACC,OAA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B,IAAI,CAACD,MAAM,CAACvB,SAAS,CAAC;IACpC;IACA,IAAIuB,MAAM,CAACE,kBAAkB,EAAE;MAC3BL,kBAAkB,CAACM,aAAa,CAACH,MAAM,CAACE,kBAAkB,CAAC;IAC/D;EACJ;EAEA,OAAO,IAAAE,4BAAoB,EAAC;IACxB/B,QAAQ;IACRI,SAAS,EAAEoB,kBAAkB,CAACQ,iBAAiB,CAAC,IAAAC,qBAAc,EAAC7B,SAAS,CAAC,CAAC;IAC1E8B,8BAA8B,EAAE;EACpC,CAAC,CAAC;AACN,CAAC;AAACxC,OAAA,CAAAC,mBAAA,GAAAA,mBAAA","ignoreList":[]}
1
+ {"version":3,"file":"createGraphQLSchema.js","sources":["../src/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,OAAO,CAC1B,MAAM,CAAsB,kBAC5B,GAAG,CAACK,CAAAA,OAAQA,KAAK,MAAM;IAE5B,MAAMC,WAAuB;QACzBC,WAAG,CAAC;;;YAGA,EAAEH,QAAQ,GAAG,CAACI,CAAAA,SAAU,CAAC,OAAO,EAAEA,OAAO,IAAI,EAAE,EAAE,IAAI,CAAC,KAAK;;;;;;;;;;;;;;;;;;;;;;QAsB/D,CAAC;KACJ;IAED,MAAMC,YAA8B;QAChC;YACI,GAAGL,QAAQ,MAAM,CAAoC,CAACM,KAAKC;gBACvDD,GAAG,CAACC,EAAE,IAAI,CAAC,GAAGA;gBACd,OAAOD;YACX,GAAG,CAAC,EAAE;YACN,MAAME;YACN,MAAMC;YACN,UAAUC;YACV,QAAQC;YACR,KAAKC;YACL,UAAUC;YACV,MAAMC;YACN,MAAMC;YACN,MAAMC;QACV;KACH;IAED,MAAMC,qBAAqB,IAAIC;IAG/B,MAAMC,UAAUxB,iBAAiBC;IAEjC,KAAK,MAAMwB,UAAUD,QAAS;QAC1B,MAAME,SAASD,OAAO,MAAM;QAC5B,IAAIC,OAAO,QAAQ,EACfnB,SAAS,IAAI,CAACmB,OAAO,QAAQ;QAEjC,IAAIA,OAAO,SAAS,EAChBhB,UAAU,IAAI,CAACgB,OAAO,SAAS;QAEnC,IAAIA,OAAO,kBAAkB,EACzBJ,mBAAmB,aAAa,CAACI,OAAO,kBAAkB;IAElE;IAGA,MAAMC,wBAAwB1B,QAAQ,SAAS,CAAC,OAAO,CAAC2B;IACxD,MAAMF,SAAS,MAAMC,sBAAsB,KAAK;IAEhD,IAAID,OAAO,QAAQ,EACfnB,SAAS,IAAI,CAACmB,OAAO,QAAQ;IAEjC,IAAIA,OAAO,SAAS,EAChBhB,UAAU,IAAI,CAACgB,OAAO,SAAS;IAEnC,IAAIA,OAAO,kBAAkB,EACzBJ,mBAAmB,aAAa,CAACI,OAAO,kBAAkB;IAI9D,OAAOG,qBAAqB;QACxBtB;QACA,WAAWe,mBAAmB,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"}
@@ -1,2 +1,2 @@
1
- import type { ResolverDecorator } from "./types";
1
+ import type { ResolverDecorator } from "./types.js";
2
2
  export declare const createResolverDecorator: <TSource = any, TContext = any, TArgs = any>(decorator: ResolverDecorator<TSource, TContext, TArgs>) => ResolverDecorator<TSource, TContext, TArgs>;
@@ -1,12 +1,4 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createResolverDecorator = void 0;
7
- const createResolverDecorator = decorator => {
8
- return decorator;
9
- };
10
- exports.createResolverDecorator = createResolverDecorator;
1
+ const createResolverDecorator = (decorator)=>decorator;
2
+ export { createResolverDecorator };
11
3
 
12
4
  //# sourceMappingURL=createResolverDecorator.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["createResolverDecorator","decorator","exports"],"sources":["createResolverDecorator.ts"],"sourcesContent":["import type { ResolverDecorator } from \"./types\";\n\nexport const createResolverDecorator = <TSource = any, TContext = any, TArgs = any>(\n decorator: ResolverDecorator<TSource, TContext, TArgs>\n) => {\n return decorator;\n};\n"],"mappings":";;;;;;AAEO,MAAMA,uBAAuB,GAChCC,SAAsD,IACrD;EACD,OAAOA,SAAS;AACpB,CAAC;AAACC,OAAA,CAAAF,uBAAA,GAAAA,uBAAA","ignoreList":[]}
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 type { GraphQLAfterQueryPlugin } from "./types";
2
- import type { 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;
@@ -10,5 +10,5 @@ interface DebugContext extends Context {
10
10
  logs?: Log[];
11
11
  };
12
12
  }
13
- declare const _default: () => (ContextPlugin<DebugContext> | GraphQLAfterQueryPlugin<DebugContext>)[];
14
13
  export default _default;
14
+ declare function _default(): (ContextPlugin<DebugContext> | GraphQLAfterQueryPlugin<DebugContext>)[];
package/debugPlugins.js CHANGED
@@ -1,38 +1,28 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _interceptConsole = require("./interceptConsole");
8
- var _api = require("@webiny/api");
9
- var _default = () => [new _api.ContextPlugin(async context => {
10
- if (!context.debug) {
11
- context.debug = {};
12
- }
13
- if (!context.debug.logs) {
14
- context.debug.logs = [];
15
- }
16
- (0, _interceptConsole.interceptConsole)((method, args) => {
17
- context.debug.logs.push({
18
- method,
19
- args
20
- });
21
- });
22
- }), {
23
- type: "graphql-after-query",
24
- apply({
25
- result,
26
- context
27
- }) {
28
- result["extensions"] = {
29
- console: [...(context.debug.logs || [])]
30
- };
31
- if (context.debug.logs) {
32
- context.debug.logs.length = 0;
33
- }
34
- }
35
- }];
36
- 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;
37
27
 
38
28
  //# sourceMappingURL=debugPlugins.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_interceptConsole","require","_api","_default","ContextPlugin","context","debug","logs","interceptConsole","method","args","push","type","apply","result","console","length","exports","default"],"sources":["debugPlugins.ts"],"sourcesContent":["import { interceptConsole } from \"./interceptConsole\";\nimport type { GraphQLAfterQueryPlugin } from \"./types\";\nimport type { 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,IAAAA,iBAAA,GAAAC,OAAA;AAGA,IAAAC,IAAA,GAAAD,OAAA;AAA4C,IAAAE,QAAA,GAY7BA,CAAA,KAAM,CACjB,IAAIC,kBAAa,CAAe,MAAMC,OAAO,IAAI;EAC7C,IAAI,CAACA,OAAO,CAACC,KAAK,EAAE;IAChBD,OAAO,CAACC,KAAK,GAAG,CAAC,CAAC;EACtB;EAEA,IAAI,CAACD,OAAO,CAACC,KAAK,CAACC,IAAI,EAAE;IACrBF,OAAO,CAACC,KAAK,CAACC,IAAI,GAAG,EAAE;EAC3B;EAEA,IAAAC,kCAAgB,EAAC,CAACC,MAAM,EAAEC,IAAI,KAAK;IAC9BL,OAAO,CAACC,KAAK,CAACC,IAAI,CAAWI,IAAI,CAAC;MAAEF,MAAM;MAAEC;IAAK,CAAC,CAAC;EACxD,CAAC,CAAC;AACN,CAAC,CAAC,EACF;EACIE,IAAI,EAAE,qBAAqB;EAC3BC,KAAKA,CAAC;IAAEC,MAAM;IAAET;EAAQ,CAAC,EAAE;IACvBS,MAAM,CAAC,YAAY,CAAC,GAAG;MAAEC,OAAO,EAAE,CAAC,IAAIV,OAAO,CAACC,KAAK,CAACC,IAAI,IAAI,EAAE,CAAC;IAAE,CAAC;IACnE,IAAIF,OAAO,CAACC,KAAK,CAACC,IAAI,EAAE;MACpBF,OAAO,CAACC,KAAK,CAACC,IAAI,CAACS,MAAM,GAAG,CAAC;IACjC;EACJ;AACJ,CAAC,CACJ;AAAAC,OAAA,CAAAC,OAAA,GAAAf,QAAA","ignoreList":[]}
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"}