import { type DecoratorContext, type Model, type ModelProperty, type Program, validateDecoratorUniqueOnNode, walkPropertiesInherited, } from "@typespec/compiler"; import { useStateMap, useStateSet } from "@typespec/compiler/utils"; import type { ComposeDecorator, GraphqlInterfaceDecorator, } from "../../generated-defs/TypeSpec.GraphQL.js"; import { GraphQLKeys, reportDiagnostic } from "../lib.js"; import { propertiesEqual } from "./utils.js"; declare const tags: unique symbol; type Tagged = BaseType & { [tags]: { [K in Tag]: void } }; /** An Interface is a model that has been marked as an Interface */ type Interface = Tagged; const [getInterface, setInterface] = useStateSet(GraphQLKeys.interface); const [getInterfaceOnly, setInterfaceOnly] = useStateSet(GraphQLKeys.interfaceOnly); const [getComposition, setComposition, _getCompositionMap] = useStateMap( GraphQLKeys.compose, ); export { /** * Get the implemented interfaces for a given model * @param program Program * @param model Model * @returns Composed interfaces or undefined if no interfaces are composed. */ getComposition, setComposition, }; /** * Check if the model is defined as a schema. * @param program Program * @param model Model * @returns Boolean */ export function isInterface(program: Program, model: Model | Interface): model is Interface { return !!getInterface(program, model as Interface); } export function isInterfaceOnly(program: Program, model: Model): boolean { return !!getInterfaceOnly(program, model as Interface); } function validateImplementedsAreInterfaces(context: DecoratorContext, interfaces: Model[]) { let valid = true; for (const iface of interfaces) { if (!isInterface(context.program, iface)) { valid = false; reportDiagnostic(context.program, { code: "invalid-interface", format: { interface: iface.name }, target: context.decoratorTarget, }); } } return valid; } function validateNoCircularImplementation( context: DecoratorContext, target: Model, interfaces: Interface[], ) { const valid = !isInterface(context.program, target) || !interfaces.includes(target); if (!valid) { reportDiagnostic(context.program, { code: "circular-interface", target: context.decoratorTarget, }); } return valid; } function validateImplementsInterfaceProperties( context: DecoratorContext, modelProperties: Map, iface: Interface, ) { let valid = true; for (const prop of walkPropertiesInherited(iface)) { if (!modelProperties.has(prop.name)) { valid = false; reportDiagnostic(context.program, { code: "missing-interface-property", format: { interface: iface.name, property: prop.name }, target: context.decoratorTarget, }); } else if (!propertiesEqual(modelProperties.get(prop.name)!, prop)) { valid = false; reportDiagnostic(context.program, { code: "incompatible-interface-property", format: { interface: iface.name, property: prop.name }, target: context.decoratorTarget, }); } } return valid; } function validateImplementsInterfacesProperties( context: DecoratorContext, target: Model, interfaces: Interface[], ) { let valid = true; const allModelProperties = new Map( [...walkPropertiesInherited(target)].map((prop) => [prop.name, prop]), ); for (const iface of interfaces) { if (!validateImplementsInterfaceProperties(context, allModelProperties, iface)) { valid = false; } } return valid; } export const $graphqlInterface: GraphqlInterfaceDecorator = (context, target, options) => { validateDecoratorUniqueOnNode(context, target, $graphqlInterface); setInterface(context.program, target as Interface); if (options?.interfaceOnly) { setInterfaceOnly(context.program, target as Interface); } }; export const $compose: ComposeDecorator = (context, target, ...interfaces) => { validateImplementedsAreInterfaces(context, interfaces as Interface[]); validateNoCircularImplementation(context, target, interfaces as Interface[]); validateImplementsInterfacesProperties(context, target, interfaces as Interface[]); const existingCompose = getComposition(context.program, target); let composed = interfaces as Interface[]; if (existingCompose) { composed = [...existingCompose, ...composed]; } setComposition(context.program, target, composed); };