@@ -233,6 +233,17 @@ app.listen(3030);
233
233
234
234
### The ` selectGraphQLResultFromTable ` helper
235
235
236
+ ** IMPORTANT** : this helper is for populating data you return from your
237
+ \* resolver; you _ should not_ use ` selectGraphQLResultFromTable ` to retrieve data
238
+ for your resolver to process. Instead use ` context.pgClient ` directly.
239
+
240
+ ** IMPORTANT** : ` selectGraphQLResultFromTable ` should only be called once per
241
+ resolver; it doesn't make sense to call it multiple times, and attempting to
242
+ combine the results is liable to cause issues. If you feel the need to call it
243
+ multiple times, please read the IMPORTANT note above, and/or consider
244
+ implementing your requirement via multiple fields/resolvers rather than trying
245
+ to do it all in one.
246
+
236
247
Resolvers are passed 4 arguments: ` parent, args, context, resolveInfo ` . In the
237
248
` context.pgClient ` is an instance of a database client from the ` pg ` module
238
249
that's already in a transaction configured with the settings for this particular
@@ -250,12 +261,35 @@ your GraphQL field. It is responsible for hooking into the query look-ahead
250
261
features of Graphile Engine to inspect the incoming GraphQL query and pull down
251
262
the relevant data from the database (including nested relations). You are then
252
263
expected to return the result of this fetch via your resolver. You can use the
253
- ` queryBuilder ` object to customise the generated query, changing the order,
264
+ ` queryBuilder ` object to customize the generated query, changing the order,
254
265
adding ` where ` clauses, ` limit ` s, etc (see below). Note that if you are not
255
266
returning a record type directly (for example you're returning a mutation
256
267
payload, or a connection interface), you should use the ` @pgField ` directive as
257
268
shown below so that the Look Ahead feature continues to work.
258
269
270
+ #### Usage for non-tables
271
+
272
+ Despite the (unfortunate) name; ` selectGraphQLResultFromTable ` can be used with
273
+ any table-like source, including a table-defining sub-query, however it should
274
+ only be used where the type perfectly matches the expected return type of the
275
+ GraphQL field.
276
+
277
+ This non-table support is particularly useful when it comes to calling
278
+ functions; for example if you had a function ` match_user() ` that returns a
279
+ ` users ` record, you could define a ` makeExtendSchemaPlugin ` resolver that
280
+ queries it like this:
281
+
282
+ ``` js
283
+ // type Query { matchingUser(searchText: String!): User }
284
+ const matchingUserResolver = async (parent , args , context , resolveInfo ) => {
285
+ const [row ] = await resolveInfo .graphile .selectGraphQLResultFromTable (
286
+ sql .fragment ` (select * from match_user(${ sql .value (args .searchText )} ))` ,
287
+ () => {} // no-op
288
+ );
289
+ return row;
290
+ };
291
+ ```
292
+
259
293
#### QueryBuilder
260
294
261
295
` queryBuilder ` is an instance of ` QueryBuilder ` , a helper that uses an SQL AST
@@ -502,7 +536,7 @@ const MyRegisterUserMutationPlugin = makeExtendSchemaPlugin(build => {
502
536
Note that the ` @pgField ` directive here is necessary for PostGraphile to "look
503
537
ahead" and determine what to request from the database.
504
538
505
- #### Mutation Example with Node ID
539
+ ### Mutation Example with Node ID
506
540
507
541
In this example we'll use a GraphQL Global Object Identifier (aka Node ID) to
508
542
soft-delete an entry from our ` app_public.items ` table. We're also going to
0 commit comments