melonykit 1.19.0 → 1.20.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.
@@ -33,23 +33,13 @@ export async function initRestEndpoints(srcFolder, filePathPatterns, app) {
33
33
 
34
34
  /** Registers a GraphQL route from schema modules and enables client events. */
35
35
  export async function initGraphqlEndpoints(srcFolder, filePathPatterns, app) {
36
- const { assertValidSchema, buildSchema, graphql } = await import('graphql');
37
- const typeDefs = ['type Query'];
38
- const rootValue = Object.create(null);
36
+ const graphqlRuntime = await import('graphql');
37
+ const { graphql } = graphqlRuntime;
38
+ const modules = [];
39
39
 
40
- await importModules(srcFolder, filePathPatterns, module => {
41
- typeDefs.push(module.typeDefs);
40
+ await importModules(srcFolder, filePathPatterns, module => modules.push(module));
42
41
 
43
- for (const [name, resolver] of Object.entries(module.resolvers)) {
44
- if (Object.hasOwn(rootValue, name))
45
- throw new Error(`Duplicate GraphQL resolver: ${name}`);
46
-
47
- rootValue[name] = resolver;
48
- }
49
- });
50
-
51
- const schema = buildSchema(typeDefs.join('\n'));
52
- assertValidSchema(schema);
42
+ const schema = _createGraphqlSchema(graphqlRuntime, modules);
53
43
 
54
44
  app.post('/api/graphql', _createEndpointHandler(async ({
55
45
  req, respondSuccess, invoke
@@ -67,7 +57,6 @@ export async function initGraphqlEndpoints(srcFolder, filePathPatterns, app) {
67
57
  source: query,
68
58
  variableValues: variables,
69
59
  operationName,
70
- rootValue,
71
60
  contextValue: { req, invoke }
72
61
  });
73
62
 
@@ -108,3 +97,70 @@ function _createEndpointHandler(expressHandler) {
108
97
  }
109
98
  };
110
99
  };
100
+
101
+ function _createGraphqlSchema(graphql, modules) {
102
+ const { assertValidSchema, buildSchema } = graphql;
103
+ const typeDefs = [];
104
+ const operations = {
105
+ Query: { fields: [], resolvers: new Map() },
106
+ Mutation: { fields: [], resolvers: new Map() }
107
+ };
108
+
109
+ for (const module of modules) {
110
+ const { queries, mutations } = module.default ?? {};
111
+ if (!queries && !mutations) {
112
+ throw new TypeError(
113
+ 'GraphQL modules must default-export queries or mutations.'
114
+ );
115
+ }
116
+
117
+ _addOperation('Query', queries);
118
+ _addOperation('Mutation', mutations);
119
+ }
120
+
121
+ for (const [name, { fields }] of Object.entries(operations)) {
122
+ if (fields.length) {
123
+ typeDefs.push(`type ${name} {\n${fields.join('\n')}\n}`);
124
+ }
125
+ }
126
+
127
+ const schema = buildSchema(typeDefs.join('\n'));
128
+ assertValidSchema(schema);
129
+
130
+ for (const [name, { resolvers }] of Object.entries(operations)) {
131
+ const fields = schema.getType(name)?.getFields();
132
+
133
+ for (const [fieldName, resolver] of resolvers) {
134
+ const field = fields?.[fieldName];
135
+ if (!field || typeof resolver !== 'function') {
136
+ throw new Error(
137
+ `Invalid GraphQL resolver: ${name}.${fieldName}`
138
+ );
139
+ }
140
+
141
+ field.resolve = (_source, args, context, info) => (
142
+ resolver(args, context, info)
143
+ );
144
+ }
145
+ }
146
+
147
+ return schema;
148
+
149
+ function _addOperation(name, operation) {
150
+ const { types, schemas, ...methods } = operation ?? {};
151
+ const { fields, resolvers } = operations[name];
152
+
153
+ if (types) typeDefs.push(types);
154
+ if (schemas?.trim()) fields.push(schemas);
155
+
156
+ for (const [fieldName, resolver] of Object.entries(methods)) {
157
+ if (resolvers.has(fieldName)) {
158
+ throw new Error(
159
+ `Duplicate GraphQL resolver: ${name}.${fieldName}`
160
+ );
161
+ }
162
+
163
+ resolvers.set(fieldName, resolver);
164
+ }
165
+ };
166
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Vadim Hermann",
3
3
  "name": "melonykit",
4
- "version": "1.19.0",
4
+ "version": "1.20.0",
5
5
  "type": "module",
6
6
  "description": "Preact based ui and utility functions toolkit",
7
7
  "main": "./dist/index.js",