@reventlessdev/reventless-graphql-server 1.0.0-alpha.17 → 1.0.0-alpha.171

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 CHANGED
@@ -55,7 +55,7 @@ Register it as a ReScript dependency in `rescript.json`:
55
55
  }
56
56
  ```
57
57
 
58
- Requires ReScript `^12.3.0` (peer dependency).
58
+ Requires ReScript `^12.3.1` (peer dependency).
59
59
 
60
60
  ## Links
61
61
 
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-graphql-server",
3
- "version": "1.0.0-alpha.17",
3
+ "version": "1.0.0-alpha.171",
4
4
  "description": "Transport-neutral GraphQL server runtime for Reventless: schema-from-fragments, resolver registration, an HTTP + graphql-ws listener, and a subscription fan-out bridge with a pluggable PubSub backend",
5
5
  "license": "Apache-2.0",
6
6
  "dependencies": {
7
- "@reventlessdev/rescript-graphql-yoga": "1.0.0-alpha.23",
8
- "@reventlessdev/reventless-core": "3.0.0-alpha.170"
7
+ "@reventlessdev/rescript-graphql-yoga": "1.0.0-alpha.29",
8
+ "@reventlessdev/reventless-core": "3.0.0-alpha.323"
9
9
  },
10
10
  "devDependencies": {
11
- "rescript": "12.3.0"
11
+ "rescript": "12.3.1"
12
12
  },
13
13
  "peerDependencies": {
14
- "rescript": "12.3.0"
14
+ "rescript": "^12.3.1"
15
15
  },
16
16
  "publishConfig": {
17
17
  "registry": "https://registry.npmjs.org"
@@ -19,7 +19,7 @@
19
19
  "repository": {
20
20
  "type": "git",
21
21
  "url": "git+https://github.com/ReventlessDev/reventless-core.git",
22
- "directory": "reventless/reventless-graphql-server"
22
+ "directory": "reventless/graphql-server"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "rescript build",
@@ -33,7 +33,7 @@ type diagnostics = {
33
33
  resolverMutationCount: int,
34
34
  resolverQueryCount: int,
35
35
  mismatches: array<string>,
36
- fullSdl: option<string>,
36
+ fullSdl?: string,
37
37
  serverRunning: bool,
38
38
  }
39
39
 
@@ -42,6 +42,11 @@ type t = {
42
42
  registerQueries: (~sdlFields: array<string>, ~resolvers: dict<resolverFn>) => unit,
43
43
  registerSubscriptions: (~sdlFields: array<string>, ~resolvers: dict<resolverFn>) => unit,
44
44
  registerTypes: (~sdlTypes: array<string>) => unit,
45
+ /** Resolvers for fields of an object type rather than of a root type — the
46
+ cross-table fields `@resolves` / `@resolvesMany` add to a queryable. The
47
+ SDL for those fields comes from the plugin's schema fragment, so only the
48
+ resolver map is registered here. */
49
+ registerFieldResolvers: (~typeName: string, ~resolvers: dict<resolverFn>) => unit,
45
50
  getMutationResolver: string => option<resolverFn>,
46
51
  getQueryResolver: string => option<resolverFn>,
47
52
  // `~contextFactory` is the yoga context factory — when supplied, requests
@@ -76,6 +81,7 @@ let make = (~label: string="GraphQL"): t => {
76
81
  let mutationResolvers: ref<dict<resolverFn>> = ref(Dict.make())
77
82
  let queryResolvers: ref<dict<resolverFn>> = ref(Dict.make())
78
83
  let subscriptionResolvers: ref<dict<resolverFn>> = ref(Dict.make())
84
+ let fieldResolvers: ref<dict<dict<resolverFn>>> = ref(Dict.make())
79
85
  let mutationFields: ref<array<string>> = ref([])
80
86
  let queryFields: ref<array<string>> = ref([])
81
87
  let subscriptionFields: ref<array<string>> = ref([])
@@ -85,7 +91,16 @@ let make = (~label: string="GraphQL"): t => {
85
91
  let lastFullSdl: ref<option<string>> = ref(None)
86
92
 
87
93
  let registerMutations = (~sdlFields: array<string>, ~resolvers: dict<resolverFn>) => {
88
- mutationFields.contents = mutationFields.contents->Array.concat(sdlFields)
94
+ // A mutation field has two legitimate writers: the admin base fragment
95
+ // carries the Plugin aggregate's lifecycle fields, and the aggregate
96
+ // auto-flow registers the same fields when the admin is constructed again
97
+ // for a later deploy onto the same server. buildASTSchema rejects a schema
98
+ // that defines a field twice, so keep the first SDL definition and drop
99
+ // repeats by field name — mirroring the resolver dict, where a repeat
100
+ // registration overwrites by key (last resolver wins).
101
+ let existing = Set.fromArray(mutationFields.contents->Array.map(extractFieldName))
102
+ let fresh = sdlFields->Array.filter(f => !(existing->Set.has(extractFieldName(f))))
103
+ mutationFields.contents = mutationFields.contents->Array.concat(fresh)
89
104
  resolvers->Dict.toArray->Array.forEach(((k, v)) => mutationResolvers.contents->Dict.set(k, v))
90
105
  }
91
106
 
@@ -96,9 +111,20 @@ let make = (~label: string="GraphQL"): t => {
96
111
 
97
112
  let registerSubscriptions = (~sdlFields: array<string>, ~resolvers: dict<resolverFn>) => {
98
113
  subscriptionFields.contents = subscriptionFields.contents->Array.concat(sdlFields)
99
- resolvers->Dict.toArray->Array.forEach(((k, v)) =>
100
- subscriptionResolvers.contents->Dict.set(k, v)
101
- )
114
+ resolvers
115
+ ->Dict.toArray
116
+ ->Array.forEach(((k, v)) => subscriptionResolvers.contents->Dict.set(k, v))
117
+ }
118
+
119
+ let registerFieldResolvers = (~typeName: string, ~resolvers: dict<resolverFn>) => {
120
+ let forType = switch fieldResolvers.contents->Dict.get(typeName) {
121
+ | Some(d) => d
122
+ | None =>
123
+ let d = Dict.make()
124
+ fieldResolvers.contents->Dict.set(typeName, d)
125
+ d
126
+ }
127
+ resolvers->Dict.toArray->Array.forEach(((k, v)) => forType->Dict.set(k, v))
102
128
  }
103
129
 
104
130
  // Identical type blocks dedupe: shared-type registrations (e.g. the
@@ -120,9 +146,7 @@ let make = (~label: string="GraphQL"): t => {
120
146
 
121
147
  let buildSdl = () => {
122
148
  let typesSdl =
123
- typeDefinitions.contents->Array.length > 0
124
- ? typeDefinitions.contents->Array.join("\n\n")
125
- : ""
149
+ typeDefinitions.contents->Array.length > 0 ? typeDefinitions.contents->Array.join("\n\n") : ""
126
150
  let mutations =
127
151
  mutationFields.contents->Array.length > 0
128
152
  ? mutationFields.contents->Array.join("\n")
@@ -142,9 +166,7 @@ ${mutations}
142
166
  ? `\n\ntype Subscription {\n${subscriptionFields.contents->Array.join("\n")}\n}`
143
167
  : ""
144
168
  let base =
145
- typesSdl->String.length > 0
146
- ? typesSdl ++ "\n\n" ++ queriesMutationsSdl
147
- : queriesMutationsSdl
169
+ typesSdl->String.length > 0 ? typesSdl ++ "\n\n" ++ queriesMutationsSdl : queriesMutationsSdl
148
170
  base ++ subscriptionsSdl
149
171
  }
150
172
 
@@ -164,6 +186,9 @@ ${mutations}
164
186
  if subscriptionResolvers.contents->Dict.keysToArray->Array.length > 0 {
165
187
  resolvers->Dict.set("Subscription", subscriptionResolvers.contents)
166
188
  }
189
+ fieldResolvers.contents
190
+ ->Dict.toArray
191
+ ->Array.forEach(((typeName, byField)) => resolvers->Dict.set(typeName, byField))
167
192
  let sdl = buildSdl()
168
193
  lastFullSdl.contents = Some(sdl)
169
194
  let schema = YG.createSchema({"typeDefs": sdl, "resolvers": resolvers})
@@ -173,8 +198,14 @@ ${mutations}
173
198
  // with the original Error even when the client response is masked — is never
174
199
  // swallowed), while verbose debug/info stay gated on GRAPHQL_DEBUG.
175
200
  let yogaLogging: YG.yogaLogger = {
176
- debug: a => if debug { log.debug(~comp=label, YG.logArgToString(a)) },
177
- info: a => if debug { log.info(~comp=label, YG.logArgToString(a)) },
201
+ debug: a =>
202
+ if debug {
203
+ log.debug(~comp=label, YG.logArgToString(a))
204
+ },
205
+ info: a =>
206
+ if debug {
207
+ log.info(~comp=label, YG.logArgToString(a))
208
+ },
178
209
  warn: a => log.warn(~comp=label, YG.logArgToString(a)),
179
210
  error: a => log.error(~comp=label, YG.logArgToString(a)),
180
211
  }
@@ -202,7 +233,10 @@ ${mutations}
202
233
  if subscriptionResolvers.contents->Dict.keysToArray->Array.length > 0 {
203
234
  let wss = YG.newWebSocketServer({"server": server, "path": "/graphql"})
204
235
  YG.wsUseServer({"schema": schema}, wss)
205
- log.info(~comp=label, `graphql-ws subscriptions on ws://localhost:${port->Int.toString}/graphql`)
236
+ log.info(
237
+ ~comp=label,
238
+ `graphql-ws subscriptions on ws://localhost:${port->Int.toString}/graphql`,
239
+ )
206
240
  }
207
241
  activeServer.contents = Some(server)
208
242
  }
@@ -211,6 +245,7 @@ ${mutations}
211
245
  mutationResolvers.contents = Dict.make()
212
246
  queryResolvers.contents = Dict.make()
213
247
  subscriptionResolvers.contents = Dict.make()
248
+ fieldResolvers.contents = Dict.make()
214
249
  mutationFields.contents = []
215
250
  queryFields.contents = []
216
251
  subscriptionFields.contents = []
@@ -271,7 +306,7 @@ ${mutations}
271
306
  resolverMutationCount: mutResolverNames->Array.length,
272
307
  resolverQueryCount: queryResolverNames->Array.length,
273
308
  mismatches,
274
- fullSdl: lastFullSdl.contents,
309
+ fullSdl: ?lastFullSdl.contents,
275
310
  serverRunning: activeServer.contents->Option.isSome,
276
311
  }
277
312
  }
@@ -309,6 +344,7 @@ ${mutations}
309
344
  registerQueries,
310
345
  registerSubscriptions,
311
346
  registerTypes,
347
+ registerFieldResolvers,
312
348
  getMutationResolver,
313
349
  getQueryResolver,
314
350
  start,
@@ -30,6 +30,9 @@ function make(labelOpt) {
30
30
  let subscriptionResolvers = {
31
31
  contents: {}
32
32
  };
33
+ let fieldResolvers = {
34
+ contents: {}
35
+ };
33
36
  let mutationFields = {
34
37
  contents: []
35
38
  };
@@ -52,7 +55,9 @@ function make(labelOpt) {
52
55
  contents: undefined
53
56
  };
54
57
  let registerMutations = (sdlFields, resolvers) => {
55
- mutationFields.contents = mutationFields.contents.concat(sdlFields);
58
+ let existing = new Set(mutationFields.contents.map(extractFieldName));
59
+ let fresh = sdlFields.filter(f => !existing.has(extractFieldName(f)));
60
+ mutationFields.contents = mutationFields.contents.concat(fresh);
56
61
  Object.entries(resolvers).forEach(param => {
57
62
  mutationResolvers.contents[param[0]] = param[1];
58
63
  });
@@ -69,6 +74,20 @@ function make(labelOpt) {
69
74
  subscriptionResolvers.contents[param[0]] = param[1];
70
75
  });
71
76
  };
77
+ let registerFieldResolvers = (typeName, resolvers) => {
78
+ let d = fieldResolvers.contents[typeName];
79
+ let forType;
80
+ if (d !== undefined) {
81
+ forType = d;
82
+ } else {
83
+ let d$1 = {};
84
+ fieldResolvers.contents[typeName] = d$1;
85
+ forType = d$1;
86
+ }
87
+ Object.entries(resolvers).forEach(param => {
88
+ forType[param[0]] = param[1];
89
+ });
90
+ };
72
91
  let registerTypes = sdlTypes => {
73
92
  sdlTypes.forEach(t => {
74
93
  if (!typeDefinitions.contents.includes(t)) {
@@ -110,6 +129,9 @@ type Mutation {
110
129
  if (Object.keys(subscriptionResolvers.contents).length !== 0) {
111
130
  resolvers["Subscription"] = subscriptionResolvers.contents;
112
131
  }
132
+ Object.entries(fieldResolvers.contents).forEach(param => {
133
+ resolvers[param[0]] = param[1];
134
+ });
113
135
  let sdl = buildSdl();
114
136
  lastFullSdl.contents = sdl;
115
137
  let schema = GraphqlYoga$1.createSchema({
@@ -165,6 +187,7 @@ type Mutation {
165
187
  mutationResolvers.contents = {};
166
188
  queryResolvers.contents = {};
167
189
  subscriptionResolvers.contents = {};
190
+ fieldResolvers.contents = {};
168
191
  mutationFields.contents = [];
169
192
  queryFields.contents = [];
170
193
  subscriptionFields.contents = [];
@@ -256,6 +279,7 @@ type Mutation {
256
279
  registerQueries: registerQueries,
257
280
  registerSubscriptions: registerSubscriptions,
258
281
  registerTypes: registerTypes,
282
+ registerFieldResolvers: registerFieldResolvers,
259
283
  getMutationResolver: getMutationResolver,
260
284
  getQueryResolver: getQueryResolver,
261
285
  start: start,
@@ -33,8 +33,7 @@ let sourceBTopic = (returnTypeName: string) => `on${returnTypeName}_stateChanged
33
33
 
34
34
  let makeFieldResolver = (~pubSub: YG.pubSub, topic: string): YG.resolverFn => {
35
35
  Obj.magic({
36
- "subscribe": (_root: JSON.t, _args: JSON.t, _ctx: JSON.t) =>
37
- pubSub->YG.pubSubSubscribe(topic),
36
+ "subscribe": (_root: JSON.t, _args: JSON.t, _ctx: JSON.t) => pubSub->YG.pubSubSubscribe(topic),
38
37
  "resolve": (payload: JSON.t) => payload,
39
38
  })
40
39
  }