/** * Helpers for the schema model tests: reading fixtures through either reader, * a structural model comparison that names the first differing type, and * builders for expected models. */ import * as IntrospectionReader from "@effect/graphql-generator/internal/IntrospectionReader" import type * as Model from "@effect/graphql-generator/internal/SchemaModel" import * as SdlReader from "@effect/graphql-generator/internal/SdlReader" import { assert } from "@effect/vitest" import * as Result from "effect/Result" import { readFileSync } from "node:fs" import { formatDiagnostic, parseOrThrow, source } from "./ast.ts" export const fixture = (file: string): string => readFileSync(new URL(`../fixtures/${file}`, import.meta.url), "utf8") export const readSdl = (body: string, path = "schema.graphql"): Model.Schema => { const result = SdlReader.read(source(body, path), parseOrThrow(body, path)) if (Result.isFailure(result)) { throw new Error(`unexpected diagnostic: ${formatDiagnostic(result.failure)}`) } return result.success } export const readIntrospection = (body: string, path = "schema.json"): Model.Schema => { const result = IntrospectionReader.read(source(body, path)) if (Result.isFailure(result)) { throw new Error(`unexpected diagnostic: ${formatDiagnostic(result.failure)}`) } return result.success } /** Maps become plain objects (so entry order is ignored) and `undefined` members are dropped. */ const normalize = (value: unknown): unknown => { if (value instanceof Map) { const out: Record = {} for (const [key, child] of value) out[String(key)] = normalize(child) return out } if (Array.isArray(value)) return value.map(normalize) if (value !== null && typeof value === "object") { const out: Record = {} for (const [key, child] of Object.entries(value)) { if (child !== undefined) out[key] = normalize(child) } return out } return value } const sortedKeys = (a: ReadonlyMap, b: ReadonlyMap): ReadonlyArray => Array.from(new Set([...a.keys(), ...b.keys()])).sort() /** * Compares two models structurally. Types are compared one at a time in name * order, so a failure names the first differing type instead of dumping both * schemas. */ export const assertModelsEqual = (actual: Model.Schema, expected: Model.Schema): void => { assert.deepStrictEqual( normalize({ queryType: actual.queryType, mutationType: actual.mutationType, subscriptionType: actual.subscriptionType }), normalize({ queryType: expected.queryType, mutationType: expected.mutationType, subscriptionType: expected.subscriptionType }), "models differ in their root operation types" ) for (const name of sortedKeys(actual.types, expected.types)) { assert.deepStrictEqual( normalize(actual.types.get(name)), normalize(expected.types.get(name)), `models differ at type "${name}"` ) } } /** Compares one named type of `model` with `expected`. */ export const assertType = (model: Model.Schema, expected: Model.NamedType): void => { assert.deepStrictEqual( normalize(model.types.get(expected.name)), normalize(expected), `type "${expected.name}" differs` ) } // Expected-model builders. Optional members default to absent. export const named = (name: string): Model.NamedTypeRef => ({ _tag: "NamedTypeRef", name }) export const list = (ofType: Model.TypeRef): Model.ListTypeRef => ({ _tag: "ListTypeRef", ofType }) export const nonNull = (ofType: Model.NamedTypeRef | Model.ListTypeRef): Model.NonNullTypeRef => ({ _tag: "NonNullTypeRef", ofType }) export const scalarType = (name: string, options: { readonly description?: string } = {}): Model.ScalarType => ({ _tag: "ScalarType", name, description: options.description }) const builtInScalars: ReadonlyArray = ["String", "Int", "Float", "Boolean", "ID"].map(( name ) => scalarType(name)) export const objectType = ( name: string, fields: ReadonlyArray, options: { readonly description?: string; readonly interfaces?: ReadonlyArray } = {} ): Model.ObjectType => ({ _tag: "ObjectType", name, description: options.description, interfaces: options.interfaces ?? [], fields }) export const interfaceType = ( name: string, fields: ReadonlyArray, options: { readonly description?: string readonly interfaces?: ReadonlyArray readonly possibleTypes?: ReadonlyArray } = {} ): Model.InterfaceType => ({ _tag: "InterfaceType", name, description: options.description, interfaces: options.interfaces ?? [], fields, possibleTypes: options.possibleTypes ?? [] }) export const unionType = ( name: string, possibleTypes: ReadonlyArray, options: { readonly description?: string } = {} ): Model.UnionType => ({ _tag: "UnionType", name, description: options.description, possibleTypes }) export const enumType = ( name: string, values: ReadonlyArray, options: { readonly description?: string } = {} ): Model.EnumType => ({ _tag: "EnumType", name, description: options.description, values }) export const enumValue = ( name: string, options: { readonly description?: string; readonly deprecationReason?: string } = {} ): Model.EnumValueDefinition => ({ name, description: options.description, deprecationReason: options.deprecationReason }) export const inputObjectType = ( name: string, fields: ReadonlyArray, options: { readonly description?: string; readonly oneOf?: boolean } = {} ): Model.InputObjectType => ({ _tag: "InputObjectType", name, description: options.description, oneOf: options.oneOf ?? false, fields }) export const field = ( name: string, type: Model.TypeRef, options: { readonly description?: string readonly arguments?: ReadonlyArray readonly deprecationReason?: string } = {} ): Model.Field => ({ name, description: options.description, arguments: options.arguments ?? [], type, deprecationReason: options.deprecationReason }) export const inputValue = ( name: string, type: Model.TypeRef, options: { readonly description?: string readonly defaultValue?: Model.ConstValue readonly deprecationReason?: string } = {} ): Model.InputValue => ({ name, description: options.description, type, defaultValue: options.defaultValue, deprecationReason: options.deprecationReason }) /** A complete expected schema; the built-in scalars are added for you. */ export const schema = (options: { readonly queryType: string readonly mutationType?: string readonly subscriptionType?: string readonly types: ReadonlyArray }): Model.Schema => ({ queryType: options.queryType, mutationType: options.mutationType, subscriptionType: options.subscriptionType, types: new Map([...builtInScalars, ...options.types].map((type) => [type.name, type])) })