UNPKG

1.79 kBTypeScriptView Raw
1import { PoolClient } from 'pg';
2import { ExecutionResult } from 'graphql';
3import { $$pgClient } from '../postgres/inventory/pgClientFromContext';
4import { mixed, WithPostGraphileContextOptions } from '../interfaces';
5interface PostGraphileContext {
6 [$$pgClient]: PoolClient;
7 [key: string]: PoolClient | mixed;
8}
9export declare type WithPostGraphileContextFn<TResult = ExecutionResult> = (options: WithPostGraphileContextOptions, callback: (context: PostGraphileContext) => Promise<TResult>) => Promise<TResult>;
10/**
11 * Creates a PostGraphile context object which should be passed into a GraphQL
12 * execution. This function will also connect a client from a Postgres pool and
13 * setup a transaction in that client.
14 *
15 * This function is intended to wrap a call to GraphQL-js execution like so:
16 *
17 * ```js
18 * const result = await withPostGraphileContext({
19 * pgPool,
20 * jwtToken,
21 * jwtSecret,
22 * pgDefaultRole,
23 * }, async context => {
24 * return await graphql(
25 * schema,
26 * query,
27 * null,
28 * { ...context },
29 * variables,
30 * operationName,
31 * );
32 * });
33 * ```
34 */
35declare const withPostGraphileContext: WithPostGraphileContextFn;
36export default withPostGraphileContext;
37interface RawExplainResult {
38 query: string;
39 result: any;
40}
41declare type ExplainResult = Omit<RawExplainResult, 'result'> & {
42 plan: string;
43};
44declare module 'pg' {
45 interface ClientBase {
46 _explainResults: Array<RawExplainResult> | null;
47 startExplain: () => void;
48 stopExplain: () => Promise<Array<ExplainResult>>;
49 }
50}
51/**
52 * Monkey-patches the `query` method of a pg Client to add debugging
53 * functionality. Use with care.
54 */
55export declare function debugPgClient(pgClient: PoolClient, allowExplain?: boolean): PoolClient;