Skip to content

Commit 319576d

Browse files
committed
Fix #233: notes on selectGraphQLResultFromTable
1 parent 8141343 commit 319576d

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/pages/postgraphile/make-extend-schema-plugin.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ app.listen(3030);
233233

234234
### The `selectGraphQLResultFromTable` helper
235235

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+
236247
Resolvers are passed 4 arguments: `parent, args, context, resolveInfo`. In the
237248
`context.pgClient` is an instance of a database client from the `pg` module
238249
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
250261
features of Graphile Engine to inspect the incoming GraphQL query and pull down
251262
the relevant data from the database (including nested relations). You are then
252263
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,
254265
adding `where` clauses, `limit`s, etc (see below). Note that if you are not
255266
returning a record type directly (for example you're returning a mutation
256267
payload, or a connection interface), you should use the `@pgField` directive as
257268
shown below so that the Look Ahead feature continues to work.
258269

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+
259293
#### QueryBuilder
260294

261295
`queryBuilder` is an instance of `QueryBuilder`, a helper that uses an SQL AST
@@ -502,7 +536,7 @@ const MyRegisterUserMutationPlugin = makeExtendSchemaPlugin(build => {
502536
Note that the `@pgField` directive here is necessary for PostGraphile to "look
503537
ahead" and determine what to request from the database.
504538

505-
#### Mutation Example with Node ID
539+
### Mutation Example with Node ID
506540

507541
In this example we'll use a GraphQL Global Object Identifier (aka Node ID) to
508542
soft-delete an entry from our `app_public.items` table. We're also going to

0 commit comments

Comments
 (0)