Skip to content

Commit bcce0e9

Browse files
author
Pat
committed
Explain how to call resolvers from resolvers
via Benjie's responses megathread graphile#153 (comment)
1 parent 62385ae commit bcce0e9

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

src/pages/postgraphile/usage-schema.md

+67-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ instead (see below). The below options are valid for
156156
generation (you almost definitely don't want this!).
157157
- `skipPlugins`: An array of [Graphile Engine](/graphile-build/plugins/)
158158
schema plugins to skip.
159-
- `readCache`: A file path string or an object. Reads cached values from local
160-
cache file to improve startup time (you may want to do this in production).
159+
- `readCache`: A file path string. Reads cached values from local cache file
160+
to improve startup time (you may want to do this in production).
161161
- `writeCache`: A file path string. Writes computed values to local cache file
162162
so startup can be faster (do this during the build phase).
163163
- `jwtSecret`: The secret for your JSON web tokens. This will be used to
@@ -246,6 +246,71 @@ look at the `postgraphile-core` and `graphile-build-pg` modules.
246246
[graphql-js]: https://www.npmjs.com/package/graphql
247247
[`pg-pool`]: https://www.npmjs.com/package/pg-pool
248248

249+
### Calling a resolver from a resolver
250+
251+
You can issue GraphQL requests from various contexts, including within a resolver. To do so you need the following:
252+
253+
* Access to the `graphql` function from the `graphql` module
254+
255+
* In a PostGraphile plugin, if you have access to the build object (which you usually will), you should get this from `build.graphql.graphql`
256+
* Failing that, you can `import { graphql } from 'graphql'` or `const { graphql } = require('graphql')`, but this has caveats.
257+
258+
* A reference to the GraphQL schema object. You can get this from many sources:
259+
260+
* in a resolver, you should extract it from `info.schema`
261+
* if you have access to the PostGraphile middleware, you can issue `await postgraphileMiddleware.getGqlSchema()`
262+
* if you don't need the PostGraphile middleware, you can use `await createPostGraphileSchema(...)` - see [schema only usage](https://www.graphile.org/postgraphile/usage-schema/) - do this once and cache it because it's expensive to compute
263+
264+
* A GraphQL operation (aka query, but includes mutations, subscriptions) to execute; this can be a string or an AST
265+
266+
* The variables to feed to the operation (if necessary)
267+
268+
* A valid GraphQL context for PostGraphile
269+
270+
* inside a resolver, you can just pass the resolvers context straight through
271+
* in other situations, have a look at `withPostGraphileContext` in the [schema only usage](https://www.graphile.org/postgraphile/usage-schema/)
272+
273+
Issuing a GraphQL operation from inside a resolver example:
274+
275+
```js
276+
/*
277+
* Assuming you have access to a `build` object, e.g. inside a
278+
* `makeExtendSchemaPlugin`, you can extract the `graphql` function
279+
* from the `graphql` library here like so:
280+
*/
281+
const { graphql: { graphql } } = build;
282+
/*
283+
* Failing the above: `import { graphql } from 'graphql';` but beware of
284+
* duplicate `graphql` modules in your `node_modules` causing issues.
285+
*/
286+
287+
function myResolver(parent, args, context, info) {
288+
// Whatever GraphQL query you wish to issue:
289+
const document = /* GraphQL */ `
290+
query MyQuery($userId: Int!) {
291+
userById(id: $userId) {
292+
username
293+
}
294+
}
295+
`;
296+
// The name of the operation in your query document (optional)
297+
const operationName = 'MyQuery';
298+
// The variables for the query
299+
const variables = { userId: args.userId };
300+
301+
const { data, errors } = await graphql(
302+
info.schema,
303+
document,
304+
null,
305+
context,
306+
variables,
307+
operationName
308+
);
309+
310+
return data.userById.username;
311+
}
312+
```
313+
249314
# Server-side TypeScript support
250315

251316
PostGraphile takes care of building and serving a GraphQL API for various

0 commit comments

Comments
 (0)