A builder class that allows construction of postgraphile options using multiple dedicated extension methods.

Hierarchy

  • PostgraphileOptionsBuilder

Constructors

Properties

headers: Dict<string | number | string[]> = {}
hookFuncs: Set<HookPluginFactory> = ...
pluginHooks: Set<PostGraphilePlugin> = ...
transformers: ((options) => PostGraphileOptions<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>)[] = []

Type declaration

    • (options): PostGraphileOptions<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>
    • Parameters

      • options: PostGraphileOptions<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>

      Returns PostGraphileOptions<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>

Methods

  • Adds multiple graphile engine plugins to appendPlugins array of postgraphile options object. setAdditionalGraphQLContextFromRequest and addGraphileBuildOptions methods can be used to pass parameters to said plugins. Explicit condition parameter is used to make a decision if plugins should be used or not (use if true, omit if false). Example:

    new PostgraphileOptionsBuilder()
    .addConditionalPlugins(config.isDev || config.enablePopulatePlugins, PopulateMoviesPlugin, PopulateTvshowsPlugin)

    Parameters

    • condition: boolean
    • Rest ...plugins: Plugin[]

      Comma-separated list of Graphile Engine schema plugins to load after the default plugins.

    Returns PostgraphileOptionsBuilder

  • Adds properties to resulting graphileBuildOptions property of postgraphile options object. Useful for modifying default postgraphile plugin settings. Example:

    new PostgraphileOptionsBuilder()
    .addGraphileBuildOptions({ pgSkipInstallingWatchFixtures: true })

    Parameters

    • graphileBuildOptions: Partial<Options>

      Additional Options to pass through into the graphile schema building system (received via the second argument of a plugin).

    Returns PostgraphileOptionsBuilder

  • This function takes a PostGraphilePlugin object and attaches it as a Postgraphile Hook when build() is called.

    Parameters

    • hookPlugin: PostGraphilePlugin

      This is a PostGraphilePlugin object.

    Returns PostgraphileOptionsBuilder

    Example

      new PostgraphileOptionsBuilder()
    .addHookPluginFunction(subscriptionAuthorizationHook)
  • This builder option must be used if the Hook Plugin requires PostGraphileOptions in the logic. A function which returns a PostGraphilePlugin should be passed into this method. That function will use PostGraphileOptions created in the build() method and attach the plugin returned from the function as a Postgraphile Hook.

    Parameters

    • hookFunction: HookPluginFactory

      This is a function which takes an argument of type PostGraphileOptions<Request, Response> and returns a PostGraphilePlugin object.

    Returns PostgraphileOptionsBuilder

    Example

      new PostgraphileOptionsBuilder()
    .addHookPluginFactory(customHookFactory)
  • Adds multiple graphile engine plugins to appendPlugins array of postgraphile options object. setAdditionalGraphQLContextFromRequest and addGraphileBuildOptions methods can be used to pass parameters to said plugins. Example:

    new PostgraphileOptionsBuilder()
    .addPlugins(
    PgSimplifyInflectorPlugin,
    ScalarTypesPlugin,
    SmartTagsPlugin,
    ValidationDirectivesPlugin,
    )

    Parameters

    • Rest ...plugins: Plugin[]

      Comma-separated list of Graphile Engine schema plugins to load after the default plugins.

    Returns PostgraphileOptionsBuilder

  • Builds a postgraphile options object with previously used extension methods.

    Returns PostGraphileOptions<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>

  • Builds a hook plugin that adds custom headers to http responses.

    Returns void

  • Sets all necessary postgraphile properties to enable subscriptions. Example:

      new PostgraphileOptionsBuilder()
    .enableSubscriptions({
    plugin: SubscriptionsPlugin,
    websocketMiddlewares: middlewares,
    hookFactory: subscriptionAuthorizationHookFactory
    })

    Results in something like this:

       import PgPubsub from '@graphile/pg-pubsub';
    import { makePluginHook } from 'postgraphile';
    //.........
    {
    appendPlugins: [SubscriptionsPlugin], // Other plugins will be included if they were added using other methods
    pluginHook: makePluginHook([PgPubsub, subscriptionAuthorizationHook]),
    subscriptions: true,
    websocketMiddlewares, // Empty array if websocketMiddlewares are not specified
    subscriptionEventEmitterMaxListeners: 0, // unlimited
    }

    To make sure that Postgraphile is able to correctly establish WebSockets connection, additional changes should be done by using enhanceHttpServerWithSubscriptions.

    See more here: https://www.graphile.org/postgraphile/subscriptions/#advanced-setup

    To customize the maximum number of listeners use:

    import PgPubsub from '@graphile/pg-pubsub'; // must be imported to extend the PostGraphileOptions type with the pg-pubsub options.
    ...
    new PostgraphileOptionsBuilder()
    .setProperties({
    subscriptionEventEmitterMaxListeners: 42,
    })

    Parameters

    • __namedParameters: {
          hookFactory?: HookPluginFactory;
          plugin: Plugin;
          websocketMiddlewares?: Middleware<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>[];
      }
      • Optional hookFactory?: HookPluginFactory
      • plugin: Plugin
      • Optional websocketMiddlewares?: Middleware<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>[]

    Returns PostgraphileOptionsBuilder

  • Adds additionalGraphQLContextFromRequest property to postgraphile options object. Define only once. Multiple calls will not expand previous calls, but will override them.

    Example:

    new PostgraphileOptionsBuilder()
    .setAdditionalGraphQLContextFromRequest(async req => {
    const { subject } = await getManagementAuthenticationContext(req);
    return { subject, serviceId: config.serviceId };
    })

    Parameters

    • callback: ((req, res) => Promise<Record<string, any>>)

      Some Graphile Engine schema plugins may need additional information available on the context argument to the resolver - you can use this function to provide such information based on the incoming request - you can even use this to change the response , e.g. setting cookies.

        • (req, res): Promise<Record<string, any>>
        • Parameters

          • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
          • res: Response<any, Record<string, any>>

          Returns Promise<Record<string, any>>

    Returns PostgraphileOptionsBuilder

  • Sets properties of postgraphile options object to be used based on some condition (use if true, omit if false). Additional calls will override and add properties. Example:

      new PostgraphileOptionsBuilder()
    .setConditionalProperties(config.isDev, {
    exportGqlSchemaPath: './src/generated/schema.graphql',
    watchPg: true,
    graphiql: true,
    enhanceGraphiql: true,
    allowExplain: true,
    })

    Parameters

    • condition: boolean
    • properties: PostGraphileOptions<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>

      An object containing postgraphile options.

    Returns PostgraphileOptionsBuilder

  • Sets default values for properties that we see as best practices which most services should start from. This includes the default GraphQL errors handler, suggested default properties, development properties in dev-environments, and excludes the 'NodePlugin'.

    Parameters

    • isDev: boolean
    • graphqlGuiEnabled: boolean

    Returns PostgraphileOptionsBuilder

  • Sets handleErrors property of postgraphile options object. Example:

      new PostgraphileOptionsBuilder()
    .setErrorsHandler((errors, req) => {
    return enhanceGraphqlErrors(
    result.errors,
    req.body?.operationName,
    customizeGraphQlErrorFields(defaultPgErrorMapper),
    logGraphQlError(defaultWriteLogMapper, req.authContext?.subject, this.logger),
    );
    }
    })

    Parameters

    • handleErrors: ((errors, req, res) => GraphQLErrorExtended[])

      Enables ability to modify errors before sending them down to the client. Optionally can send down custom responses. If you use this then showErrorStack and extendedError may have no effect.

        • (errors, req, res): GraphQLErrorExtended[]
        • Parameters

          • errors: readonly GraphQLError[]
          • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
          • res: Response<any, Record<string, any>>

          Returns GraphQLErrorExtended[]

    Returns PostgraphileOptionsBuilder

  • Enables or disables graphiql GUI depending on a passed condition parameter. Enable if true, disable if false. Sets 3 postgraphile properties: graphiql, enhanceGraphiql and allowExplain. Example:

      new PostgraphileOptionsBuilder()
    .setGraphiql(config.graphqlGuiEnabled)

    Parameters

    • condition: boolean

    Returns PostgraphileOptionsBuilder

  • Sets pgSettings property of postgraphile options object. Example:

      new PostgraphileOptionsBuilder()
    .setPgSettings(async req => {
    const { subject } = await getManagementAuthenticationContext(req, idServiceParams);
    return buildPgSettings(subject, config.dbGqlRole, config.serviceId);
    })

    Parameters

    • pgSettings: ((req) => Dict<mixed> | Promise<Dict<mixed>>)

      A plain object specifying custom config values to set in the PostgreSQL transaction (accessed via current_setting('my.custom.setting')) or an (optionally asynchronous) function which will return the same (or a Promise to the same) based on the incoming web request (e.g. to extract session data).

        • (req): Dict<mixed> | Promise<Dict<mixed>>
        • Parameters

          • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

          Returns Dict<mixed> | Promise<Dict<mixed>>

    Returns PostgraphileOptionsBuilder

  • Sets properties of postgraphile options object to be used in without any conditions. Additional calls will override and add properties. Example:

      new PostgraphileOptionsBuilder()
    .setProperties({
    enableCors: true,
    dynamicJson: true,
    ignoreRBAC: false,
    legacyRelations: 'omit',
    setofFunctionsContainNulls: false,
    })

    Parameters

    • properties: PostGraphileOptions<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>

      An object containing postgraphile options.

    Returns PostgraphileOptionsBuilder

  • Skips multiple default postgraphile plugins by adding them to skipPlugins array of postgraphile options object . Example:

    new PostgraphileOptionsBuilder()
    .skipPlugins(NodePlugin, MutationPayloadQueryPlugin)

    Parameters

    • Rest ...plugins: Plugin[]

      Comma-separated list of Graphile Engine schema plugins to skip.

    Returns PostgraphileOptionsBuilder

Generated using TypeDoc