import { getNamedType, GraphQLArgument, GraphQLField, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, isInterfaceType, } from "graphql"; import { RenderContext } from "../common/RenderContext"; import { ArgMap, Field, FieldMap } from "gqlgen-runtime/dist/types"; import { isEmpty } from "./support"; export function objectType( type: GraphQLObjectType | GraphQLInterfaceType | GraphQLInputObjectType, ctx: RenderContext ) { const typeObj: FieldMap = Object.keys(type.getFields()).reduce>((r, f) => { const field = type.getFields()[f]; const namedType = getNamedType(field.type); const fieldObj: Field = { type: namedType.name }; r[f] = fieldObj; const args = (>field).args || []; if (args.length > 0) { fieldObj.args = args.reduce>((r, a) => { const concreteType = a.type.toString(); const typename = getNamedType(a.type).name; r[a.name] = [typename]; if (typename !== concreteType) { r[a.name]?.push(concreteType); } return r; }, {}); } return r; }, {}); if (isInterfaceType(type) && ctx.schema) { ctx.schema.getPossibleTypes(type).map((t) => { if (!isEmpty(typeObj)) { typeObj[`on_${t.name}`] = { type: t.name }; } }); } if (!isEmpty(typeObj)) { typeObj.__typename = { type: "String" }; } // const scalar = Object.keys(type.getFields()) // .map(f => type.getFields()[f]) // .filter(f => isScalarType(getNamedType(f.type)) || isEnumType(getNamedType(f.type))) // .map(f => f.name) // if (scalar.length > 0) typeObj.scalar = scalar return typeObj; }