import { isThenable, type MaybePromise } from '@pothos/core'; import { execute } from '@pothos/test-utils'; import { buildSchema, type FieldNode, type GraphQLField, type GraphQLNamedType, type GraphQLObjectType, type GraphQLResolveInfo, parse, } from 'graphql'; import { expect } from 'vitest'; import type { Adapter, Mappings, Node, QueryVisitor, SelectFn, WalkedType } from '../src'; import { NodeAdapter, Plan } from '../src'; import type { NodeBase } from '../src/adapter'; import type { Mapping } from '../src/loader-map'; import type { EntryOptions } from '../src/types'; /** The plan type the fake adapter builds. */ export type FakePlan = Plan; /** * What a plugin's own entry point does with a plan: plan the field, play it, and hand back the * caller's own selection when the paths matched nothing. The package exported this as * `queryFromInfo` until prisma took it over — drizzle and prisma-next each override the rule, so * it is prisma's — and the tests that drive a whole walk keep a copy of it here. */ export function queryFromInfo = Node>( adapter: Adapter, options: EntryOptions, ): Query { const plan = Plan.fromInfo(adapter, options) as | MaybePromise> | undefined; return ( plan ? isThenable(plan) ? plan.then((settled) => settled.query()) : plan.query() : (options.initial ?? {}) ) as Query; } /** A model: one object per name, so identity is model identity. */ export interface FakeModel { name: string; relations: Record; } /** * A prisma-like map: no `select` means every column, `extras` are computed values keyed by name, * anything else is an argument. */ export interface FakeMap { select?: Record; extras?: Record; [arg: string]: unknown; } const ALL: FakeMap = Object.freeze({}); export function createModels(...names: string[]) { const models: Record = {}; for (const name of names) { models[name] = { name, relations: {} }; } return models; } export type FakeVisitor = QueryVisitor; /** * How the fake map reads onto the shared query tree, and how a node is written back. The models * are the whole of the schema side, so a test that only needs the tree rules builds one with no * models at all. */ export class FakeAdapter extends NodeAdapter { constructor(private readonly models: Record = {}) { super(); } modelFor(type: GraphQLNamedType) { return this.models[type.extensions?.model as string]; } typeSelection(type: GraphQLNamedType) { return type.extensions?.model ? ((type.extensions.select as FakeMap | undefined) ?? ALL) : undefined; } fieldSelection(field: GraphQLField, _type: WalkedType) { return field.extensions?.select as FakeMap | SelectFn | undefined; } visitQuery({ select, extras, ...args }: FakeMap, model: FakeModel, visit: FakeVisitor) { // No `select` means every column, which is final. if (!select) { visit.allColumns(); } for (const key of Object.keys(select ?? {})) { const value = select![key]; if (!value) { continue; } const target = model.relations[key]; if (target) { visit.relation(key, target, value === true ? ALL : value); } else { visit.column(key); } } for (const key of Object.keys(extras ?? {})) { visit.computed(key, extras![key]); } visit.args(args); } toQuery(node: Node): FakeMap { const select: Record = {}; const query: FakeMap = { ...node.args }; for (const [name, child] of node.relations) { const nested = this.toQuery(child); select[name] = hasKeys(nested) ? nested : true; } if (node.computed.size > 0) { query.extras = Object.fromEntries(node.computed); } if (node.columns) { for (const column of node.columns) { select[column] = true; } return { ...query, select }; } return hasKeys(select) ? { ...query, select } : query; } } /** * A recorded mapping without the position the walker records with it, so a test can compare what * was mapped beneath a field on its own. Positions are asserted where they are the subject. */ export function mappingOf(mapping: Mapping | null | undefined) { return mapping ? { nested: mappingsOf(mapping.nested) } : mapping; } export function mappingsOf(mappings: Mappings) { const stripped: Record = {}; for (const key of Object.keys(mappings)) { stripped[key] = mappingOf(mappings[key]); } return stripped; } export interface TypeSetup { model?: string; select?: FakeMap; fields?: Record | false>; extensions?: Record; } /** Builds a schema from SDL and attaches the adapter's extensions to its types and fields. */ export function createSchema(sdl: string, setup: Record) { const schema = buildSchema(sdl); for (const [typeName, { model, select, fields, extensions }] of Object.entries(setup)) { const type = schema.getType(typeName) as GraphQLNamedType & { extensions: Record; getFields?: () => Record>; }; type.extensions = { ...type.extensions, ...extensions, model, select }; for (const [fieldName, selection] of Object.entries(fields ?? {})) { const field = type.getFields!()[fieldName] as GraphQLField & { extensions: Record; }; field.extensions = { ...field.extensions, select: selection }; } } return schema; } /** * Executes `source` and captures the resolve info of the first root field, or of `at` * (`[typeName, fieldName]`) beneath it: the same shape a plugin's field resolver receives. Every * object resolves to an empty object so the nested resolver runs. */ export async function resolveInfo( schema: ReturnType, source: string, { at, variableValues }: { at?: [string, string]; variableValues?: Record } = {}, ) { let captured: GraphQLResolveInfo | undefined; const rootValue: Record = {}; for (const field of Object.keys(schema.getQueryType()!.getFields())) { rootValue[field] = (_args: unknown, _ctx: unknown, info: GraphQLResolveInfo) => { if (!at) { captured ??= info; } return {}; }; } if (at) { const field = (schema.getType(at[0]) as GraphQLObjectType).getFields()[at[1]]; field.resolve = (_parent, _args, _ctx, info) => { captured ??= info; return null; }; } const result = await execute({ schema, document: parse(source), rootValue, variableValues }); if (!captured) { throw new Error(`No field resolved: ${JSON.stringify(result.errors)}`); } return captured; } export function fieldNodeOf(info: GraphQLResolveInfo): FieldNode { return info.fieldNodes[0]; } /** Whether an object has any own enumerable key: `toQuery` writes `true` for an empty selection. */ function hasKeys(value: object) { return Object.keys(value).length > 0; } /** An error clients see as `Unexpected error.`, whose `cause` has `detail`. */ export function unexpected(detail: string) { return expect.objectContaining({ message: 'Unexpected error.', cause: expect.objectContaining({ message: expect.stringContaining(detail) }), }); }