import { GraphQLFieldResolver, GraphQLInputObjectType, GraphQLInputType, GraphQLNonNull, GraphQLSchema, GraphQLString, IntrospectionObjectType, IntrospectionType } from 'graphql'; import { DataResolver, GenerateConfig, TypeGenerator } from './GraphQLGenieInterfaces'; import { InputGenerator } from './InputGenerator'; import { Relations, createResolver, getPayloadTypeDef, getPayloadTypeName } from './TypeGeneratorUtilities'; export class GenerateCreate implements TypeGenerator { private objectName: string; private types: IntrospectionObjectType[]; private config: GenerateConfig; private dataResolver: DataResolver; private schema: GraphQLSchema; private fields: object; private resolvers: Map>; private currInputObjectTypes: Map; private currOutputObjectTypeDefs: Set; private schemaInfo: IntrospectionType[]; private relations: Relations; constructor(dataResolver: DataResolver, objectName: string, types: IntrospectionObjectType[], $config: GenerateConfig, currInputObjectTypes: Map, currOutputObjectTypeDefs: Set, schemaInfo: IntrospectionType[], schema: GraphQLSchema, relations: Relations) { this.dataResolver = dataResolver; this.objectName = objectName; this.types = types; this.config = $config; this.currInputObjectTypes = currInputObjectTypes; this.currOutputObjectTypeDefs = currOutputObjectTypeDefs; this.schema = schema; this.schemaInfo = schemaInfo; this.relations = relations; this.fields = {}; this.resolvers = new Map>(); this.generate(); } generate() { this.types.forEach(type => { const args = {}; const createInputName = `Create${type.name}MutationInput`; const createInput = new GraphQLInputObjectType({ name: createInputName, fields: { data: {type: new GraphQLNonNull(new InputGenerator(this.schema.getType(type.name), this.config, this.currInputObjectTypes, this.schemaInfo, this.schema, this.relations).generateCreateInput())}, clientMutationId: {type: GraphQLString} } }); this.currInputObjectTypes.set(createInputName, createInput); args['input'] = { type: new GraphQLNonNull(createInput) }; const outputTypeName = getPayloadTypeName(type.name); this.fields[`create${type.name}`] = { type: outputTypeName, args: args }; this.currOutputObjectTypeDefs.add(getPayloadTypeDef(type.name)); this.resolvers.set(`create${type.name}`, createResolver(this.dataResolver)); }); } public getResolvers(): Map>> { return new Map([[this.objectName, this.resolvers]]); } public getFieldsOnObject(): Map { return new Map([[this.objectName, this.fields]]); } }