@webiny/handler-graphql 6.3.0-beta.4 → 6.4.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/ResolverDecoration.js +18 -14
  2. package/ResolverDecoration.js.map +1 -1
  3. package/builtInTypes/AnyScalar.js +7 -8
  4. package/builtInTypes/AnyScalar.js.map +1 -1
  5. package/builtInTypes/DateScalar.js +12 -18
  6. package/builtInTypes/DateScalar.js.map +1 -1
  7. package/builtInTypes/DateTimeScalar.js +12 -18
  8. package/builtInTypes/DateTimeScalar.js.map +1 -1
  9. package/builtInTypes/DateTimeZScalar.js +23 -35
  10. package/builtInTypes/DateTimeZScalar.js.map +1 -1
  11. package/builtInTypes/IconScalar.js +59 -87
  12. package/builtInTypes/IconScalar.js.map +1 -1
  13. package/builtInTypes/JsonScalar.js +2 -1
  14. package/builtInTypes/JsonScalar.js.map +1 -1
  15. package/builtInTypes/LongScalar.js +30 -39
  16. package/builtInTypes/LongScalar.js.map +1 -1
  17. package/builtInTypes/NumberScalar.js +31 -38
  18. package/builtInTypes/NumberScalar.js.map +1 -1
  19. package/builtInTypes/RefInputScalar.js +45 -68
  20. package/builtInTypes/RefInputScalar.js.map +1 -1
  21. package/builtInTypes/TimeScalar.js +28 -41
  22. package/builtInTypes/TimeScalar.js.map +1 -1
  23. package/builtInTypes/index.js +0 -2
  24. package/createGraphQLHandler.js +69 -80
  25. package/createGraphQLHandler.js.map +1 -1
  26. package/createGraphQLSchema.js +49 -66
  27. package/createGraphQLSchema.js.map +1 -1
  28. package/createRequestBody.js +20 -18
  29. package/createRequestBody.js.map +1 -1
  30. package/createResolverDecorator.js +2 -3
  31. package/createResolverDecorator.js.map +1 -1
  32. package/debugPlugins.js +24 -27
  33. package/debugPlugins.js.map +1 -1
  34. package/errors.js +6 -5
  35. package/errors.js.map +1 -1
  36. package/exports/api/graphql.js +1 -3
  37. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js +52 -59
  38. package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js.map +1 -1
  39. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js +27 -21
  40. package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js.map +1 -1
  41. package/features/GraphQLSchemaBuilder/abstractions.js +3 -2
  42. package/features/GraphQLSchemaBuilder/abstractions.js.map +1 -1
  43. package/features/GraphQLSchemaBuilder/feature.js +6 -5
  44. package/features/GraphQLSchemaBuilder/feature.js.map +1 -1
  45. package/graphql/abstractions.core.js +2 -7
  46. package/graphql/abstractions.core.js.map +1 -1
  47. package/graphql/abstractions.js +0 -2
  48. package/graphql/abstractions.public.js +2 -8
  49. package/graphql/abstractions.public.js.map +1 -1
  50. package/index.js +2 -3
  51. package/index.js.map +1 -1
  52. package/interceptConsole.js +30 -27
  53. package/interceptConsole.js.map +1 -1
  54. package/package.json +13 -13
  55. package/plugins/GraphQLSchemaPlugin.js +20 -21
  56. package/plugins/GraphQLSchemaPlugin.js.map +1 -1
  57. package/plugins/index.js +0 -2
  58. package/processRequestBody.js +33 -36
  59. package/processRequestBody.js.map +1 -1
  60. package/responses.js +48 -65
  61. package/responses.js.map +1 -1
  62. package/types.js +0 -3
  63. package/utils/index.js +0 -2
  64. package/utils/resolve.js +15 -14
  65. package/utils/resolve.js.map +1 -1
  66. package/builtInTypes/index.js.map +0 -1
  67. package/exports/api/graphql.js.map +0 -1
  68. package/graphql/abstractions.js.map +0 -1
  69. package/plugins/index.js.map +0 -1
  70. package/types.js.map +0 -1
  71. package/utils/index.js.map +0 -1
@@ -1,48 +1,41 @@
1
1
  import { GraphQLScalarType } from "graphql";
2
2
  import { Kind } from "graphql/language/index.js";
3
- import WebinyError from "@webiny/error";
4
- const parseValue = value => {
5
- if (String(value).match(/^0x/) !== null) {
6
- throw new WebinyError("Value sent must be a non-hex number.", "INVALID_VALUE", {
7
- value
3
+ import error from "@webiny/error";
4
+ const parseValue = (value)=>{
5
+ if (null !== String(value).match(/^0x/)) throw new error("Value sent must be a non-hex number.", "INVALID_VALUE", {
6
+ value
8
7
  });
9
- } else if (typeof value === "number") {
10
- return value;
11
- } else if (value === null || value === undefined) {
12
- return null;
13
- } else if (isNaN(value) === true) {
14
- throw new WebinyError("Value sent must be a number.", "INVALID_VALUE", {
15
- value
8
+ if ("number" == typeof value) return value;
9
+ if (null == value) return null;
10
+ if (true === isNaN(value)) throw new error("Value sent must be a number.", "INVALID_VALUE", {
11
+ value
16
12
  });
17
- }
18
- return parseFloat(value);
13
+ return parseFloat(value);
19
14
  };
20
- export const NumberScalar = new GraphQLScalarType({
21
- name: "Number",
22
- description: "A custom input type to be used with numbers. Supports Int and Float.",
23
- serialize: value => {
24
- try {
25
- return parseValue(value);
26
- } catch {
27
- console.log({
28
- message: "Value sent must be a number.",
29
- code: "INVALID_VALUE",
30
- data: {
31
- value
15
+ const NumberScalar = new GraphQLScalarType({
16
+ name: "Number",
17
+ description: "A custom input type to be used with numbers. Supports Int and Float.",
18
+ serialize: (value)=>{
19
+ try {
20
+ return parseValue(value);
21
+ } catch {
22
+ console.log({
23
+ message: "Value sent must be a number.",
24
+ code: "INVALID_VALUE",
25
+ data: {
26
+ value
27
+ }
28
+ });
29
+ return null;
32
30
  }
33
- });
34
- return null;
31
+ },
32
+ parseValue: parseValue,
33
+ parseLiteral: (ast)=>{
34
+ if (ast.kind === Kind.INT) return Number(ast.value);
35
+ if (ast.kind === Kind.FLOAT) return parseFloat(ast.value);
36
+ throw new Error(`Expected type Number, found {${ast.kind}}`);
35
37
  }
36
- },
37
- parseValue,
38
- parseLiteral: ast => {
39
- if (ast.kind === Kind.INT) {
40
- return Number(ast.value);
41
- } else if (ast.kind === Kind.FLOAT) {
42
- return parseFloat(ast.value);
43
- }
44
- throw new Error(`Expected type Number, found {${ast.kind}}`);
45
- }
46
38
  });
39
+ export { NumberScalar };
47
40
 
48
41
  //# sourceMappingURL=NumberScalar.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["GraphQLScalarType","Kind","WebinyError","parseValue","value","String","match","undefined","isNaN","parseFloat","NumberScalar","name","description","serialize","console","log","message","code","data","parseLiteral","ast","kind","INT","Number","FLOAT","Error"],"sources":["NumberScalar.ts"],"sourcesContent":["import { GraphQLScalarType } from \"graphql\";\nimport { Kind } from \"graphql/language/index.js\";\nimport WebinyError from \"@webiny/error\";\n\nconst parseValue = (value: any) => {\n if (String(value).match(/^0x/) !== null) {\n throw new WebinyError(\"Value sent must be a non-hex number.\", \"INVALID_VALUE\", {\n value\n });\n } else if (typeof value === \"number\") {\n return value;\n } else if (value === null || value === undefined) {\n return null;\n } else if (isNaN(value) === true) {\n throw new WebinyError(\"Value sent must be a number.\", \"INVALID_VALUE\", {\n value\n });\n }\n return parseFloat(value);\n};\n\nexport const NumberScalar = new GraphQLScalarType({\n name: \"Number\",\n description: \"A custom input type to be used with numbers. Supports Int and Float.\",\n serialize: (value: any) => {\n try {\n return parseValue(value);\n } catch {\n console.log({\n message: \"Value sent must be a number.\",\n code: \"INVALID_VALUE\",\n data: {\n value\n }\n });\n return null;\n }\n },\n parseValue,\n parseLiteral: ast => {\n if (ast.kind === Kind.INT) {\n return Number(ast.value);\n } else if (ast.kind === Kind.FLOAT) {\n return parseFloat(ast.value);\n }\n throw new Error(`Expected type Number, found {${ast.kind}}`);\n }\n});\n"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,SAAS;AAC3C,SAASC,IAAI,QAAQ,2BAA2B;AAChD,OAAOC,WAAW,MAAM,eAAe;AAEvC,MAAMC,UAAU,GAAIC,KAAU,IAAK;EAC/B,IAAIC,MAAM,CAACD,KAAK,CAAC,CAACE,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE;IACrC,MAAM,IAAIJ,WAAW,CAAC,sCAAsC,EAAE,eAAe,EAAE;MAC3EE;IACJ,CAAC,CAAC;EACN,CAAC,MAAM,IAAI,OAAOA,KAAK,KAAK,QAAQ,EAAE;IAClC,OAAOA,KAAK;EAChB,CAAC,MAAM,IAAIA,KAAK,KAAK,IAAI,IAAIA,KAAK,KAAKG,SAAS,EAAE;IAC9C,OAAO,IAAI;EACf,CAAC,MAAM,IAAIC,KAAK,CAACJ,KAAK,CAAC,KAAK,IAAI,EAAE;IAC9B,MAAM,IAAIF,WAAW,CAAC,8BAA8B,EAAE,eAAe,EAAE;MACnEE;IACJ,CAAC,CAAC;EACN;EACA,OAAOK,UAAU,CAACL,KAAK,CAAC;AAC5B,CAAC;AAED,OAAO,MAAMM,YAAY,GAAG,IAAIV,iBAAiB,CAAC;EAC9CW,IAAI,EAAE,QAAQ;EACdC,WAAW,EAAE,sEAAsE;EACnFC,SAAS,EAAGT,KAAU,IAAK;IACvB,IAAI;MACA,OAAOD,UAAU,CAACC,KAAK,CAAC;IAC5B,CAAC,CAAC,MAAM;MACJU,OAAO,CAACC,GAAG,CAAC;QACRC,OAAO,EAAE,8BAA8B;QACvCC,IAAI,EAAE,eAAe;QACrBC,IAAI,EAAE;UACFd;QACJ;MACJ,CAAC,CAAC;MACF,OAAO,IAAI;IACf;EACJ,CAAC;EACDD,UAAU;EACVgB,YAAY,EAAEC,GAAG,IAAI;IACjB,IAAIA,GAAG,CAACC,IAAI,KAAKpB,IAAI,CAACqB,GAAG,EAAE;MACvB,OAAOC,MAAM,CAACH,GAAG,CAAChB,KAAK,CAAC;IAC5B,CAAC,MAAM,IAAIgB,GAAG,CAACC,IAAI,KAAKpB,IAAI,CAACuB,KAAK,EAAE;MAChC,OAAOf,UAAU,CAACW,GAAG,CAAChB,KAAK,CAAC;IAChC;IACA,MAAM,IAAIqB,KAAK,CAAC,gCAAgCL,GAAG,CAACC,IAAI,GAAG,CAAC;EAChE;AACJ,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"builtInTypes/NumberScalar.js","sources":["../../src/builtInTypes/NumberScalar.ts"],"sourcesContent":["import { GraphQLScalarType } from \"graphql\";\nimport { Kind } from \"graphql/language/index.js\";\nimport WebinyError from \"@webiny/error\";\n\nconst parseValue = (value: any) => {\n if (String(value).match(/^0x/) !== null) {\n throw new WebinyError(\"Value sent must be a non-hex number.\", \"INVALID_VALUE\", {\n value\n });\n } else if (typeof value === \"number\") {\n return value;\n } else if (value === null || value === undefined) {\n return null;\n } else if (isNaN(value) === true) {\n throw new WebinyError(\"Value sent must be a number.\", \"INVALID_VALUE\", {\n value\n });\n }\n return parseFloat(value);\n};\n\nexport const NumberScalar = new GraphQLScalarType({\n name: \"Number\",\n description: \"A custom input type to be used with numbers. Supports Int and Float.\",\n serialize: (value: any) => {\n try {\n return parseValue(value);\n } catch {\n console.log({\n message: \"Value sent must be a number.\",\n code: \"INVALID_VALUE\",\n data: {\n value\n }\n });\n return null;\n }\n },\n parseValue,\n parseLiteral: ast => {\n if (ast.kind === Kind.INT) {\n return Number(ast.value);\n } else if (ast.kind === Kind.FLOAT) {\n return parseFloat(ast.value);\n }\n throw new Error(`Expected type Number, found {${ast.kind}}`);\n }\n});\n"],"names":["parseValue","value","String","WebinyError","isNaN","parseFloat","NumberScalar","GraphQLScalarType","console","ast","Kind","Number","Error"],"mappings":";;;AAIA,MAAMA,aAAa,CAACC;IAChB,IAAIC,AAA+B,SAA/BA,OAAOD,OAAO,KAAK,CAAC,QACpB,MAAM,IAAIE,MAAY,wCAAwC,iBAAiB;QAC3EF;IACJ;IACG,IAAI,AAAiB,YAAjB,OAAOA,OACd,OAAOA;IACJ,IAAIA,QAAAA,OACP,OAAO;IACJ,IAAIG,AAAiB,SAAjBA,MAAMH,QACb,MAAM,IAAIE,MAAY,gCAAgC,iBAAiB;QACnEF;IACJ;IAEJ,OAAOI,WAAWJ;AACtB;AAEO,MAAMK,eAAe,IAAIC,kBAAkB;IAC9C,MAAM;IACN,aAAa;IACb,WAAW,CAACN;QACR,IAAI;YACA,OAAOD,WAAWC;QACtB,EAAE,OAAM;YACJO,QAAQ,GAAG,CAAC;gBACR,SAAS;gBACT,MAAM;gBACN,MAAM;oBACFP;gBACJ;YACJ;YACA,OAAO;QACX;IACJ;IACAD,YAAAA;IACA,cAAcS,CAAAA;QACV,IAAIA,IAAI,IAAI,KAAKC,KAAK,GAAG,EACrB,OAAOC,OAAOF,IAAI,KAAK;QACpB,IAAIA,IAAI,IAAI,KAAKC,KAAK,KAAK,EAC9B,OAAOL,WAAWI,IAAI,KAAK;QAE/B,MAAM,IAAIG,MAAM,CAAC,6BAA6B,EAAEH,IAAI,IAAI,CAAC,CAAC,CAAC;IAC/D;AACJ"}
@@ -1,76 +1,53 @@
1
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!");
21
- }
22
- for (const test of tests) {
23
- if (test.re.test(value) === null) {
24
- throw new Error(test.message);
2
+ const tests = [
3
+ {
4
+ re: /^([0-9a-zA-Z_-]+)$/,
5
+ message: "Must be a string matching a-z, A-Z, 0-9, underscore (_) or dash(-)!"
6
+ },
7
+ {
8
+ re: /^-/,
9
+ message: "Must not start with a dash (-)!"
10
+ },
11
+ {
12
+ re: /-$/,
13
+ message: "Must not end with a dash (-)!"
14
+ },
15
+ {
16
+ re: /^_/,
17
+ message: "Must not start with an underscore (_)!"
18
+ },
19
+ {
20
+ re: /_$/,
21
+ message: "Must not end with an underscore (_)!"
25
22
  }
26
- }
27
- return value;
23
+ ];
24
+ const isValidId = (value)=>{
25
+ if ("string" != typeof value || value.length < 1) throw new Error("Must be a string with at least 1 character!");
26
+ for (const test of tests)if (null === test.re.test(value)) throw new Error(test.message);
27
+ return value;
28
28
  };
29
- export const RefInputScalar = new GraphQLScalarType({
30
- name: "RefInput",
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
- */
35
- serialize: value => {
36
- if (!value || value.id === null) {
37
- return null;
38
- }
39
- return typeof value === "string" ? value : value.id;
40
- },
41
- /**
42
- * We can set value as any because we are handling it.
43
- */
44
- parseValue: value => {
45
- if (!value || value.id === null) {
46
- return null;
47
- }
48
- if (typeof value === "string") {
49
- return isValidId(value);
50
- }
51
- if ("id" in value) {
52
- return isValidId(value.id);
53
- }
54
- throw new Error("Invalid RefInput value!");
55
- },
56
- parseLiteral: ast => {
57
- if (ast.kind === "StringValue") {
58
- return isValidId(ast.value);
59
- }
60
- if (ast.kind === "ObjectValue") {
61
- for (let i = 0; i < ast.fields.length; i++) {
62
- const {
63
- name,
64
- value
65
- } = ast.fields[i];
66
- if (name.value === "id") {
67
- // @ts-expect-error
68
- return isValidId(value.value);
29
+ const RefInputScalar = new GraphQLScalarType({
30
+ name: "RefInput",
31
+ description: "A custom input type to be used with references. Supports plain ID and `{ id: ID }` Object literal.",
32
+ serialize: (value)=>{
33
+ if (!value || null === value.id) return null;
34
+ return "string" == typeof value ? value : value.id;
35
+ },
36
+ parseValue: (value)=>{
37
+ if (!value || null === value.id) return null;
38
+ if ("string" == typeof value) return isValidId(value);
39
+ if ("id" in value) return isValidId(value.id);
40
+ throw new Error("Invalid RefInput value!");
41
+ },
42
+ parseLiteral: (ast)=>{
43
+ if ("StringValue" === ast.kind) return isValidId(ast.value);
44
+ if ("ObjectValue" === ast.kind) for(let i = 0; i < ast.fields.length; i++){
45
+ const { name, value } = ast.fields[i];
46
+ if ("id" === name.value) return isValidId(value.value);
69
47
  }
70
- }
48
+ throw new Error("Invalid RefInput value!");
71
49
  }
72
- throw new Error("Invalid RefInput value!");
73
- }
74
50
  });
51
+ export { RefInputScalar };
75
52
 
76
53
  //# sourceMappingURL=RefInputScalar.js.map
@@ -1 +1 @@
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,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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,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
+ {"version":3,"file":"builtInTypes/RefInputScalar.js","sources":["../../src/builtInTypes/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"],"names":["tests","isValidId","value","Error","test","RefInputScalar","GraphQLScalarType","ast","i","name"],"mappings":";AAEA,MAAMA,QAAQ;IACV;QACI,IAAI;QACJ,SAAS;IACb;IACA;QACI,IAAI;QACJ,SAAS;IACb;IACA;QACI,IAAI;QACJ,SAAS;IACb;IACA;QACI,IAAI;QACJ,SAAS;IACb;IACA;QACI,IAAI;QACJ,SAAS;IACb;CACH;AAED,MAAMC,YAAY,CAACC;IACf,IAAI,AAAiB,YAAjB,OAAOA,SAAsBA,MAAM,MAAM,GAAG,GAC5C,MAAM,IAAIC,MAAM;IAEpB,KAAK,MAAMC,QAAQJ,MACf,IAAII,AAAwB,SAAxBA,KAAK,EAAE,CAAC,IAAI,CAACF,QACb,MAAM,IAAIC,MAAMC,KAAK,OAAO;IAGpC,OAAOF;AACX;AAEO,MAAMG,iBAAiB,IAAIC,kBAAkB;IAChD,MAAM;IACN,aACI;IAIJ,WAAW,CAACJ;QACR,IAAI,CAACA,SAASA,AAAa,SAAbA,MAAM,EAAE,EAClB,OAAO;QAGX,OAAO,AAAiB,YAAjB,OAAOA,QAAqBA,QAAQA,MAAM,EAAE;IACvD;IAIA,YAAY,CAACA;QACT,IAAI,CAACA,SAASA,AAAa,SAAbA,MAAM,EAAE,EAClB,OAAO;QAGX,IAAI,AAAiB,YAAjB,OAAOA,OACP,OAAOD,UAAUC;QAGrB,IAAI,QAAQA,OACR,OAAOD,UAAUC,MAAM,EAAE;QAG7B,MAAM,IAAIC,MAAM;IACpB;IACA,cAAcI,CAAAA;QACV,IAAIA,AAAa,kBAAbA,IAAI,IAAI,EACR,OAAON,UAAUM,IAAI,KAAK;QAG9B,IAAIA,AAAa,kBAAbA,IAAI,IAAI,EACR,IAAK,IAAIC,IAAI,GAAGA,IAAID,IAAI,MAAM,CAAC,MAAM,EAAEC,IAAK;YACxC,MAAM,EAAEC,IAAI,EAAEP,KAAK,EAAE,GAAGK,IAAI,MAAM,CAACC,EAAE;YACrC,IAAIC,AAAe,SAAfA,KAAK,KAAK,EAEV,OAAOR,UAAUC,MAAM,KAAK;QAEpC;QAGJ,MAAM,IAAIC,MAAM;IACpB;AACJ"}
@@ -1,53 +1,40 @@
1
1
  import { GraphQLScalarType } from "graphql";
2
- import WebinyError from "@webiny/error";
2
+ import error from "@webiny/error";
3
3
  const re = /^([0-9]{2}):([0-9]{2})(:([0-9]{2}))?$/;
4
- const parseTime = value => {
5
- if (typeof value !== "string" || !value || value.match(re) === null) {
6
- throw new WebinyError("Value does not look like time.", "TIME_VALIDATION_ERROR", {
7
- value
4
+ const parseTime = (value)=>{
5
+ if ("string" != typeof value || !value || null === value.match(re)) throw new error("Value does not look like time.", "TIME_VALIDATION_ERROR", {
6
+ value
8
7
  });
9
- }
10
- const parsed = value.split(":").map(Number);
11
- if (parsed.length < 2) {
12
- throw new WebinyError(`Could not parse the value.`, "TIME_VALIDATION_ERROR", {
13
- value
8
+ const parsed = value.split(":").map(Number);
9
+ if (parsed.length < 2) throw new error("Could not parse the value.", "TIME_VALIDATION_ERROR", {
10
+ value
14
11
  });
15
- }
16
- const [hours, minutes, seconds = 0] = parsed;
17
- if (hours >= 24) {
18
- throw new WebinyError(`There cannot be more than 24 hours.`, "TIME_VALIDATION_ERROR", {
19
- value
12
+ const [hours, minutes, seconds = 0] = parsed;
13
+ if (hours >= 24) throw new error("There cannot be more than 24 hours.", "TIME_VALIDATION_ERROR", {
14
+ value
20
15
  });
21
- } else if (minutes >= 60) {
22
- throw new WebinyError(`There cannot be more than 59 minutes.`, "TIME_VALIDATION_ERROR", {
23
- value
16
+ if (minutes >= 60) throw new error("There cannot be more than 59 minutes.", "TIME_VALIDATION_ERROR", {
17
+ value
24
18
  });
25
- } else if (seconds >= 60) {
26
- throw new WebinyError(`There cannot be more than 59 seconds.`, "TIME_VALIDATION_ERROR", {
27
- value
19
+ if (seconds >= 60) throw new error("There cannot be more than 59 seconds.", "TIME_VALIDATION_ERROR", {
20
+ value
28
21
  });
29
- }
30
- return {
31
- hours,
32
- minutes,
33
- seconds
34
- };
22
+ return {
23
+ hours,
24
+ minutes,
25
+ seconds
26
+ };
35
27
  };
36
- const convertToTime = value => {
37
- const {
38
- hours,
39
- minutes,
40
- seconds
41
- } = parseTime(value);
42
- return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
28
+ const convertToTime = (value)=>{
29
+ const { hours, minutes, seconds } = parseTime(value);
30
+ return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
43
31
  };
44
- export const TimeScalar = new GraphQLScalarType({
45
- name: "Time",
46
- description: "A custom type to support time-only input.",
47
- // sending to client
48
- serialize: convertToTime,
49
- // received from client
50
- parseValue: convertToTime
32
+ const TimeScalar = new GraphQLScalarType({
33
+ name: "Time",
34
+ description: "A custom type to support time-only input.",
35
+ serialize: convertToTime,
36
+ parseValue: convertToTime
51
37
  });
38
+ export { TimeScalar };
52
39
 
53
40
  //# sourceMappingURL=TimeScalar.js.map
@@ -1 +1 @@
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,EA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,CAAC,EAAE,GAAG,CAAC,IAAID,MAAM,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
+ {"version":3,"file":"builtInTypes/TimeScalar.js","sources":["../../src/builtInTypes/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"],"names":["re","parseTime","value","WebinyError","parsed","Number","hours","minutes","seconds","convertToTime","String","TimeScalar","GraphQLScalarType"],"mappings":";;AAGA,MAAMA,KAAK;AAEX,MAAMC,YAAY,CAACC;IACf,IAAI,AAAiB,YAAjB,OAAOA,SAAsB,CAACA,SAASA,AAAoB,SAApBA,MAAM,KAAK,CAACF,KACnD,MAAM,IAAIG,MAAY,kCAAkC,yBAAyB;QAAED;IAAM;IAE7F,MAAME,SAASF,MAAM,KAAK,CAAC,KAAK,GAAG,CAACG;IACpC,IAAID,OAAO,MAAM,GAAG,GAChB,MAAM,IAAID,MAAY,8BAA8B,yBAAyB;QAAED;IAAM;IAEzF,MAAM,CAACI,OAAOC,SAASC,UAAU,CAAC,CAAC,GAAGJ;IACtC,IAAIE,SAAS,IACT,MAAM,IAAIH,MAAY,uCAAuC,yBAAyB;QAClFD;IACJ;IACG,IAAIK,WAAW,IAClB,MAAM,IAAIJ,MAAY,yCAAyC,yBAAyB;QACpFD;IACJ;IACG,IAAIM,WAAW,IAClB,MAAM,IAAIL,MAAY,yCAAyC,yBAAyB;QACpFD;IACJ;IAEJ,OAAO;QACHI;QACAC;QACAC;IACJ;AACJ;AAEA,MAAMC,gBAAgB,CAACP;IACnB,MAAM,EAAEI,KAAK,EAAEC,OAAO,EAAEC,OAAO,EAAE,GAAGP,UAAUC;IAC9C,OAAO,GAAGQ,OAAOJ,OAAO,QAAQ,CAAC,GAAG,KAAK,CAAC,EAAEI,OAAOH,SAAS,QAAQ,CAAC,GAAG,KAAK,CAAC,EAAEG,OAC5EF,SACF,QAAQ,CAAC,GAAG,MAAM;AACxB;AAEO,MAAMG,aAAa,IAAIC,kBAAkB;IAC5C,MAAM;IACN,aAAa;IAEb,WAAWH;IAEX,YAAYA;AAChB"}
@@ -8,5 +8,3 @@ export * from "./NumberScalar.js";
8
8
  export * from "./RefInputScalar.js";
9
9
  export * from "./TimeScalar.js";
10
10
  export * from "./IconScalar.js";
11
-
12
- //# sourceMappingURL=index.js.map
@@ -1,93 +1,82 @@
1
- import { boolean } from "boolean";
1
+ import { boolean as external_boolean_boolean } from "boolean";
2
2
  import { RoutePlugin } from "@webiny/handler";
3
- import WebinyError from "@webiny/error";
3
+ import _webiny_error from "@webiny/error";
4
4
  import { createGraphQLSchema, getSchemaPlugins } from "./createGraphQLSchema.js";
5
5
  import debugPlugins from "./debugPlugins.js";
6
6
  import { processRequestBody } from "./processRequestBody.js";
7
7
  import { createRequestBody } from "./createRequestBody.js";
8
- const DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year
9
-
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("#");
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("#");
19
16
  };
20
- const formatErrorPayload = error => {
21
- if (error instanceof WebinyError) {
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
+ });
22
25
  return JSON.stringify({
23
- type: "CoreGraphQLWebinyError",
24
- message: error.message,
25
- code: error.code,
26
- data: error.data,
27
- stack: error.stack
26
+ type: "Error",
27
+ name: error.name,
28
+ message: error.message,
29
+ stack: error.stack
28
30
  });
29
- }
30
- return JSON.stringify({
31
- type: "Error",
32
- name: error.name,
33
- message: error.message,
34
- stack: error.stack
35
- });
36
31
  };
37
- export default (options = {}) => {
38
- let schema = undefined;
39
- let cacheKey = undefined;
40
- const debug = boolean(options.debug);
41
- const path = options?.path || "/graphql";
42
- const route = new RoutePlugin(async ({
43
- onPost,
44
- onOptions,
45
- context
46
- }) => {
47
- onOptions(path, async (_, reply) => {
48
- return reply.status(204).headers({
49
- "Cache-Control": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`
50
- }).send({}).hijack();
51
- });
52
- onPost(path, async (request, reply) => {
53
- const contextCacheKey = createCacheKey(context);
54
- if (!schema || cacheKey !== contextCacheKey) {
55
- try {
56
- schema = await createGraphQLSchema(context);
57
- cacheKey = contextCacheKey;
58
- } catch (ex) {
59
- return reply.code(500).send(formatErrorPayload(ex));
60
- }
61
- }
62
- let body;
63
- try {
64
- body = createRequestBody(request.body);
65
- } catch (ex) {
66
- console.error(`Error while creating the body request.`);
67
- console.error(formatErrorPayload(ex));
68
- throw ex;
69
- }
70
- try {
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
- }
79
- return reply.status(200).send(result);
80
- } catch (ex) {
81
- console.error(`Error while processing the body request.`);
82
- console.error(formatErrorPayload(ex));
83
- throw ex;
84
- }
32
+ const createGraphQLHandler = (options = {})=>{
33
+ let schema;
34
+ let cacheKey;
35
+ const debug = external_boolean_boolean(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
+ });
85
70
  });
86
- });
87
- route.name = "handler.graphql.route.default";
88
- return [...(debug ? debugPlugins() : []), {
89
- type: "wcp-telemetry-tracker"
90
- }, route];
71
+ route.name = "handler.graphql.route.default";
72
+ return [
73
+ ...debug ? debugPlugins() : [],
74
+ {
75
+ type: "wcp-telemetry-tracker"
76
+ },
77
+ route
78
+ ];
91
79
  };
80
+ export default createGraphQLHandler;
92
81
 
93
82
  //# sourceMappingURL=createGraphQLHandler.js.map
@@ -1 +1 @@
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,iBAAiB;AAE1B,MAAMC,qBAAqB,GAAG,QAAQ,CAAC,CAAC;;AAExC,MAAMC,cAAc,GAAIC,OAAgB,IAAK;EACzC,MAAMC,OAAO,GAAGP,gBAAgB,CAACM,OAAO,CAAC;EACzC;EACA;;EAEA;EACA;EACA,MAAME,MAAM,GAAGF,OAAO,CAACG,OAAO,EAAEC,gBAAgB,CAAC,CAAC;EAElD,OAAO,CAACF,MAAM,GAAG,UAAUA,MAAM,CAACG,EAAE,EAAE,GAAG,IAAI,EAAEJ,OAAO,CAACK,MAAM,CAACC,QAAQ,CAAC,CAAC,CAAC,CACpEC,MAAM,CAACC,OAAO,CAAC,CACfC,IAAI,CAAC,GAAG,CAAC;AAClB,CAAC;AAED,MAAMC,kBAAkB,GAAIC,KAAY,IAAa;EACjD,IAAIA,KAAK,YAAYpB,WAAW,EAAE;IAC9B,OAAOqB,IAAI,CAACC,SAAS,CAA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":["../src/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"],"names":["DEFAULT_CACHE_MAX_AGE","createCacheKey","context","plugins","getSchemaPlugins","tenant","Boolean","formatErrorPayload","error","WebinyError","JSON","options","schema","cacheKey","debug","boolean","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,MA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