import ConfigStore from '../config-store'; import { BaseTypeRef, FieldNullability, FieldRequiredness, GiraphQLInputFieldType, GiraphQLNameInputFieldType, GiraphQLNameOutputFieldType, GiraphQLOutputFieldType, InputType, InputTypeParam, OutputType, SchemaTypes, TypeParam, } from '..'; export function typeFromNonListParam( type: OutputType, configStore: ConfigStore, nullable: boolean, ): GiraphQLNameOutputFieldType { const ref = configStore.getOutputTypeRef(type); const kind = ref instanceof BaseTypeRef ? ref.kind : configStore.getTypeConfig(ref).graphqlKind; const name = ref instanceof BaseTypeRef ? ref.name : configStore.getTypeConfig(ref).name; if (kind !== 'InputObject') { return { kind, ref, nullable, }; } throw new Error( `Expected input param ${name} to be an InputObject, Enum, or Scalar but got ${kind}`, ); } export function typeFromParam( param: TypeParam, configStore: ConfigStore, nullable: FieldNullability<[unknown]>, ): GiraphQLOutputFieldType { const itemNullable = typeof nullable === 'object' ? nullable.items : false; const listNullable = typeof nullable === 'object' ? nullable.list : !!nullable; if (Array.isArray(param)) { return { kind: 'List', type: typeFromNonListParam(param[0], configStore, itemNullable), nullable: listNullable, }; } return typeFromNonListParam(param, configStore, listNullable); } export function inputTypeFromNonListParam( type: InputType, configStore: ConfigStore, required: boolean, ): GiraphQLNameInputFieldType { const ref = configStore.getInputTypeRef(type); const kind = ref instanceof BaseTypeRef ? ref.kind : configStore.getTypeConfig(ref).graphqlKind; const name = ref instanceof BaseTypeRef ? ref.name : configStore.getTypeConfig(ref).name; if (kind === 'InputObject' || kind === 'Enum' || kind === 'Scalar') { return { kind, ref, required, }; } throw new Error( `Expected input param ${name} to be an InputObject, Enum, or Scalar but got ${kind}`, ); } export function inputTypeFromParam( param: InputTypeParam, configStore: ConfigStore, required: FieldRequiredness<[unknown]>, ): GiraphQLInputFieldType { const itemRequired = typeof required === 'object' ? required.items : true; const listRequired = typeof required === 'object' ? required.list : !!required; if (Array.isArray(param)) { return { kind: 'List', type: inputTypeFromNonListParam(param[0], configStore, itemRequired), required: listRequired, }; } return inputTypeFromNonListParam(param, configStore, listRequired); }