@webiny/handler-graphql 0.0.0-unstable.78f581c1d2 → 0.0.0-unstable.7be00a75a9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -5
- package/ResolverDecoration.d.ts +6 -0
- package/ResolverDecoration.js +23 -0
- package/ResolverDecoration.js.map +1 -0
- package/builtInTypes/AnyScalar.d.ts +1 -1
- package/builtInTypes/AnyScalar.js +9 -18
- package/builtInTypes/AnyScalar.js.map +1 -1
- package/builtInTypes/DateScalar.d.ts +1 -1
- package/builtInTypes/DateScalar.js +16 -32
- package/builtInTypes/DateScalar.js.map +1 -1
- package/builtInTypes/DateTimeScalar.d.ts +1 -1
- package/builtInTypes/DateTimeScalar.js +16 -32
- package/builtInTypes/DateTimeScalar.js.map +1 -1
- package/builtInTypes/DateTimeZScalar.d.ts +1 -1
- package/builtInTypes/DateTimeZScalar.js +27 -58
- package/builtInTypes/DateTimeZScalar.js.map +1 -1
- package/builtInTypes/IconScalar.d.ts +10 -0
- package/builtInTypes/IconScalar.js +65 -0
- package/builtInTypes/IconScalar.js.map +1 -0
- package/builtInTypes/JsonScalar.d.ts +1 -1
- package/builtInTypes/JsonScalar.js +4 -10
- package/builtInTypes/JsonScalar.js.map +1 -1
- package/builtInTypes/LongScalar.d.ts +2 -1
- package/builtInTypes/LongScalar.js +38 -9
- package/builtInTypes/LongScalar.js.map +1 -1
- package/builtInTypes/NumberScalar.d.ts +1 -1
- package/builtInTypes/NumberScalar.js +35 -58
- package/builtInTypes/NumberScalar.js.map +1 -1
- package/builtInTypes/RefInputScalar.d.ts +1 -1
- package/builtInTypes/RefInputScalar.js +48 -60
- package/builtInTypes/RefInputScalar.js.map +1 -1
- package/builtInTypes/TimeScalar.d.ts +1 -1
- package/builtInTypes/TimeScalar.js +31 -62
- package/builtInTypes/TimeScalar.js.map +1 -1
- package/builtInTypes/index.d.ts +10 -9
- package/builtInTypes/index.js +10 -122
- package/createGraphQLHandler.d.ts +3 -3
- package/createGraphQLHandler.js +77 -55
- package/createGraphQLHandler.js.map +1 -1
- package/createGraphQLSchema.d.ts +4 -2
- package/createGraphQLSchema.js +67 -54
- package/createGraphQLSchema.js.map +1 -1
- package/createRequestBody.d.ts +2 -0
- package/createRequestBody.js +28 -0
- package/createRequestBody.js.map +1 -0
- package/createResolverDecorator.d.ts +2 -0
- package/createResolverDecorator.js +4 -0
- package/createResolverDecorator.js.map +1 -0
- package/debugPlugins.d.ts +2 -2
- package/debugPlugins.js +28 -45
- package/debugPlugins.js.map +1 -1
- package/errors.js +7 -17
- package/errors.js.map +1 -1
- package/exports/api/graphql.d.ts +2 -0
- package/exports/api/graphql.js +2 -0
- package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.d.ts +16 -0
- package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js +57 -0
- package/features/GraphQLSchemaBuilder/GraphQLSchemaBuilder.js.map +1 -0
- package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.d.ts +13 -0
- package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js +35 -0
- package/features/GraphQLSchemaBuilder/GraphQLSchemaComposer.js.map +1 -0
- package/features/GraphQLSchemaBuilder/abstractions.d.ts +34 -0
- package/features/GraphQLSchemaBuilder/abstractions.js +6 -0
- package/features/GraphQLSchemaBuilder/abstractions.js.map +1 -0
- package/features/GraphQLSchemaBuilder/feature.d.ts +4 -0
- package/features/GraphQLSchemaBuilder/feature.js +11 -0
- package/features/GraphQLSchemaBuilder/feature.js.map +1 -0
- package/graphql/abstractions.core.d.ts +11 -0
- package/graphql/abstractions.core.js +5 -0
- package/graphql/abstractions.core.js.map +1 -0
- package/graphql/abstractions.d.ts +2 -0
- package/graphql/abstractions.js +2 -0
- package/graphql/abstractions.public.d.ts +18 -0
- package/graphql/abstractions.public.js +5 -0
- package/graphql/abstractions.public.js.map +1 -0
- package/index.d.ts +10 -5
- package/index.js +12 -57
- package/index.js.map +1 -1
- package/interceptConsole.js +31 -39
- package/interceptConsole.js.map +1 -1
- package/package.json +32 -29
- package/plugins/GraphQLSchemaPlugin.d.ts +13 -5
- package/plugins/GraphQLSchemaPlugin.js +23 -29
- package/plugins/GraphQLSchemaPlugin.js.map +1 -1
- package/plugins/index.d.ts +1 -1
- package/plugins/index.js +1 -18
- package/processRequestBody.d.ts +4 -5
- package/processRequestBody.js +35 -42
- package/processRequestBody.js.map +1 -1
- package/responses.d.ts +11 -1
- package/responses.js +44 -92
- package/responses.js.map +1 -1
- package/types.d.ts +20 -13
- package/types.js +0 -5
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/resolve.d.ts +21 -0
- package/utils/resolve.js +20 -0
- package/utils/resolve.js.map +1 -0
- package/builtInTypes/index.js.map +0 -1
- package/plugins/index.js.map +0 -1
- package/types.js.map +0 -1
package/createGraphQLHandler.js
CHANGED
|
@@ -1,60 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var _processRequestBody = _interopRequireDefault(require("./processRequestBody"));
|
|
17
|
-
|
|
18
|
-
var _handler = require("@webiny/handler");
|
|
19
|
-
|
|
20
|
-
const DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* TODO Until we figure out how to better convert incoming body, we will leave it as any.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const createRequestBody = body => {
|
|
27
|
-
return typeof body === "string" ? JSON.parse(body) : body;
|
|
1
|
+
import { toBoolean } from "@webiny/stdlib";
|
|
2
|
+
import { RoutePlugin } from "@webiny/handler";
|
|
3
|
+
import _webiny_error from "@webiny/error";
|
|
4
|
+
import { createGraphQLSchema, getSchemaPlugins } from "./createGraphQLSchema.js";
|
|
5
|
+
import debugPlugins from "./debugPlugins.js";
|
|
6
|
+
import { processRequestBody } from "./processRequestBody.js";
|
|
7
|
+
import { createRequestBody } from "./createRequestBody.js";
|
|
8
|
+
const DEFAULT_CACHE_MAX_AGE = 30758400;
|
|
9
|
+
const createCacheKey = (context)=>{
|
|
10
|
+
const plugins = getSchemaPlugins(context);
|
|
11
|
+
const tenant = context.tenancy?.getCurrentTenant();
|
|
12
|
+
return [
|
|
13
|
+
tenant ? `tenant:${tenant.id}` : null,
|
|
14
|
+
plugins.length.toString()
|
|
15
|
+
].filter(Boolean).join("#");
|
|
28
16
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
onOptions,
|
|
37
|
-
context
|
|
38
|
-
}) => {
|
|
39
|
-
onOptions(path, async (_, reply) => {
|
|
40
|
-
return reply.status(204).headers({
|
|
41
|
-
"Cache-Control": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`
|
|
42
|
-
}).send({});
|
|
17
|
+
const formatErrorPayload = (error)=>{
|
|
18
|
+
if (error instanceof _webiny_error) return JSON.stringify({
|
|
19
|
+
type: "CoreGraphQLWebinyError",
|
|
20
|
+
message: error.message,
|
|
21
|
+
code: error.code,
|
|
22
|
+
data: error.data,
|
|
23
|
+
stack: error.stack
|
|
43
24
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
25
|
+
return JSON.stringify({
|
|
26
|
+
type: "Error",
|
|
27
|
+
name: error.name,
|
|
28
|
+
message: error.message,
|
|
29
|
+
stack: error.stack
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
const createGraphQLHandler = (options = {})=>{
|
|
33
|
+
let schema;
|
|
34
|
+
let cacheKey;
|
|
35
|
+
const debug = toBoolean(options.debug);
|
|
36
|
+
const path = options?.path || "/graphql";
|
|
37
|
+
const route = new RoutePlugin(async ({ onPost, onOptions, context })=>{
|
|
38
|
+
onOptions(path, async (_, reply)=>reply.status(204).headers({
|
|
39
|
+
"Cache-Control": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`
|
|
40
|
+
}).send({}).hijack());
|
|
41
|
+
onPost(path, async (request, reply)=>{
|
|
42
|
+
const contextCacheKey = createCacheKey(context);
|
|
43
|
+
if (!schema || cacheKey !== contextCacheKey) try {
|
|
44
|
+
schema = await createGraphQLSchema(context);
|
|
45
|
+
cacheKey = contextCacheKey;
|
|
46
|
+
} catch (ex) {
|
|
47
|
+
return reply.code(500).send(formatErrorPayload(ex));
|
|
48
|
+
}
|
|
49
|
+
let body;
|
|
50
|
+
try {
|
|
51
|
+
body = createRequestBody(request.body);
|
|
52
|
+
} catch (ex) {
|
|
53
|
+
console.error("Error while creating the body request.");
|
|
54
|
+
console.error(formatErrorPayload(ex));
|
|
55
|
+
throw ex;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const result = await processRequestBody(body, schema, context);
|
|
59
|
+
if (reply.sent) {
|
|
60
|
+
console.warn("Reply already sent, cannot send the result (handler-graphql).");
|
|
61
|
+
return reply;
|
|
62
|
+
}
|
|
63
|
+
return reply.status(200).send(result);
|
|
64
|
+
} catch (ex) {
|
|
65
|
+
console.error("Error while processing the body request.");
|
|
66
|
+
console.error(formatErrorPayload(ex));
|
|
67
|
+
throw ex;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
52
70
|
});
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
71
|
+
route.name = "handler.graphql.route.default";
|
|
72
|
+
return [
|
|
73
|
+
...debug ? debugPlugins() : [],
|
|
74
|
+
{
|
|
75
|
+
type: "wcp-telemetry-tracker"
|
|
76
|
+
},
|
|
77
|
+
route
|
|
78
|
+
];
|
|
58
79
|
};
|
|
80
|
+
export default createGraphQLHandler;
|
|
59
81
|
|
|
60
|
-
|
|
82
|
+
//# sourceMappingURL=createGraphQLHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"createGraphQLHandler.js","sources":["../src/createGraphQLHandler.ts"],"sourcesContent":["import { toBoolean } from \"@webiny/stdlib\";\nimport type { GraphQLSchema } from \"graphql\";\nimport type { Context } from \"@webiny/handler\";\nimport { RoutePlugin } from \"@webiny/handler\";\nimport WebinyError from \"@webiny/error\";\nimport type { Plugin } from \"@webiny/plugins/types.js\";\nimport type { GraphQLRequestBody, HandlerGraphQLOptions } from \"./types.js\";\nimport { createGraphQLSchema, getSchemaPlugins } from \"./createGraphQLSchema.js\";\nimport debugPlugins from \"./debugPlugins.js\";\nimport { processRequestBody } from \"./processRequestBody.js\";\nimport { createRequestBody } from \"~/createRequestBody.js\";\n\nconst DEFAULT_CACHE_MAX_AGE = 30758400; // 1 year\n\nconst createCacheKey = (context: Context) => {\n const plugins = getSchemaPlugins(context);\n // TODO: in the near future, we have to assign a fixed name to every\n // TODO: GraphQLSchema plugin, to be able to create a reliable cache key.\n\n // TODO: `getCurrentTenant` should be injected as a parameter.\n // @ts-expect-error TODO: We should not be accessing `context` like this here.\n const tenant = context.tenancy?.getCurrentTenant();\n\n return [tenant ? `tenant:${tenant.id}` : null, plugins.length.toString()]\n .filter(Boolean)\n .join(\"#\");\n};\n\nconst formatErrorPayload = (error: Error): string => {\n if (error instanceof WebinyError) {\n return JSON.stringify({\n type: \"CoreGraphQLWebinyError\",\n message: error.message,\n code: error.code,\n data: error.data,\n stack: error.stack\n });\n }\n\n return JSON.stringify({\n type: \"Error\",\n name: error.name,\n message: error.message,\n stack: error.stack\n });\n};\n\nexport default (options: HandlerGraphQLOptions = {}): Plugin[] => {\n let schema: GraphQLSchema | undefined = undefined;\n let cacheKey: string | undefined = undefined;\n\n const debug = toBoolean(options.debug);\n\n const path = options?.path || \"/graphql\";\n\n const route = new RoutePlugin(async ({ onPost, onOptions, context }) => {\n onOptions(path, async (_, reply) => {\n return reply\n .status(204)\n .headers({\n \"Cache-Control\": `public, max-age=${DEFAULT_CACHE_MAX_AGE}`\n })\n .send({})\n .hijack();\n });\n onPost(path, async (request, reply) => {\n const contextCacheKey = createCacheKey(context as Context);\n if (!schema || cacheKey !== contextCacheKey) {\n try {\n schema = await createGraphQLSchema(context);\n cacheKey = contextCacheKey;\n } catch (ex) {\n return reply.code(500).send(formatErrorPayload(ex));\n }\n }\n let body: GraphQLRequestBody | GraphQLRequestBody[];\n try {\n body = createRequestBody(request.body);\n } catch (ex) {\n console.error(`Error while creating the body request.`);\n console.error(formatErrorPayload(ex));\n throw ex;\n }\n try {\n const result = await processRequestBody(body, schema, context);\n /**\n * IMPORTANT! Do not send anything if reply was already sent.\n */\n if (reply.sent) {\n console.warn(\"Reply already sent, cannot send the result (handler-graphql).\");\n return reply;\n }\n return reply.status(200).send(result);\n } catch (ex) {\n console.error(`Error while processing the body request.`);\n console.error(formatErrorPayload(ex));\n throw ex;\n }\n });\n });\n\n route.name = \"handler.graphql.route.default\";\n\n return [\n ...(debug ? debugPlugins() : []),\n {\n type: \"wcp-telemetry-tracker\"\n },\n route\n ];\n};\n"],"names":["DEFAULT_CACHE_MAX_AGE","createCacheKey","context","plugins","getSchemaPlugins","tenant","Boolean","formatErrorPayload","error","WebinyError","JSON","options","schema","cacheKey","debug","toBoolean","path","route","RoutePlugin","onPost","onOptions","_","reply","request","contextCacheKey","createGraphQLSchema","ex","body","createRequestBody","console","result","processRequestBody","debugPlugins"],"mappings":";;;;;;;AAYA,MAAMA,wBAAwB;AAE9B,MAAMC,iBAAiB,CAACC;IACpB,MAAMC,UAAUC,iBAAiBF;IAMjC,MAAMG,SAASH,QAAQ,OAAO,EAAE;IAEhC,OAAO;QAACG,SAAS,CAAC,OAAO,EAAEA,OAAO,EAAE,EAAE,GAAG;QAAMF,QAAQ,MAAM,CAAC,QAAQ;KAAG,CACpE,MAAM,CAACG,SACP,IAAI,CAAC;AACd;AAEA,MAAMC,qBAAqB,CAACC;IACxB,IAAIA,iBAAiBC,eACjB,OAAOC,KAAK,SAAS,CAAC;QAClB,MAAM;QACN,SAASF,MAAM,OAAO;QACtB,MAAMA,MAAM,IAAI;QAChB,MAAMA,MAAM,IAAI;QAChB,OAAOA,MAAM,KAAK;IACtB;IAGJ,OAAOE,KAAK,SAAS,CAAC;QAClB,MAAM;QACN,MAAMF,MAAM,IAAI;QAChB,SAASA,MAAM,OAAO;QACtB,OAAOA,MAAM,KAAK;IACtB;AACJ;AAEA,6BAAgB,CAAAG,UAAiC,CAAC,CAAC;IAC/C,IAAIC;IACJ,IAAIC;IAEJ,MAAMC,QAAQC,UAAUJ,QAAQ,KAAK;IAErC,MAAMK,OAAOL,SAAS,QAAQ;IAE9B,MAAMM,QAAQ,IAAIC,YAAY,OAAO,EAAEC,MAAM,EAAEC,SAAS,EAAElB,OAAO,EAAE;QAC/DkB,UAAUJ,MAAM,OAAOK,GAAGC,QACfA,MACF,MAAM,CAAC,KACP,OAAO,CAAC;gBACL,iBAAiB,CAAC,gBAAgB,EAAEtB,uBAAuB;YAC/D,GACC,IAAI,CAAC,CAAC,GACN,MAAM;QAEfmB,OAAOH,MAAM,OAAOO,SAASD;YACzB,MAAME,kBAAkBvB,eAAeC;YACvC,IAAI,CAACU,UAAUC,aAAaW,iBACxB,IAAI;gBACAZ,SAAS,MAAMa,oBAAoBvB;gBACnCW,WAAWW;YACf,EAAE,OAAOE,IAAI;gBACT,OAAOJ,MAAM,IAAI,CAAC,KAAK,IAAI,CAACf,mBAAmBmB;YACnD;YAEJ,IAAIC;YACJ,IAAI;gBACAA,OAAOC,kBAAkBL,QAAQ,IAAI;YACzC,EAAE,OAAOG,IAAI;gBACTG,QAAQ,KAAK,CAAC;gBACdA,QAAQ,KAAK,CAACtB,mBAAmBmB;gBACjC,MAAMA;YACV;YACA,IAAI;gBACA,MAAMI,SAAS,MAAMC,mBAAmBJ,MAAMf,QAAQV;gBAItD,IAAIoB,MAAM,IAAI,EAAE;oBACZO,QAAQ,IAAI,CAAC;oBACb,OAAOP;gBACX;gBACA,OAAOA,MAAM,MAAM,CAAC,KAAK,IAAI,CAACQ;YAClC,EAAE,OAAOJ,IAAI;gBACTG,QAAQ,KAAK,CAAC;gBACdA,QAAQ,KAAK,CAACtB,mBAAmBmB;gBACjC,MAAMA;YACV;QACJ;IACJ;IAEAT,MAAM,IAAI,GAAG;IAEb,OAAO;WACCH,QAAQkB,iBAAiB,EAAE;QAC/B;YACI,MAAM;QACV;QACAf;KACH;AACL"}
|
package/createGraphQLSchema.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
import { Context } from "@webiny/api/types";
|
|
2
|
-
|
|
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>;
|
package/createGraphQLSchema.js
CHANGED
|
@@ -1,68 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const createGraphQLSchema = context => {
|
|
19
|
-
const scalars = context.plugins.byType("graphql-scalar").map(item => item.scalar);
|
|
20
|
-
const typeDefs = [(0, _graphqlTag.default)`
|
|
1
|
+
import graphql_tag from "graphql-tag";
|
|
2
|
+
import { makeExecutableSchema } from "@graphql-tools/schema";
|
|
3
|
+
import { mergeResolvers } from "@graphql-tools/merge";
|
|
4
|
+
import { AnyScalar, DateScalar, DateTimeScalar, IconScalar, JsonScalar, LongScalar, NumberScalar, RefInputScalar, TimeScalar } from "./builtInTypes/index.js";
|
|
5
|
+
import { ResolverDecoration } from "./ResolverDecoration.js";
|
|
6
|
+
import { GraphQLSchemaComposer } from "./features/GraphQLSchemaBuilder/abstractions.js";
|
|
7
|
+
import { GraphQLSchemaComposerFeature } from "./features/GraphQLSchemaBuilder/feature.js";
|
|
8
|
+
const getSchemaPlugins = (context)=>context.plugins.byType("graphql-schema").filter((pl)=>{
|
|
9
|
+
if ("function" == typeof pl.isApplicable) return pl.isApplicable(context);
|
|
10
|
+
return true;
|
|
11
|
+
});
|
|
12
|
+
const createGraphQLSchema = async (context)=>{
|
|
13
|
+
GraphQLSchemaComposerFeature.register(context.container);
|
|
14
|
+
const scalars = context.plugins.byType("graphql-scalar").map((item)=>item.scalar);
|
|
15
|
+
const typeDefs = [
|
|
16
|
+
graphql_tag`
|
|
21
17
|
type Query
|
|
22
18
|
type Mutation
|
|
23
|
-
${scalars.map(scalar
|
|
19
|
+
${scalars.map((scalar)=>`scalar ${scalar.name}`).join(" ")}
|
|
24
20
|
scalar JSON
|
|
25
21
|
scalar Long
|
|
22
|
+
scalar Icon
|
|
26
23
|
scalar RefInput
|
|
27
24
|
scalar Number
|
|
28
25
|
scalar Any
|
|
29
26
|
scalar Date
|
|
30
27
|
scalar DateTime
|
|
31
28
|
scalar Time
|
|
32
|
-
`];
|
|
33
|
-
const resolvers = [(0, _objectSpread2.default)((0, _objectSpread2.default)({}, scalars.reduce((acc, s) => {
|
|
34
|
-
acc[s.name] = s;
|
|
35
|
-
return acc;
|
|
36
|
-
}, {})), {}, {
|
|
37
|
-
JSON: _builtInTypes.JsonScalar,
|
|
38
|
-
Long: _builtInTypes.LongScalar,
|
|
39
|
-
RefInput: _builtInTypes.RefInput,
|
|
40
|
-
Number: _builtInTypes.NumberScalar,
|
|
41
|
-
Any: _builtInTypes.AnyScalar,
|
|
42
|
-
DateTime: _builtInTypes.DateTimeScalar,
|
|
43
|
-
Date: _builtInTypes.DateScalar,
|
|
44
|
-
Time: _builtInTypes.TimeScalar
|
|
45
|
-
})];
|
|
46
|
-
const gqlPlugins = context.plugins.byType("graphql-schema");
|
|
47
|
-
|
|
48
|
-
for (let i = 0; i < gqlPlugins.length; i++) {
|
|
49
|
-
const plugin = gqlPlugins[i];
|
|
50
|
-
/**
|
|
51
|
-
* TODO @ts-refactor
|
|
52
|
-
* Figure out correct types on typeDefs and resolvers
|
|
53
|
-
*/
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
|
|
56
|
-
typeDefs.push(plugin.schema.typeDefs); // @ts-ignore
|
|
57
29
|
|
|
58
|
-
|
|
59
|
-
|
|
30
|
+
type Error {
|
|
31
|
+
code: String
|
|
32
|
+
message: String
|
|
33
|
+
data: JSON
|
|
34
|
+
stack: String
|
|
35
|
+
}
|
|
60
36
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
37
|
+
type BooleanResponse {
|
|
38
|
+
data: Boolean
|
|
39
|
+
error: Error
|
|
40
|
+
}
|
|
41
|
+
`
|
|
42
|
+
];
|
|
43
|
+
const resolvers = [
|
|
44
|
+
{
|
|
45
|
+
...scalars.reduce((acc, s)=>{
|
|
46
|
+
acc[s.name] = s;
|
|
47
|
+
return acc;
|
|
48
|
+
}, {}),
|
|
49
|
+
JSON: JsonScalar,
|
|
50
|
+
Long: LongScalar,
|
|
51
|
+
RefInput: RefInputScalar,
|
|
52
|
+
Number: NumberScalar,
|
|
53
|
+
Any: AnyScalar,
|
|
54
|
+
DateTime: DateTimeScalar,
|
|
55
|
+
Date: DateScalar,
|
|
56
|
+
Time: TimeScalar,
|
|
57
|
+
Icon: IconScalar
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
const resolverDecoration = new ResolverDecoration();
|
|
61
|
+
const plugins = getSchemaPlugins(context);
|
|
62
|
+
for (const plugin of plugins){
|
|
63
|
+
const schema = plugin.schema;
|
|
64
|
+
if (schema.typeDefs) typeDefs.push(schema.typeDefs);
|
|
65
|
+
if (schema.resolvers) resolvers.push(schema.resolvers);
|
|
66
|
+
if (schema.resolverDecorators) resolverDecoration.addDecorators(schema.resolverDecorators);
|
|
67
|
+
}
|
|
68
|
+
const graphQLSchemaComposer = context.container.resolve(GraphQLSchemaComposer);
|
|
69
|
+
const schema = await graphQLSchemaComposer.build();
|
|
70
|
+
if (schema.typeDefs) typeDefs.push(schema.typeDefs);
|
|
71
|
+
if (schema.resolvers) resolvers.push(schema.resolvers);
|
|
72
|
+
if (schema.resolverDecorators) resolverDecoration.addDecorators(schema.resolverDecorators);
|
|
73
|
+
return makeExecutableSchema({
|
|
74
|
+
typeDefs,
|
|
75
|
+
resolvers: resolverDecoration.decorateResolvers(mergeResolvers(resolvers)),
|
|
76
|
+
inheritResolversFromInterfaces: true
|
|
77
|
+
});
|
|
66
78
|
};
|
|
79
|
+
export { createGraphQLSchema, getSchemaPlugins };
|
|
67
80
|
|
|
68
|
-
|
|
81
|
+
//# sourceMappingURL=createGraphQLSchema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"createGraphQLSchema.js","sources":["../src/createGraphQLSchema.ts"],"sourcesContent":["import gql from \"graphql-tag\";\nimport { makeExecutableSchema } from \"@graphql-tools/schema\";\nimport { mergeResolvers } from \"@graphql-tools/merge\";\nimport type { GraphQLScalarType } from \"graphql/type/definition.js\";\nimport type { GraphQLScalarPlugin, Resolvers, TypeDefs } from \"./types.js\";\nimport type { Context } from \"@webiny/api/types.js\";\nimport {\n RefInputScalar,\n NumberScalar,\n AnyScalar,\n DateScalar,\n DateTimeScalar,\n JsonScalar,\n TimeScalar,\n LongScalar,\n IconScalar\n} from \"./builtInTypes/index.js\";\nimport { ResolverDecoration } from \"./ResolverDecoration.js\";\nimport type { GraphQLSchemaPlugin } from \"~/plugins/index.js\";\nimport { GraphQLSchemaComposer } from \"~/features/GraphQLSchemaBuilder/abstractions.js\";\nimport { GraphQLSchemaComposerFeature } from \"~/features/GraphQLSchemaBuilder/feature.js\";\n\nexport const getSchemaPlugins = (context: Context) => {\n return context.plugins.byType<GraphQLSchemaPlugin>(\"graphql-schema\").filter(pl => {\n if (typeof pl.isApplicable === \"function\") {\n return pl.isApplicable(context);\n }\n return true;\n });\n};\n\nexport const createGraphQLSchema = async (context: Context) => {\n GraphQLSchemaComposerFeature.register(context.container);\n\n const scalars = context.plugins\n .byType<GraphQLScalarPlugin>(\"graphql-scalar\")\n .map(item => item.scalar);\n\n const typeDefs: TypeDefs[] = [\n gql`\n type Query\n type Mutation\n ${scalars.map(scalar => `scalar ${scalar.name}`).join(\" \")}\n scalar JSON\n scalar Long\n scalar Icon\n scalar RefInput\n scalar Number\n scalar Any\n scalar Date\n scalar DateTime\n scalar Time\n\n type Error {\n code: String\n message: String\n data: JSON\n stack: String\n }\n\n type BooleanResponse {\n data: Boolean\n error: Error\n }\n `\n ];\n\n const resolvers: Resolvers<any>[] = [\n {\n ...scalars.reduce<Record<string, GraphQLScalarType>>((acc, s) => {\n acc[s.name] = s;\n return acc;\n }, {}),\n JSON: JsonScalar,\n Long: LongScalar,\n RefInput: RefInputScalar,\n Number: NumberScalar,\n Any: AnyScalar,\n DateTime: DateTimeScalar,\n Date: DateScalar,\n Time: TimeScalar,\n Icon: IconScalar\n }\n ];\n\n const resolverDecoration = new ResolverDecoration();\n\n // Process legacy plugins\n const plugins = getSchemaPlugins(context);\n\n for (const plugin of plugins) {\n const schema = plugin.schema;\n if (schema.typeDefs) {\n typeDefs.push(schema.typeDefs);\n }\n if (schema.resolvers) {\n resolvers.push(schema.resolvers);\n }\n if (schema.resolverDecorators) {\n resolverDecoration.addDecorators(schema.resolverDecorators);\n }\n }\n\n // Process new DI implementations\n const graphQLSchemaComposer = context.container.resolve(GraphQLSchemaComposer);\n const schema = await graphQLSchemaComposer.build();\n\n if (schema.typeDefs) {\n typeDefs.push(schema.typeDefs);\n }\n if (schema.resolvers) {\n resolvers.push(schema.resolvers);\n }\n if (schema.resolverDecorators) {\n resolverDecoration.addDecorators(schema.resolverDecorators);\n }\n\n // Create executable schema\n return makeExecutableSchema({\n typeDefs,\n resolvers: resolverDecoration.decorateResolvers(mergeResolvers(resolvers)),\n inheritResolversFromInterfaces: true\n });\n};\n"],"names":["getSchemaPlugins","context","pl","createGraphQLSchema","GraphQLSchemaComposerFeature","scalars","item","typeDefs","gql","scalar","resolvers","acc","s","JsonScalar","LongScalar","RefInputScalar","NumberScalar","AnyScalar","DateTimeScalar","DateScalar","TimeScalar","IconScalar","resolverDecoration","ResolverDecoration","plugins","plugin","schema","graphQLSchemaComposer","GraphQLSchemaComposer","makeExecutableSchema","mergeResolvers"],"mappings":";;;;;;;AAsBO,MAAMA,mBAAmB,CAACC,UACtBA,QAAQ,OAAO,CAAC,MAAM,CAAsB,kBAAkB,MAAM,CAACC,CAAAA;QACxE,IAAI,AAA2B,cAA3B,OAAOA,GAAG,YAAY,EACtB,OAAOA,GAAG,YAAY,CAACD;QAE3B,OAAO;IACX;AAGG,MAAME,sBAAsB,OAAOF;IACtCG,6BAA6B,QAAQ,CAACH,QAAQ,SAAS;IAEvD,MAAMI,UAAUJ,QAAQ,OAAO,CAC1B,MAAM,CAAsB,kBAC5B,GAAG,CAACK,CAAAA,OAAQA,KAAK,MAAM;IAE5B,MAAMC,WAAuB;QACzBC,WAAG,CAAC;;;YAGA,EAAEH,QAAQ,GAAG,CAACI,CAAAA,SAAU,CAAC,OAAO,EAAEA,OAAO,IAAI,EAAE,EAAE,IAAI,CAAC,KAAK;;;;;;;;;;;;;;;;;;;;;;QAsB/D,CAAC;KACJ;IAED,MAAMC,YAA8B;QAChC;YACI,GAAGL,QAAQ,MAAM,CAAoC,CAACM,KAAKC;gBACvDD,GAAG,CAACC,EAAE,IAAI,CAAC,GAAGA;gBACd,OAAOD;YACX,GAAG,CAAC,EAAE;YACN,MAAME;YACN,MAAMC;YACN,UAAUC;YACV,QAAQC;YACR,KAAKC;YACL,UAAUC;YACV,MAAMC;YACN,MAAMC;YACN,MAAMC;QACV;KACH;IAED,MAAMC,qBAAqB,IAAIC;IAG/B,MAAMC,UAAUxB,iBAAiBC;IAEjC,KAAK,MAAMwB,UAAUD,QAAS;QAC1B,MAAME,SAASD,OAAO,MAAM;QAC5B,IAAIC,OAAO,QAAQ,EACfnB,SAAS,IAAI,CAACmB,OAAO,QAAQ;QAEjC,IAAIA,OAAO,SAAS,EAChBhB,UAAU,IAAI,CAACgB,OAAO,SAAS;QAEnC,IAAIA,OAAO,kBAAkB,EACzBJ,mBAAmB,aAAa,CAACI,OAAO,kBAAkB;IAElE;IAGA,MAAMC,wBAAwB1B,QAAQ,SAAS,CAAC,OAAO,CAAC2B;IACxD,MAAMF,SAAS,MAAMC,sBAAsB,KAAK;IAEhD,IAAID,OAAO,QAAQ,EACfnB,SAAS,IAAI,CAACmB,OAAO,QAAQ;IAEjC,IAAIA,OAAO,SAAS,EAChBhB,UAAU,IAAI,CAACgB,OAAO,SAAS;IAEnC,IAAIA,OAAO,kBAAkB,EACzBJ,mBAAmB,aAAa,CAACI,OAAO,kBAAkB;IAI9D,OAAOG,qBAAqB;QACxBtB;QACA,WAAWe,mBAAmB,iBAAiB,CAACQ,eAAepB;QAC/D,gCAAgC;IACpC;AACJ"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import zod from "zod";
|
|
2
|
+
import { WebinyError } from "@webiny/error";
|
|
3
|
+
import { createZodError } from "@webiny/utils/createZodError.js";
|
|
4
|
+
const requestBodySchema = zod.looseObject({
|
|
5
|
+
query: zod.string(),
|
|
6
|
+
variables: zod.record(zod.string(), zod.any()).nullish().optional(),
|
|
7
|
+
operationName: zod.string().nullish().optional().transform((value)=>value || void 0)
|
|
8
|
+
});
|
|
9
|
+
const schema = zod.union([
|
|
10
|
+
requestBodySchema,
|
|
11
|
+
zod.array(requestBodySchema)
|
|
12
|
+
]);
|
|
13
|
+
const createRequestBody = (input)=>{
|
|
14
|
+
const body = "string" == typeof input ? JSON.parse(input) : input;
|
|
15
|
+
const result = schema.safeParse(body);
|
|
16
|
+
if (!result.success) {
|
|
17
|
+
const error = createZodError(result.error);
|
|
18
|
+
throw new WebinyError({
|
|
19
|
+
message: "Invalid GraphQL request! Check your query and variables.",
|
|
20
|
+
code: "GRAPHQL_REQUEST_INVALID",
|
|
21
|
+
data: error.data
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return result.data;
|
|
25
|
+
};
|
|
26
|
+
export { createRequestBody };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=createRequestBody.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRequestBody.js","sources":["../src/createRequestBody.ts"],"sourcesContent":["import zod from \"zod\";\nimport { WebinyError } from \"@webiny/error\";\nimport { createZodError } from \"@webiny/utils/createZodError.js\";\nimport type { GraphQLRequestBody } from \"~/types.js\";\n\nconst requestBodySchema = zod.looseObject({\n query: zod.string(),\n variables: zod.record(zod.string(), zod.any()).nullish().optional(),\n operationName: zod\n .string()\n .nullish()\n .optional()\n .transform(value => {\n return value || undefined;\n })\n});\n\nconst schema = zod.union([requestBodySchema, zod.array(requestBodySchema)]);\n\nexport const createRequestBody = (input: unknown): GraphQLRequestBody | GraphQLRequestBody[] => {\n const body = typeof input === \"string\" ? JSON.parse(input) : input;\n\n const result = schema.safeParse(body);\n if (!result.success) {\n const error = createZodError(result.error);\n throw new WebinyError({\n message: \"Invalid GraphQL request! Check your query and variables.\",\n code: \"GRAPHQL_REQUEST_INVALID\",\n data: error.data\n });\n }\n return result.data;\n};\n"],"names":["requestBodySchema","zod","value","undefined","schema","createRequestBody","input","body","JSON","result","error","createZodError","WebinyError"],"mappings":";;;AAKA,MAAMA,oBAAoBC,IAAI,WAAW,CAAC;IACtC,OAAOA,IAAI,MAAM;IACjB,WAAWA,IAAI,MAAM,CAACA,IAAI,MAAM,IAAIA,IAAI,GAAG,IAAI,OAAO,GAAG,QAAQ;IACjE,eAAeA,IAAAA,MACJ,GACN,OAAO,GACP,QAAQ,GACR,SAAS,CAACC,CAAAA,QACAA,SAASC;AAE5B;AAEA,MAAMC,SAASH,IAAI,KAAK,CAAC;IAACD;IAAmBC,IAAI,KAAK,CAACD;CAAmB;AAEnE,MAAMK,oBAAoB,CAACC;IAC9B,MAAMC,OAAO,AAAiB,YAAjB,OAAOD,QAAqBE,KAAK,KAAK,CAACF,SAASA;IAE7D,MAAMG,SAASL,OAAO,SAAS,CAACG;IAChC,IAAI,CAACE,OAAO,OAAO,EAAE;QACjB,MAAMC,QAAQC,eAAeF,OAAO,KAAK;QACzC,MAAM,IAAIG,YAAY;YAClB,SAAS;YACT,MAAM;YACN,MAAMF,MAAM,IAAI;QACpB;IACJ;IACA,OAAOD,OAAO,IAAI;AACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createResolverDecorator.js","sources":["../src/createResolverDecorator.ts"],"sourcesContent":["import type { ResolverDecorator } from \"./types.js\";\n\nexport const createResolverDecorator = <TSource = any, TContext = any, TArgs = any>(\n decorator: ResolverDecorator<TSource, TContext, TArgs>\n) => {\n return decorator;\n};\n"],"names":["createResolverDecorator","decorator"],"mappings":"AAEO,MAAMA,0BAA0B,CACnCC,YAEOA"}
|
package/debugPlugins.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { GraphQLAfterQueryPlugin } from "./types";
|
|
2
|
-
import { Context } from "@webiny/api/types";
|
|
1
|
+
import type { GraphQLAfterQueryPlugin } from "./types.js";
|
|
2
|
+
import type { Context } from "@webiny/api/types.js";
|
|
3
3
|
import { ContextPlugin } from "@webiny/api";
|
|
4
4
|
interface Log {
|
|
5
5
|
method: string;
|
package/debugPlugins.js
CHANGED
|
@@ -1,45 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
apply({
|
|
31
|
-
result,
|
|
32
|
-
context
|
|
33
|
-
}) {
|
|
34
|
-
result["extensions"] = {
|
|
35
|
-
console: [...(context.debug.logs || [])]
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
if (context.debug.logs) {
|
|
39
|
-
context.debug.logs.length = 0;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
}];
|
|
44
|
-
|
|
45
|
-
exports.default = _default;
|
|
1
|
+
import { interceptConsole } from "./interceptConsole.js";
|
|
2
|
+
import { ContextPlugin } from "@webiny/api";
|
|
3
|
+
const debugPlugins = ()=>[
|
|
4
|
+
new ContextPlugin(async (context)=>{
|
|
5
|
+
if (!context.debug) context.debug = {};
|
|
6
|
+
if (!context.debug.logs) context.debug.logs = [];
|
|
7
|
+
interceptConsole((method, args)=>{
|
|
8
|
+
context.debug.logs.push({
|
|
9
|
+
method,
|
|
10
|
+
args
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
}),
|
|
14
|
+
{
|
|
15
|
+
type: "graphql-after-query",
|
|
16
|
+
apply ({ result, context }) {
|
|
17
|
+
result["extensions"] = {
|
|
18
|
+
console: [
|
|
19
|
+
...context.debug.logs || []
|
|
20
|
+
]
|
|
21
|
+
};
|
|
22
|
+
if (context.debug.logs) context.debug.logs.length = 0;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
];
|
|
26
|
+
export default debugPlugins;
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=debugPlugins.js.map
|
package/debugPlugins.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"debugPlugins.js","sources":["../src/debugPlugins.ts"],"sourcesContent":["import { interceptConsole } from \"./interceptConsole.js\";\nimport type { GraphQLAfterQueryPlugin } from \"./types.js\";\nimport type { Context } from \"@webiny/api/types.js\";\nimport { ContextPlugin } from \"@webiny/api\";\n\ninterface Log {\n method: string;\n args: any;\n}\ninterface DebugContext extends Context {\n debug: {\n logs?: Log[];\n };\n}\n\nexport default () => [\n new ContextPlugin<DebugContext>(async context => {\n if (!context.debug) {\n context.debug = {};\n }\n\n if (!context.debug.logs) {\n context.debug.logs = [];\n }\n\n interceptConsole((method, args) => {\n (context.debug.logs as Log[]).push({ method, args });\n });\n }),\n {\n type: \"graphql-after-query\",\n apply({ result, context }) {\n result[\"extensions\"] = { console: [...(context.debug.logs || [])] };\n if (context.debug.logs) {\n context.debug.logs.length = 0;\n }\n }\n } as GraphQLAfterQueryPlugin<DebugContext>\n];\n"],"names":["ContextPlugin","context","interceptConsole","method","args","result"],"mappings":";;AAeA,qBAAgB,IAAK;QACjB,IAAIA,cAA4B,OAAMC;YAClC,IAAI,CAACA,QAAQ,KAAK,EACdA,QAAQ,KAAK,GAAG,CAAC;YAGrB,IAAI,CAACA,QAAQ,KAAK,CAAC,IAAI,EACnBA,QAAQ,KAAK,CAAC,IAAI,GAAG,EAAE;YAG3BC,iBAAiB,CAACC,QAAQC;gBACrBH,QAAQ,KAAK,CAAC,IAAI,CAAW,IAAI,CAAC;oBAAEE;oBAAQC;gBAAK;YACtD;QACJ;QACA;YACI,MAAM;YACN,OAAM,EAAEC,MAAM,EAAEJ,OAAO,EAAE;gBACrBI,MAAM,CAAC,aAAa,GAAG;oBAAE,SAAS;2BAAKJ,QAAQ,KAAK,CAAC,IAAI,IAAI,EAAE;qBAAE;gBAAC;gBAClE,IAAIA,QAAQ,KAAK,CAAC,IAAI,EAClBA,QAAQ,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG;YAEpC;QACJ;KACJ"}
|
package/errors.js
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.NotFoundError = void 0;
|
|
9
|
-
|
|
10
|
-
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
11
|
-
|
|
12
|
-
class NotFoundError extends _error.default {
|
|
13
|
-
constructor(message = "Not found.") {
|
|
14
|
-
super(message, "NOT_FOUND");
|
|
15
|
-
}
|
|
16
|
-
|
|
1
|
+
import error from "@webiny/error";
|
|
2
|
+
class NotFoundError extends error {
|
|
3
|
+
constructor(message = "Not found."){
|
|
4
|
+
super(message, "NOT_FOUND");
|
|
5
|
+
}
|
|
17
6
|
}
|
|
7
|
+
export { NotFoundError };
|
|
18
8
|
|
|
19
|
-
|
|
9
|
+
//# sourceMappingURL=errors.js.map
|
package/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"errors.js","sources":["../src/errors.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\n\nexport class NotFoundError extends WebinyError {\n constructor(message = \"Not found.\") {\n super(message, \"NOT_FOUND\");\n }\n}\n"],"names":["NotFoundError","WebinyError","message"],"mappings":";AAEO,MAAMA,sBAAsBC;IAC/B,YAAYC,UAAU,YAAY,CAAE;QAChC,KAAK,CAACA,SAAS;IACnB;AACJ"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GraphQLSchemaBuilder as Abstraction } from "./abstractions.js";
|
|
2
|
+
import type { IGraphQLSchema } from "../../graphql/abstractions.public.js";
|
|
3
|
+
import type { ResolverDecorator } from "../../types.js";
|
|
4
|
+
export declare class GraphQLSchemaBuilder implements Abstraction.Interface {
|
|
5
|
+
private readonly typeDefsArray;
|
|
6
|
+
private readonly resolvers;
|
|
7
|
+
private readonly resolverDecorators;
|
|
8
|
+
addTypeDefs(typeDefs: string): this;
|
|
9
|
+
addResolver<TArgs = any>(config: Abstraction.Config<TArgs>): this;
|
|
10
|
+
/**
|
|
11
|
+
* @internal This method needs revisiting, to align with DI concepts.
|
|
12
|
+
*/
|
|
13
|
+
addResolverDecorator(path: string, decorator: ResolverDecorator): this;
|
|
14
|
+
build(): IGraphQLSchema;
|
|
15
|
+
private setResolverAtPath;
|
|
16
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
class GraphQLSchemaBuilder {
|
|
2
|
+
addTypeDefs(typeDefs) {
|
|
3
|
+
this.typeDefsArray.push(typeDefs);
|
|
4
|
+
return this;
|
|
5
|
+
}
|
|
6
|
+
addResolver(config) {
|
|
7
|
+
const { path, dependencies = [], resolver } = config;
|
|
8
|
+
const pathParts = path.split(".");
|
|
9
|
+
const graphqlResolver = (parent, args, context, info)=>{
|
|
10
|
+
const resolvedDeps = dependencies.map((dep)=>{
|
|
11
|
+
const [abstraction] = Array.isArray(dep) ? dep : [
|
|
12
|
+
dep
|
|
13
|
+
];
|
|
14
|
+
return context.container.resolve(abstraction);
|
|
15
|
+
});
|
|
16
|
+
const actualResolver = resolver(...resolvedDeps);
|
|
17
|
+
return actualResolver({
|
|
18
|
+
parent,
|
|
19
|
+
args,
|
|
20
|
+
context,
|
|
21
|
+
info
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
this.setResolverAtPath(pathParts, graphqlResolver);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
addResolverDecorator(path, decorator) {
|
|
28
|
+
if (!this.resolverDecorators[path]) this.resolverDecorators[path] = [];
|
|
29
|
+
this.resolverDecorators[path].push(decorator);
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
build() {
|
|
33
|
+
return {
|
|
34
|
+
typeDefs: this.typeDefsArray.join("\n"),
|
|
35
|
+
resolvers: this.resolvers,
|
|
36
|
+
resolverDecorators: this.resolverDecorators
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
setResolverAtPath(pathParts, resolver) {
|
|
40
|
+
let current = this.resolvers;
|
|
41
|
+
for(let i = 0; i < pathParts.length - 1; i++){
|
|
42
|
+
const part = pathParts[i];
|
|
43
|
+
if (!current[part]) current[part] = {};
|
|
44
|
+
current = current[part];
|
|
45
|
+
}
|
|
46
|
+
const finalKey = pathParts[pathParts.length - 1];
|
|
47
|
+
current[finalKey] = resolver;
|
|
48
|
+
}
|
|
49
|
+
constructor(){
|
|
50
|
+
this.typeDefsArray = [];
|
|
51
|
+
this.resolvers = {};
|
|
52
|
+
this.resolverDecorators = {};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export { GraphQLSchemaBuilder };
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=GraphQLSchemaBuilder.js.map
|