UNPKG

graphile-test

Version:
78 lines (77 loc) 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLTest = void 0; const graphile_build_1 = require("graphile-build"); const graphile_build_2 = require("graphile-build"); const graphile_build_pg_1 = require("graphile-build-pg"); const pg_1 = require("postgraphile/adaptors/pg"); const context_1 = require("./context"); /** * Minimal preset that provides core functionality without Node/Relay. * This matches the pattern from graphile-settings. */ const MinimalPreset = { extends: [graphile_build_2.defaultPreset, graphile_build_pg_1.defaultPreset], disablePlugins: ['NodePlugin'], }; /** * Creates a GraphQL test context using PostGraphile v5 preset-based API. * * @param input - Configuration including schemas and optional preset * @param conn - Database connection result from pgsql-test * @returns GraphQL test context with setup, teardown, and query functions */ const GraphQLTest = (input, conn) => { const { schemas, authRole, preset: userPreset } = input; let schema; let resolvedPreset; let pgService; const pgPool = conn.manager.getPool(conn.pg.config); const setup = async () => { // Create the pgService - this will be used for withPgClient pgService = (0, pg_1.makePgService)({ pool: pgPool, schemas, }); // Build the complete preset by extending the minimal preset // with user-provided preset configuration const completePreset = { extends: [ MinimalPreset, ...(userPreset?.extends ?? []), ], ...(userPreset?.disablePlugins && { disablePlugins: userPreset.disablePlugins }), ...(userPreset?.plugins && { plugins: userPreset.plugins }), ...(userPreset?.schema && { schema: userPreset.schema }), ...(userPreset?.grafast && { grafast: userPreset.grafast }), pgServices: [pgService], }; // Use makeSchema from graphile-build to create the schema const result = await (0, graphile_build_1.makeSchema)(completePreset); schema = result.schema; resolvedPreset = result.resolvedPreset; }; const teardown = async () => { // Optional cleanup - schema is garbage collected }; const query = async (opts) => { return await (0, context_1.runGraphQLInContext)({ input, schema, resolvedPreset, authRole: authRole ?? 'anonymous', pgPool, pgService, conn, query: opts.query, variables: opts.variables, reqOptions: opts.reqOptions, }); }; return { setup, teardown, query, }; }; exports.GraphQLTest = GraphQLTest;