You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
functionmyResolver(parent, args, context, info) {
288
+
// Whatever GraphQL query you wish to issue:
289
+
constdocument=/* 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
+
constoperationName='MyQuery';
298
+
// The variables for the query
299
+
constvariables= { userId:args.userId };
300
+
301
+
const { data, errors } =awaitgraphql(
302
+
info.schema,
303
+
document,
304
+
null,
305
+
context,
306
+
variables,
307
+
operationName
308
+
);
309
+
310
+
returndata.userById.username;
311
+
}
312
+
```
313
+
249
314
# Server-side TypeScript support
250
315
251
316
PostGraphile takes care of building and serving a GraphQL API for various
0 commit comments