@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.
- package/ResolverDecoration.js +18 -14
- package/ResolverDecoration.js.map +1 -1
- package/builtInTypes/AnyScalar.js +7 -8
- package/builtInTypes/AnyScalar.js.map +1 -1
- package/builtInTypes/DateScalar.js +12 -18
- package/builtInTypes/DateScalar.js.map +1 -1
- package/builtInTypes/DateTimeScalar.js +12 -18
- package/builtInTypes/DateTimeScalar.js.map +1 -1
- package/builtInTypes/DateTimeZScalar.js +23 -35
- package/builtInTypes/DateTimeZScalar.js.map +1 -1
- package/builtInTypes/IconScalar.js +59 -87
- package/builtInTypes/IconScalar.js.map +1 -1
- package/builtInTypes/JsonScalar.js +2 -1
- package/builtInTypes/JsonScalar.js.map +1 -1
- package/builtInTypes/LongScalar.js +30 -39
- package/builtInTypes/LongScalar.js.map +1 -1
- package/builtInTypes/NumberScalar.js +31 -38
- package/builtInTypes/NumberScalar.js.map +1 -1
- package/builtInTypes/RefInputScalar.js +45 -68
- package/builtInTypes/RefInputScalar.js.map +1 -1
- package/builtInTypes/TimeScalar.js +28 -41
- package/builtInTypes/TimeScalar.js.map +1 -1
- package/builtInTypes/index.js +0 -2
- package/createGraphQLHandler.js +69 -80
- package/createGraphQLHandler.js.map +1 -1
- package/createGraphQLSchema.js +49 -66
- package/createGraphQLSchema.js.map +1 -1
- package/createRequestBody.js +20 -18
- package/createRequestBody.js.map +1 -1
- package/createResolverDecorator.js +2 -3
- package/createResolverDecorator.js.map +1 -1
- package/debugPlugins.js +24 -27
- package/debugPlugins.js.map +1 -1
- package/errors.js +6 -5
- package/errors.js.map +1 -1
- package/exports/api/graphql.js +1 -3
- package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js +52 -59
- package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js.map +1 -1
- package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js +27 -21
- package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js.map +1 -1
- package/features/GraphQLSchemaBuilder/abstractions.js +3 -2
- package/features/GraphQLSchemaBuilder/abstractions.js.map +1 -1
- package/features/GraphQLSchemaBuilder/feature.js +6 -5
- package/features/GraphQLSchemaBuilder/feature.js.map +1 -1
- package/graphql/abstractions.core.js +2 -7
- package/graphql/abstractions.core.js.map +1 -1
- package/graphql/abstractions.js +0 -2
- package/graphql/abstractions.public.js +2 -8
- package/graphql/abstractions.public.js.map +1 -1
- package/index.js +2 -3
- package/index.js.map +1 -1
- package/interceptConsole.js +30 -27
- package/interceptConsole.js.map +1 -1
- package/package.json +13 -13
- package/plugins/GraphQLSchemaPlugin.js +20 -21
- package/plugins/GraphQLSchemaPlugin.js.map +1 -1
- package/plugins/index.js +0 -2
- package/processRequestBody.js +33 -36
- package/processRequestBody.js.map +1 -1
- package/responses.js +48 -65
- package/responses.js.map +1 -1
- package/types.js +0 -3
- package/utils/index.js +0 -2
- package/utils/resolve.js +15 -14
- package/utils/resolve.js.map +1 -1
- package/builtInTypes/index.js.map +0 -1
- package/exports/api/graphql.js.map +0 -1
- package/graphql/abstractions.js.map +0 -1
- package/plugins/index.js.map +0 -1
- package/types.js.map +0 -1
- 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
|
|
4
|
-
const parseValue = value
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
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,"
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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,"
|
|
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
|
|
2
|
+
import error from "@webiny/error";
|
|
3
3
|
const re = /^([0-9]{2}):([0-9]{2})(:([0-9]{2}))?$/;
|
|
4
|
-
const parseTime = value
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
value
|
|
16
|
+
if (minutes >= 60) throw new error("There cannot be more than 59 minutes.", "TIME_VALIDATION_ERROR", {
|
|
17
|
+
value
|
|
24
18
|
});
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
22
|
+
return {
|
|
23
|
+
hours,
|
|
24
|
+
minutes,
|
|
25
|
+
seconds
|
|
26
|
+
};
|
|
35
27
|
};
|
|
36
|
-
const convertToTime = value
|
|
37
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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,"
|
|
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"}
|
package/builtInTypes/index.js
CHANGED
package/createGraphQLHandler.js
CHANGED
|
@@ -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
|
|
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;
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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,"
|
|
1
|
+
{"version":3,"file":"createGraphQLHandler.js","sources":["../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,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,yBAAQJ,QAAQ,KAAK;IAEnC,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"}
|