Skip to content

Commit

Permalink
Explain how to call resolvers from resolvers
Browse files Browse the repository at this point in the history
via Benjie's responses megathread
graphile#153 (comment)
  • Loading branch information
Pat committed Jul 7, 2020
1 parent 62385ae commit bcce0e9
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions src/pages/postgraphile/usage-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ instead (see below). The below options are valid for
generation (you almost definitely don't want this!).
- `skipPlugins`: An array of [Graphile Engine](/graphile-build/plugins/)
schema plugins to skip.
- `readCache`: A file path string or an object. Reads cached values from local
cache file to improve startup time (you may want to do this in production).
- `readCache`: A file path string. Reads cached values from local cache file
to improve startup time (you may want to do this in production).
- `writeCache`: A file path string. Writes computed values to local cache file
so startup can be faster (do this during the build phase).
- `jwtSecret`: The secret for your JSON web tokens. This will be used to
Expand Down Expand Up @@ -246,6 +246,71 @@ look at the `postgraphile-core` and `graphile-build-pg` modules.
[graphql-js]: https://www.npmjs.com/package/graphql
[`pg-pool`]: https://www.npmjs.com/package/pg-pool

### Calling a resolver from a resolver

You can issue GraphQL requests from various contexts, including within a resolver. To do so you need the following:

* Access to the `graphql` function from the `graphql` module

* In a PostGraphile plugin, if you have access to the build object (which you usually will), you should get this from `build.graphql.graphql`
* Failing that, you can `import { graphql } from 'graphql'` or `const { graphql } = require('graphql')`, but this has caveats.

* A reference to the GraphQL schema object. You can get this from many sources:

* in a resolver, you should extract it from `info.schema`
* if you have access to the PostGraphile middleware, you can issue `await postgraphileMiddleware.getGqlSchema()`
* 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

* A GraphQL operation (aka query, but includes mutations, subscriptions) to execute; this can be a string or an AST

* The variables to feed to the operation (if necessary)

* A valid GraphQL context for PostGraphile

* inside a resolver, you can just pass the resolvers context straight through
* in other situations, have a look at `withPostGraphileContext` in the [schema only usage](https://www.graphile.org/postgraphile/usage-schema/)

Issuing a GraphQL operation from inside a resolver example:

```js
/*
* Assuming you have access to a `build` object, e.g. inside a
* `makeExtendSchemaPlugin`, you can extract the `graphql` function
* from the `graphql` library here like so:
*/
const { graphql: { graphql } } = build;
/*
* Failing the above: `import { graphql } from 'graphql';` but beware of
* duplicate `graphql` modules in your `node_modules` causing issues.
*/

function myResolver(parent, args, context, info) {
// Whatever GraphQL query you wish to issue:
const document = /* GraphQL */ `
query MyQuery($userId: Int!) {
userById(id: $userId) {
username
}
}
`;
// The name of the operation in your query document (optional)
const operationName = 'MyQuery';
// The variables for the query
const variables = { userId: args.userId };

const { data, errors } = await graphql(
info.schema,
document,
null,
context,
variables,
operationName
);

return data.userById.username;
}
```

# Server-side TypeScript support

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

0 comments on commit bcce0e9

Please sign in to comment.