@webiny/handler-graphql 0.0.0-unstable.ecd8734205 → 0.0.0-unstable.f6dc066313

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 (104) hide show
  1. package/README.md +9 -5
  2. package/ResolverDecoration.d.ts +6 -0
  3. package/ResolverDecoration.js +19 -0
  4. package/ResolverDecoration.js.map +1 -0
  5. package/builtInTypes/AnyScalar.d.ts +1 -1
  6. package/builtInTypes/AnyScalar.js +4 -9
  7. package/builtInTypes/AnyScalar.js.map +1 -1
  8. package/builtInTypes/DateScalar.d.ts +1 -1
  9. package/builtInTypes/DateScalar.js +10 -13
  10. package/builtInTypes/DateScalar.js.map +1 -1
  11. package/builtInTypes/DateTimeScalar.d.ts +1 -1
  12. package/builtInTypes/DateTimeScalar.js +10 -13
  13. package/builtInTypes/DateTimeScalar.js.map +1 -1
  14. package/builtInTypes/DateTimeZScalar.d.ts +1 -1
  15. package/builtInTypes/DateTimeZScalar.js +10 -16
  16. package/builtInTypes/DateTimeZScalar.js.map +1 -1
  17. package/builtInTypes/IconScalar.d.ts +10 -0
  18. package/builtInTypes/IconScalar.js +93 -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 +2 -1
  24. package/builtInTypes/LongScalar.js +48 -8
  25. package/builtInTypes/LongScalar.js.map +1 -1
  26. package/builtInTypes/NumberScalar.d.ts +1 -1
  27. package/builtInTypes/NumberScalar.js +14 -21
  28. package/builtInTypes/NumberScalar.js.map +1 -1
  29. package/builtInTypes/RefInputScalar.d.ts +1 -1
  30. package/builtInTypes/RefInputScalar.js +40 -18
  31. package/builtInTypes/RefInputScalar.js.map +1 -1
  32. package/builtInTypes/TimeScalar.d.ts +1 -1
  33. package/builtInTypes/TimeScalar.js +11 -17
  34. package/builtInTypes/TimeScalar.js.map +1 -1
  35. package/builtInTypes/index.d.ts +10 -9
  36. package/builtInTypes/index.js +11 -103
  37. package/builtInTypes/index.js.map +1 -1
  38. package/createGraphQLHandler.d.ts +3 -3
  39. package/createGraphQLHandler.js +43 -34
  40. package/createGraphQLHandler.js.map +1 -1
  41. package/createGraphQLSchema.d.ts +4 -2
  42. package/createGraphQLSchema.js +78 -45
  43. package/createGraphQLSchema.js.map +1 -1
  44. package/createRequestBody.d.ts +2 -0
  45. package/createRequestBody.js +26 -0
  46. package/createRequestBody.js.map +1 -0
  47. package/createResolverDecorator.d.ts +2 -0
  48. package/createResolverDecorator.js +5 -0
  49. package/createResolverDecorator.js.map +1 -0
  50. package/debugPlugins.d.ts +2 -2
  51. package/debugPlugins.js +6 -11
  52. package/debugPlugins.js.map +1 -1
  53. package/errors.js +4 -10
  54. package/errors.js.map +1 -1
  55. package/exports/api/graphql.d.ts +2 -0
  56. package/exports/api/graphql.js +4 -0
  57. package/exports/api/graphql.js.map +1 -0
  58. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.d.ts +16 -0
  59. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js +64 -0
  60. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js.map +1 -0
  61. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.d.ts +13 -0
  62. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js +29 -0
  63. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js.map +1 -0
  64. package/features/GraphQLSchemaBuilder/abstractions.d.ts +34 -0
  65. package/features/GraphQLSchemaBuilder/abstractions.js +5 -0
  66. package/features/GraphQLSchemaBuilder/abstractions.js.map +1 -0
  67. package/features/GraphQLSchemaBuilder/feature.d.ts +1 -0
  68. package/features/GraphQLSchemaBuilder/feature.js +10 -0
  69. package/features/GraphQLSchemaBuilder/feature.js.map +1 -0
  70. package/graphql/abstractions.core.d.ts +11 -0
  71. package/graphql/abstractions.core.js +10 -0
  72. package/graphql/abstractions.core.js.map +1 -0
  73. package/graphql/abstractions.d.ts +2 -0
  74. package/graphql/abstractions.js +4 -0
  75. package/graphql/abstractions.js.map +1 -0
  76. package/graphql/abstractions.public.d.ts +19 -0
  77. package/graphql/abstractions.public.js +12 -0
  78. package/graphql/abstractions.public.js.map +1 -0
  79. package/index.d.ts +10 -5
  80. package/index.js +12 -46
  81. package/index.js.map +1 -1
  82. package/interceptConsole.js +8 -13
  83. package/interceptConsole.js.map +1 -1
  84. package/package.json +23 -26
  85. package/plugins/GraphQLSchemaPlugin.d.ts +13 -5
  86. package/plugins/GraphQLSchemaPlugin.js +16 -14
  87. package/plugins/GraphQLSchemaPlugin.js.map +1 -1
  88. package/plugins/index.d.ts +1 -1
  89. package/plugins/index.js +2 -15
  90. package/plugins/index.js.map +1 -1
  91. package/processRequestBody.d.ts +4 -5
  92. package/processRequestBody.js +15 -13
  93. package/processRequestBody.js.map +1 -1
  94. package/responses.js +7 -28
  95. package/responses.js.map +1 -1
  96. package/types.d.ts +20 -13
  97. package/types.js +2 -4
  98. package/types.js.map +1 -1
  99. package/utils/index.d.ts +1 -0
  100. package/utils/index.js +3 -0
  101. package/utils/index.js.map +1 -0
  102. package/utils/resolve.d.ts +21 -0
  103. package/utils/resolve.js +19 -0
  104. package/utils/resolve.js.map +1 -0
@@ -1,40 +1,61 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.RefInputScalar = void 0;
7
- var _graphql = require("graphql");
8
- const isMongoId = value => {
9
- if (/^[0-9a-fA-F]{24}$/.test(value)) {
10
- return value;
1
+ import { GraphQLScalarType } from "graphql";
2
+ const tests = [{
3
+ re: /^([0-9a-zA-Z_-]+)$/,
4
+ message: "Must be a string matching a-z, A-Z, 0-9, underscore (_) or dash(-)!"
5
+ }, {
6
+ re: /^-/,
7
+ message: "Must not start with a dash (-)!"
8
+ }, {
9
+ re: /-$/,
10
+ message: "Must not end with a dash (-)!"
11
+ }, {
12
+ re: /^_/,
13
+ message: "Must not start with an underscore (_)!"
14
+ }, {
15
+ re: /_$/,
16
+ message: "Must not end with an underscore (_)!"
17
+ }];
18
+ const isValidId = value => {
19
+ if (typeof value !== "string" || value.length < 1) {
20
+ throw new Error("Must be a string with at least 1 character!");
11
21
  }
12
- throw new Error("Must be a valid Mongo ID!");
22
+ for (const test of tests) {
23
+ if (test.re.test(value) === null) {
24
+ throw new Error(test.message);
25
+ }
26
+ }
27
+ return value;
13
28
  };
14
- const RefInputScalar = new _graphql.GraphQLScalarType({
29
+ export const RefInputScalar = new GraphQLScalarType({
15
30
  name: "RefInput",
16
31
  description: "A custom input type to be used with references. Supports plain ID and `{ id: ID }` Object literal.",
32
+ /**
33
+ * We can set value as any because we are handling it.
34
+ */
17
35
  serialize: value => {
18
36
  if (!value || value.id === null) {
19
37
  return null;
20
38
  }
21
39
  return typeof value === "string" ? value : value.id;
22
40
  },
41
+ /**
42
+ * We can set value as any because we are handling it.
43
+ */
23
44
  parseValue: value => {
24
45
  if (!value || value.id === null) {
25
46
  return null;
26
47
  }
27
48
  if (typeof value === "string") {
28
- return isMongoId(value);
49
+ return isValidId(value);
29
50
  }
30
51
  if ("id" in value) {
31
- return isMongoId(value.id);
52
+ return isValidId(value.id);
32
53
  }
33
54
  throw new Error("Invalid RefInput value!");
34
55
  },
35
56
  parseLiteral: ast => {
36
57
  if (ast.kind === "StringValue") {
37
- return isMongoId(ast.value);
58
+ return isValidId(ast.value);
38
59
  }
39
60
  if (ast.kind === "ObjectValue") {
40
61
  for (let i = 0; i < ast.fields.length; i++) {
@@ -43,12 +64,13 @@ const RefInputScalar = new _graphql.GraphQLScalarType({
43
64
  value
44
65
  } = ast.fields[i];
45
66
  if (name.value === "id") {
46
- // @ts-ignore
47
- return isMongoId(value.value);
67
+ // @ts-expect-error
68
+ return isValidId(value.value);
48
69
  }
49
70
  }
50
71
  }
51
72
  throw new Error("Invalid RefInput value!");
52
73
  }
53
74
  });
54
- exports.RefInputScalar = RefInputScalar;
75
+
76
+ //# sourceMappingURL=RefInputScalar.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["isMongoId","value","test","Error","RefInputScalar","GraphQLScalarType","name","description","serialize","id","parseValue","parseLiteral","ast","kind","i","fields","length"],"sources":["RefInputScalar.ts"],"sourcesContent":["import { GraphQLScalarType } from \"graphql\";\n\nconst isMongoId = (value: any): string => {\n if (/^[0-9a-fA-F]{24}$/.test(value)) {\n return value;\n }\n\n throw new Error(\"Must be a valid Mongo ID!\");\n};\n\nexport const RefInputScalar = new GraphQLScalarType({\n name: \"RefInput\",\n description:\n \"A custom input type to be used with references. Supports plain ID and `{ id: ID }` Object literal.\",\n serialize: value => {\n if (!value || value.id === null) {\n return null;\n }\n\n return typeof value === \"string\" ? value : value.id;\n },\n parseValue: value => {\n if (!value || value.id === null) {\n return null;\n }\n\n if (typeof value === \"string\") {\n return isMongoId(value);\n }\n\n if (\"id\" in value) {\n return isMongoId(value.id);\n }\n\n throw new Error(\"Invalid RefInput value!\");\n },\n parseLiteral: ast => {\n if (ast.kind === \"StringValue\") {\n return isMongoId(ast.value);\n }\n\n if (ast.kind === \"ObjectValue\") {\n for (let i = 0; i < ast.fields.length; i++) {\n const { name, value } = ast.fields[i];\n if (name.value === \"id\") {\n // @ts-ignore\n return isMongoId(value.value);\n }\n }\n }\n\n throw new Error(\"Invalid RefInput value!\");\n }\n});\n"],"mappings":";;;;;;AAAA;AAEA,MAAMA,SAAS,GAAIC,KAAU,IAAa;EACtC,IAAI,mBAAmB,CAACC,IAAI,CAACD,KAAK,CAAC,EAAE;IACjC,OAAOA,KAAK;EAChB;EAEA,MAAM,IAAIE,KAAK,CAAC,2BAA2B,CAAC;AAChD,CAAC;AAEM,MAAMC,cAAc,GAAG,IAAIC,0BAAiB,CAAC;EAChDC,IAAI,EAAE,UAAU;EAChBC,WAAW,EACP,oGAAoG;EACxGC,SAAS,EAAEP,KAAK,IAAI;IAChB,IAAI,CAACA,KAAK,IAAIA,KAAK,CAACQ,EAAE,KAAK,IAAI,EAAE;MAC7B,OAAO,IAAI;IACf;IAEA,OAAO,OAAOR,KAAK,KAAK,QAAQ,GAAGA,KAAK,GAAGA,KAAK,CAACQ,EAAE;EACvD,CAAC;EACDC,UAAU,EAAET,KAAK,IAAI;IACjB,IAAI,CAACA,KAAK,IAAIA,KAAK,CAACQ,EAAE,KAAK,IAAI,EAAE;MAC7B,OAAO,IAAI;IACf;IAEA,IAAI,OAAOR,KAAK,KAAK,QAAQ,EAAE;MAC3B,OAAOD,SAAS,CAACC,KAAK,CAAC;IAC3B;IAEA,IAAI,IAAI,IAAIA,KAAK,EAAE;MACf,OAAOD,SAAS,CAACC,KAAK,CAACQ,EAAE,CAAC;IAC9B;IAEA,MAAM,IAAIN,KAAK,CAAC,yBAAyB,CAAC;EAC9C,CAAC;EACDQ,YAAY,EAAEC,GAAG,IAAI;IACjB,IAAIA,GAAG,CAACC,IAAI,KAAK,aAAa,EAAE;MAC5B,OAAOb,SAAS,CAACY,GAAG,CAACX,KAAK,CAAC;IAC/B;IAEA,IAAIW,GAAG,CAACC,IAAI,KAAK,aAAa,EAAE;MAC5B,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGF,GAAG,CAACG,MAAM,CAACC,MAAM,EAAEF,CAAC,EAAE,EAAE;QACxC,MAAM;UAAER,IAAI;UAAEL;QAAM,CAAC,GAAGW,GAAG,CAACG,MAAM,CAACD,CAAC,CAAC;QACrC,IAAIR,IAAI,CAACL,KAAK,KAAK,IAAI,EAAE;UACrB;UACA,OAAOD,SAAS,CAACC,KAAK,CAACA,KAAK,CAAC;QACjC;MACJ;IACJ;IAEA,MAAM,IAAIE,KAAK,CAAC,yBAAyB,CAAC;EAC9C;AACJ,CAAC,CAAC;AAAC"}
1
+ {"version":3,"names":["GraphQLScalarType","tests","re","message","isValidId","value","length","Error","test","RefInputScalar","name","description","serialize","id","parseValue","parseLiteral","ast","kind","i","fields"],"sources":["RefInputScalar.ts"],"sourcesContent":["import { GraphQLScalarType } from \"graphql\";\n\nconst tests = [\n {\n re: /^([0-9a-zA-Z_-]+)$/,\n message: \"Must be a string matching a-z, A-Z, 0-9, underscore (_) or dash(-)!\"\n },\n {\n re: /^-/,\n message: \"Must not start with a dash (-)!\"\n },\n {\n re: /-$/,\n message: \"Must not end with a dash (-)!\"\n },\n {\n re: /^_/,\n message: \"Must not start with an underscore (_)!\"\n },\n {\n re: /_$/,\n message: \"Must not end with an underscore (_)!\"\n }\n];\n\nconst isValidId = (value: any): string => {\n if (typeof value !== \"string\" || value.length < 1) {\n throw new Error(\"Must be a string with at least 1 character!\");\n }\n for (const test of tests) {\n if (test.re.test(value) === null) {\n throw new Error(test.message);\n }\n }\n return value;\n};\n\nexport const RefInputScalar = new GraphQLScalarType({\n name: \"RefInput\",\n description:\n \"A custom input type to be used with references. Supports plain ID and `{ id: ID }` Object literal.\",\n /**\n * We can set value as any because we are handling it.\n */\n serialize: (value: any) => {\n if (!value || value.id === null) {\n return null;\n }\n\n return typeof value === \"string\" ? value : value.id;\n },\n /**\n * We can set value as any because we are handling it.\n */\n parseValue: (value: any) => {\n if (!value || value.id === null) {\n return null;\n }\n\n if (typeof value === \"string\") {\n return isValidId(value);\n }\n\n if (\"id\" in value) {\n return isValidId(value.id);\n }\n\n throw new Error(\"Invalid RefInput value!\");\n },\n parseLiteral: ast => {\n if (ast.kind === \"StringValue\") {\n return isValidId(ast.value);\n }\n\n if (ast.kind === \"ObjectValue\") {\n for (let i = 0; i < ast.fields.length; i++) {\n const { name, value } = ast.fields[i];\n if (name.value === \"id\") {\n // @ts-expect-error\n return isValidId(value.value);\n }\n }\n }\n\n throw new Error(\"Invalid RefInput value!\");\n }\n});\n"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,SAAS;AAE3C,MAAMC,KAAK,GAAG,CACV;EACIC,EAAE,EAAE,oBAAoB;EACxBC,OAAO,EAAE;AACb,CAAC,EACD;EACID,EAAE,EAAE,IAAI;EAC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,EAAE,CAAC;IAC9B;IAEA,MAAM,IAAIN,KAAK,CAAC,yBAAyB,CAAC;EAC9C,CAAC;EACDQ,YAAY,EAAEC,GAAG,IAAI;IACjB,IAAIA,GAAG,CAACC,IAAI,KAAK,aAAa,EAAE;MAC5B,OAAOb,SAAS,CAACY,GAAG,CAACX,KAAK,CAAC;IAC/B;IAEA,IAAIW,GAAG,CAACC,IAAI,KAAK,aAAa,EAAE;MAC5B,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGF,GAAG,CAACG,MAAM,CAACb,MAAM,EAAEY,CAAC,EAAE,EAAE;QACxC,MAAM;UAAER,IAAI;UAAEL;QAAM,CAAC,GAAGW,GAAG,CAACG,MAAM,CAACD,CAAC,CAAC;QACrC,IAAIR,IAAI,CAACL,KAAK,KAAK,IAAI,EAAE;UACrB;UACA,OAAOD,SAAS,CAACC,KAAK,CAACA,KAAK,CAAC;QACjC;MACJ;IACJ;IAEA,MAAM,IAAIE,KAAK,CAAC,yBAAyB,CAAC;EAC9C;AACJ,CAAC,CAAC","ignoreList":[]}
@@ -1,2 +1,2 @@
1
1
  import { GraphQLScalarType } from "graphql";
2
- export declare const TimeScalar: GraphQLScalarType;
2
+ export declare const TimeScalar: GraphQLScalarType<string, string>;
@@ -1,36 +1,29 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.TimeScalar = void 0;
8
- var _graphql = require("graphql");
9
- var _error = _interopRequireDefault(require("@webiny/error"));
1
+ import { GraphQLScalarType } from "graphql";
2
+ import WebinyError from "@webiny/error";
10
3
  const re = /^([0-9]{2}):([0-9]{2})(:([0-9]{2}))?$/;
11
4
  const parseTime = value => {
12
- if (!value || value.match(re) === null) {
13
- throw new _error.default("Value does not look like time.", "TIME_VALIDATION_ERROR", {
5
+ if (typeof value !== "string" || !value || value.match(re) === null) {
6
+ throw new WebinyError("Value does not look like time.", "TIME_VALIDATION_ERROR", {
14
7
  value
15
8
  });
16
9
  }
17
10
  const parsed = value.split(":").map(Number);
18
11
  if (parsed.length < 2) {
19
- throw new _error.default(`Could not parse the value.`, "TIME_VALIDATION_ERROR", {
12
+ throw new WebinyError(`Could not parse the value.`, "TIME_VALIDATION_ERROR", {
20
13
  value
21
14
  });
22
15
  }
23
16
  const [hours, minutes, seconds = 0] = parsed;
24
17
  if (hours >= 24) {
25
- throw new _error.default(`There cannot be more than 24 hours.`, "TIME_VALIDATION_ERROR", {
18
+ throw new WebinyError(`There cannot be more than 24 hours.`, "TIME_VALIDATION_ERROR", {
26
19
  value
27
20
  });
28
21
  } else if (minutes >= 60) {
29
- throw new _error.default(`There cannot be more than 59 minutes.`, "TIME_VALIDATION_ERROR", {
22
+ throw new WebinyError(`There cannot be more than 59 minutes.`, "TIME_VALIDATION_ERROR", {
30
23
  value
31
24
  });
32
25
  } else if (seconds >= 60) {
33
- throw new _error.default(`There cannot be more than 59 seconds.`, "TIME_VALIDATION_ERROR", {
26
+ throw new WebinyError(`There cannot be more than 59 seconds.`, "TIME_VALIDATION_ERROR", {
34
27
  value
35
28
  });
36
29
  }
@@ -48,7 +41,7 @@ const convertToTime = value => {
48
41
  } = parseTime(value);
49
42
  return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
50
43
  };
51
- const TimeScalar = new _graphql.GraphQLScalarType({
44
+ export const TimeScalar = new GraphQLScalarType({
52
45
  name: "Time",
53
46
  description: "A custom type to support time-only input.",
54
47
  // sending to client
@@ -56,4 +49,5 @@ const TimeScalar = new _graphql.GraphQLScalarType({
56
49
  // received from client
57
50
  parseValue: convertToTime
58
51
  });
59
- exports.TimeScalar = TimeScalar;
52
+
53
+ //# sourceMappingURL=TimeScalar.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["re","parseTime","value","match","WebinyError","parsed","split","map","Number","length","hours","minutes","seconds","convertToTime","String","padStart","TimeScalar","GraphQLScalarType","name","description","serialize","parseValue"],"sources":["TimeScalar.ts"],"sourcesContent":["import { GraphQLScalarType } from \"graphql\";\nimport WebinyError from \"@webiny/error\";\n\nconst re = /^([0-9]{2}):([0-9]{2})(:([0-9]{2}))?$/;\n\nconst parseTime = (value?: string) => {\n if (!value || value.match(re) === null) {\n throw new WebinyError(\"Value does not look like time.\", \"TIME_VALIDATION_ERROR\", { value });\n }\n const parsed = value.split(\":\").map(Number);\n if (parsed.length < 2) {\n throw new WebinyError(`Could not parse the value.`, \"TIME_VALIDATION_ERROR\", { value });\n }\n const [hours, minutes, seconds = 0] = parsed;\n if (hours >= 24) {\n throw new WebinyError(`There cannot be more than 24 hours.`, \"TIME_VALIDATION_ERROR\", {\n value\n });\n } else if (minutes >= 60) {\n throw new WebinyError(`There cannot be more than 59 minutes.`, \"TIME_VALIDATION_ERROR\", {\n value\n });\n } else if (seconds >= 60) {\n throw new WebinyError(`There cannot be more than 59 seconds.`, \"TIME_VALIDATION_ERROR\", {\n value\n });\n }\n return {\n hours,\n minutes,\n seconds\n };\n};\n\nconst convertToTime = (value: string): string => {\n const { hours, minutes, seconds } = parseTime(value);\n return `${String(hours).padStart(2, \"0\")}:${String(minutes).padStart(2, \"0\")}:${String(\n seconds\n ).padStart(2, \"0\")}`;\n};\n\nexport const TimeScalar = new GraphQLScalarType({\n name: \"Time\",\n description: \"A custom type to support time-only input.\",\n // sending to client\n serialize: convertToTime,\n // received from client\n parseValue: convertToTime\n});\n"],"mappings":";;;;;;;AAAA;AACA;AAEA,MAAMA,EAAE,GAAG,uCAAuC;AAElD,MAAMC,SAAS,GAAIC,KAAc,IAAK;EAClC,IAAI,CAACA,KAAK,IAAIA,KAAK,CAACC,KAAK,CAACH,EAAE,CAAC,KAAK,IAAI,EAAE;IACpC,MAAM,IAAII,cAAW,CAAC,gCAAgC,EAAE,uBAAuB,EAAE;MAAEF;IAAM,CAAC,CAAC;EAC/F;EACA,MAAMG,MAAM,GAAGH,KAAK,C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xD;EACAC,SAAS,EAAEP,aAAa;EACxB;EACAQ,UAAU,EAAER;AAChB,CAAC,CAAC;AAAC"}
1
+ {"version":3,"names":["GraphQLScalarType","WebinyError","re","parseTime","value","match","parsed","split","map","Number","length","hours","minutes","seconds","convertToTime","String","padStart","TimeScalar","name","description","serialize","parseValue"],"sources":["TimeScalar.ts"],"sourcesContent":["import { GraphQLScalarType } from \"graphql\";\nimport WebinyError from \"@webiny/error\";\n\nconst re = /^([0-9]{2}):([0-9]{2})(:([0-9]{2}))?$/;\n\nconst parseTime = (value?: unknown) => {\n if (typeof value !== \"string\" || !value || value.match(re) === null) {\n throw new WebinyError(\"Value does not look like time.\", \"TIME_VALIDATION_ERROR\", { value });\n }\n const parsed = value.split(\":\").map(Number);\n if (parsed.length < 2) {\n throw new WebinyError(`Could not parse the value.`, \"TIME_VALIDATION_ERROR\", { value });\n }\n const [hours, minutes, seconds = 0] = parsed;\n if (hours >= 24) {\n throw new WebinyError(`There cannot be more than 24 hours.`, \"TIME_VALIDATION_ERROR\", {\n value\n });\n } else if (minutes >= 60) {\n throw new WebinyError(`There cannot be more than 59 minutes.`, \"TIME_VALIDATION_ERROR\", {\n value\n });\n } else if (seconds >= 60) {\n throw new WebinyError(`There cannot be more than 59 seconds.`, \"TIME_VALIDATION_ERROR\", {\n value\n });\n }\n return {\n hours,\n minutes,\n seconds\n };\n};\n\nconst convertToTime = (value: unknown): string => {\n const { hours, minutes, seconds } = parseTime(value);\n return `${String(hours).padStart(2, \"0\")}:${String(minutes).padStart(2, \"0\")}:${String(\n seconds\n ).padStart(2, \"0\")}`;\n};\n\nexport const TimeScalar = new GraphQLScalarType({\n name: \"Time\",\n description: \"A custom type to support time-only input.\",\n // sending to client\n serialize: convertToTime,\n // received from client\n parseValue: convertToTime\n});\n"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,SAAS;AAC3C,OAAOC,WAAW,MAAM,eAAe;AAEvC,MAAMC,EAAE,GAAG,uCAAuC;AAElD,MAAMC,SA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AM,CAACH,OAAO,CAAC,CAACI,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,IAAID,MAAM,CAClFF,OACJ,CAAC,CAACG,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE;AACxB,CAAC;AAED,OAAO,MAAMC,UAAU,GAAG,IAAIjB,iBAAiB,CAAC;EAC5CkB,IAAI,EAAE,MAAM;EACZC,WAAW,EAAE,2CAA2C;EACxD;EACAC,SAAS,EAAEN,aAAa;EACxB;EACAO,UAAU,EAAEP;AAChB,CAAC,CAAC","ignoreList":[]}
@@ -1,9 +1,10 @@
1
- export * from "./AnyScalar";
2
- export * from "./DateScalar";
3
- export * from "./DateTimeScalar";
4
- export * from "./DateTimeZScalar";
5
- export * from "./JsonScalar";
6
- export * from "./LongScalar";
7
- export * from "./NumberScalar";
8
- export * from "./RefInputScalar";
9
- export * from "./TimeScalar";
1
+ export * from "./AnyScalar.js";
2
+ export * from "./DateScalar.js";
3
+ export * from "./DateTimeScalar.js";
4
+ export * from "./DateTimeZScalar.js";
5
+ export * from "./JsonScalar.js";
6
+ export * from "./LongScalar.js";
7
+ export * from "./NumberScalar.js";
8
+ export * from "./RefInputScalar.js";
9
+ export * from "./TimeScalar.js";
10
+ export * from "./IconScalar.js";
@@ -1,104 +1,12 @@
1
- "use strict";
1
+ export * from "./AnyScalar.js";
2
+ export * from "./DateScalar.js";
3
+ export * from "./DateTimeScalar.js";
4
+ export * from "./DateTimeZScalar.js";
5
+ export * from "./JsonScalar.js";
6
+ export * from "./LongScalar.js";
7
+ export * from "./NumberScalar.js";
8
+ export * from "./RefInputScalar.js";
9
+ export * from "./TimeScalar.js";
10
+ export * from "./IconScalar.js";
2
11
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _AnyScalar = require("./AnyScalar");
7
- Object.keys(_AnyScalar).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _AnyScalar[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _AnyScalar[key];
14
- }
15
- });
16
- });
17
- var _DateScalar = require("./DateScalar");
18
- Object.keys(_DateScalar).forEach(function (key) {
19
- if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _DateScalar[key]) return;
21
- Object.defineProperty(exports, key, {
22
- enumerable: true,
23
- get: function () {
24
- return _DateScalar[key];
25
- }
26
- });
27
- });
28
- var _DateTimeScalar = require("./DateTimeScalar");
29
- Object.keys(_DateTimeScalar).forEach(function (key) {
30
- if (key === "default" || key === "__esModule") return;
31
- if (key in exports && exports[key] === _DateTimeScalar[key]) return;
32
- Object.defineProperty(exports, key, {
33
- enumerable: true,
34
- get: function () {
35
- return _DateTimeScalar[key];
36
- }
37
- });
38
- });
39
- var _DateTimeZScalar = require("./DateTimeZScalar");
40
- Object.keys(_DateTimeZScalar).forEach(function (key) {
41
- if (key === "default" || key === "__esModule") return;
42
- if (key in exports && exports[key] === _DateTimeZScalar[key]) return;
43
- Object.defineProperty(exports, key, {
44
- enumerable: true,
45
- get: function () {
46
- return _DateTimeZScalar[key];
47
- }
48
- });
49
- });
50
- var _JsonScalar = require("./JsonScalar");
51
- Object.keys(_JsonScalar).forEach(function (key) {
52
- if (key === "default" || key === "__esModule") return;
53
- if (key in exports && exports[key] === _JsonScalar[key]) return;
54
- Object.defineProperty(exports, key, {
55
- enumerable: true,
56
- get: function () {
57
- return _JsonScalar[key];
58
- }
59
- });
60
- });
61
- var _LongScalar = require("./LongScalar");
62
- Object.keys(_LongScalar).forEach(function (key) {
63
- if (key === "default" || key === "__esModule") return;
64
- if (key in exports && exports[key] === _LongScalar[key]) return;
65
- Object.defineProperty(exports, key, {
66
- enumerable: true,
67
- get: function () {
68
- return _LongScalar[key];
69
- }
70
- });
71
- });
72
- var _NumberScalar = require("./NumberScalar");
73
- Object.keys(_NumberScalar).forEach(function (key) {
74
- if (key === "default" || key === "__esModule") return;
75
- if (key in exports && exports[key] === _NumberScalar[key]) return;
76
- Object.defineProperty(exports, key, {
77
- enumerable: true,
78
- get: function () {
79
- return _NumberScalar[key];
80
- }
81
- });
82
- });
83
- var _RefInputScalar = require("./RefInputScalar");
84
- Object.keys(_RefInputScalar).forEach(function (key) {
85
- if (key === "default" || key === "__esModule") return;
86
- if (key in exports && exports[key] === _RefInputScalar[key]) return;
87
- Object.defineProperty(exports, key, {
88
- enumerable: true,
89
- get: function () {
90
- return _RefInputScalar[key];
91
- }
92
- });
93
- });
94
- var _TimeScalar = require("./TimeScalar");
95
- Object.keys(_TimeScalar).forEach(function (key) {
96
- if (key === "default" || key === "__esModule") return;
97
- if (key in exports && exports[key] === _TimeScalar[key]) return;
98
- Object.defineProperty(exports, key, {
99
- enumerable: true,
100
- get: function () {
101
- return _TimeScalar[key];
102
- }
103
- });
104
- });
12
+ //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./AnyScalar\";\nexport * from \"./DateScalar\";\nexport * from \"./DateTimeScalar\";\nexport * from \"./DateTimeZScalar\";\nexport * from \"./JsonScalar\";\nexport * from \"./LongScalar\";\nexport * from \"./NumberScalar\";\nexport * from \"./RefInputScalar\";\nexport * from \"./TimeScalar\";\n"],"mappings":";;;;;AAAA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./AnyScalar.js\";\nexport * from \"./DateScalar.js\";\nexport * from \"./DateTimeScalar.js\";\nexport * from \"./DateTimeZScalar.js\";\nexport * from \"./JsonScalar.js\";\nexport * from \"./LongScalar.js\";\nexport * from \"./NumberScalar.js\";\nexport * from \"./RefInputScalar.js\";\nexport * from \"./TimeScalar.js\";\nexport * from \"./IconScalar.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
@@ -1,4 +1,4 @@
1
- import { PluginCollection } from "@webiny/plugins/types";
2
- import { HandlerGraphQLOptions } from "./types";
3
- declare const _default: (options?: HandlerGraphQLOptions) => PluginCollection;
1
+ import type { Plugin } from "@webiny/plugins/types.js";
2
+ import type { HandlerGraphQLOptions } from "./types.js";
3
+ declare const _default: (options?: HandlerGraphQLOptions) => Plugin[];
4
4
  export default _default;
@@ -1,32 +1,30 @@
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 = _interopRequireDefault(require("./processRequestBody"));
1
+ import { boolean } from "boolean";
2
+ import { RoutePlugin } from "@webiny/handler";
3
+ import WebinyError 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";
14
8
  const DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year
15
9
 
16
- const createRequestBody = body => {
17
- /**
18
- * We are trusting that the body payload is correct.
19
- * The `processRequestBody` will fail if it is not.
20
- */
21
- return typeof body === "string" ? JSON.parse(body) : body;
10
+ const createCacheKey = context => {
11
+ const plugins = getSchemaPlugins(context);
12
+ // TODO: in the near future, we have to assign a fixed name to every
13
+ // TODO: GraphQLSchema plugin, to be able to create a reliable cache key.
14
+
15
+ // TODO: `getCurrentTenant` should be injected as a parameter.
16
+ // @ts-expect-error TODO: We should not be accessing `context` like this here.
17
+ const tenant = context.tenancy?.getCurrentTenant();
18
+ return [tenant ? `tenant:${tenant.id}` : null, plugins.length.toString()].filter(Boolean).join("#");
22
19
  };
23
20
  const formatErrorPayload = error => {
24
- if (error instanceof _error.default) {
21
+ if (error instanceof WebinyError) {
25
22
  return JSON.stringify({
26
23
  type: "CoreGraphQLWebinyError",
27
24
  message: error.message,
28
25
  code: error.code,
29
- data: error.data
26
+ data: error.data,
27
+ stack: error.stack
30
28
  });
31
29
  }
32
30
  return JSON.stringify({
@@ -36,11 +34,12 @@ const formatErrorPayload = error => {
36
34
  stack: error.stack
37
35
  });
38
36
  };
39
- var _default = (options = {}) => {
37
+ export default (options = {}) => {
40
38
  let schema = undefined;
41
- const debug = (0, _boolean.boolean)(options.debug);
42
- const path = (options === null || options === void 0 ? void 0 : options.path) || "/graphql";
43
- const route = new _handler.RoutePlugin(async ({
39
+ let cacheKey = undefined;
40
+ const debug = boolean(options.debug);
41
+ const path = options?.path || "/graphql";
42
+ const route = new RoutePlugin(async ({
44
43
  onPost,
45
44
  onOptions,
46
45
  context
@@ -51,9 +50,11 @@ var _default = (options = {}) => {
51
50
  }).send({}).hijack();
52
51
  });
53
52
  onPost(path, async (request, reply) => {
54
- if (!schema) {
53
+ const contextCacheKey = createCacheKey(context);
54
+ if (!schema || cacheKey !== contextCacheKey) {
55
55
  try {
56
- schema = (0, _createGraphQLSchema.createGraphQLSchema)(context);
56
+ schema = await createGraphQLSchema(context);
57
+ cacheKey = contextCacheKey;
57
58
  } catch (ex) {
58
59
  return reply.code(500).send(formatErrorPayload(ex));
59
60
  }
@@ -62,23 +63,31 @@ var _default = (options = {}) => {
62
63
  try {
63
64
  body = createRequestBody(request.body);
64
65
  } catch (ex) {
65
- console.log(`Error while creating the body request.`);
66
- console.log(formatErrorPayload(ex));
66
+ console.error(`Error while creating the body request.`);
67
+ console.error(formatErrorPayload(ex));
67
68
  throw ex;
68
69
  }
69
70
  try {
70
- const result = await (0, _processRequestBody.default)(body, schema, context);
71
+ const result = await processRequestBody(body, schema, context);
72
+ /**
73
+ * IMPORTANT! Do not send anything if reply was already sent.
74
+ */
75
+ if (reply.sent) {
76
+ console.warn("Reply already sent, cannot send the result (handler-graphql).");
77
+ return reply;
78
+ }
71
79
  return reply.status(200).send(result);
72
80
  } catch (ex) {
73
- console.log(`Error while processing the body request.`);
74
- console.log(formatErrorPayload(ex));
81
+ console.error(`Error while processing the body request.`);
82
+ console.error(formatErrorPayload(ex));
75
83
  throw ex;
76
84
  }
77
85
  });
78
86
  });
79
87
  route.name = "handler.graphql.route.default";
80
- return [...(debug ? (0, _debugPlugins.default)() : []), {
88
+ return [...(debug ? debugPlugins() : []), {
81
89
  type: "wcp-telemetry-tracker"
82
90
  }, route];
83
91
  };
84
- exports.default = _default;
92
+
93
+ //# sourceMappingURL=createGraphQLHandler.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["DEFAULT_CACHE_MAX_AGE","createRequestBody","body","JSON","parse","formatErrorPayload","error","WebinyError","stringify","type","message","code","data","name","stack","options","schema","undefined","debug","boolean","path","route","RoutePlugin","onPost","onOptions","context","_","reply","status","headers","send","hijack","request","createGraphQLSchema","ex","console","log","result","processRequestBody","debugPlugins"],"sources":["createGraphQLHandler.ts"],"sourcesContent":["import { boolean } from \"boolean\";\nimport { GraphQLSchema } from \"graphql\";\nimport { RoutePlugin } from \"@webiny/handler\";\nimport WebinyError from \"@webiny/error\";\nimport { PluginCollection } from \"@webiny/plugins/types\";\nimport { GraphQLRequestBody, HandlerGraphQLOptions } from \"./types\";\nimport { createGraphQLSchema } from \"./createGraphQLSchema\";\nimport debugPlugins from \"./debugPlugins\";\nimport processRequestBody from \"./processRequestBody\";\n\nconst DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year\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 = {}): 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 .hijack();\n });\n onPost(path, async (request, reply) => {\n if (!schema) {\n try {\n schema = createGraphQLSchema(context);\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.log(`Error while creating the body request.`);\n console.log(formatErrorPayload(ex));\n throw ex;\n }\n try {\n const result = await processRequestBody(body, schema, context);\n return reply.status(200).send(result);\n } catch (ex) {\n console.log(`Error while processing the body request.`);\n console.log(formatErrorPayload(ex));\n throw ex;\n }\n });\n });\n\n route.name = \"handler.graphql.route.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OAAO,CACH,IAAIK,KAAK,GAAG,IAAAqB,qBAAY,GAAE,GAAG,EAAE,CAAC,EAChC;IACI9B,IAAI,EAAE;EACV,CAAC,EACDY,KAAK,CACR;AACL,CAAC;AAAA"}
1
+ {"version":3,"names":["boolean","RoutePlugin","WebinyError","createGraphQLSchema","getSchemaPlugins","debugPlugins","processRequestBody","createRequestBody","DEFAULT_CACHE_MAX_AGE","createCacheKey","context","plugins","tenant","tenancy","getCurrentTenant","id","length","toString","filter","Boolean","join","formatErrorPayload","error","JSON","stringify","type","message","code","data","stack","name","options","schema","undefined","cacheKey","debug","path","route","onPost","onOptions","_","reply","status","headers","send","hijack","request","contextCacheKey","ex","body","console","result","sent","warn"],"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.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 = 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 = 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"],"mappings":"AAAA,SAASA,OAAO,QAAQ,SAAS;AAGjC,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,OAAOC,WAAW,MAAM,eAAe;AAGvC,SAASC,mBAAmB,EAAEC,gBAAgB;AAC9C,OAAOC,YAAY;AACnB,SAASC,kBAAkB;AAC3B,SAASC,iBAA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+DAA+D,CAAC;UAC7E,OAAOZ,KAAK;QAChB;QACA,OAAOA,KAAK,CAACC,MAAM,CAAC,GAAG,CAAC,CAACE,IAAI,CAACO,MAAM,CAAC;MACzC,CAAC,CAAC,OAAOH,EAAE,EAAE;QACTE,OAAO,CAAC5B,KAAK,CAAC,0CAA0C,CAAC;QACzD4B,OAAO,CAAC5B,KAAK,CAACD,kBAAkB,CAAC2B,EAAE,CAAC,CAAC;QACrC,MAAMA,EAAE;MACZ;IACJ,CAAC,CAAC;EACN,CAAC,CAAC;EAEFX,KAAK,CAACP,IAAI,GAAG,+BAA+B;EAE5C,OAAO,CACH,IAAIK,KAAK,GAAG9B,YAAY,CAAC,CAAC,GAAG,EAAE,CAAC,EAChC;IACIoB,IAAI,EAAE;EACV,CAAC,EACDY,KAAK,CACR;AACL,CAAC","ignoreList":[]}
@@ -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>;