I'm working with nestJs and i'm trying to create a mutation which receives an array of key, values as a parameter. Also, i'm defining an ObjectType which will define the mongoose schema, and graphql objectType.
CarName: the data of the array.SetCarParams: the input to the mutation.Car: The mongoose+graphql schema definition
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
@ObjectType()
export class CarName {
@Field()
key: string;
@Field()
value: string;
}
@InputType()
export class SetCarParams {
@Field(() => [CarName])
names: CarName[];
}
@Schema()
@ObjectType()
export class Car {
@Prop({ type: [CarName] })
@Field(() => [CarName])
names: CarName[];
}
export type CarDocument = Car & Document;
export const CarDto = SchemaFactory.createForClass(Car);
@Mutation(() => Car)
setCar(@Args(camelCase(Car.name)) carParams: SetCarParams) {
console.log('do something');
}
The error i'm receiving : CannotDetermineInputTypeError: Cannot determine a GraphQL input type for the "names". Make sure your class is decorated with an appropriate decorator.
- when i set the type to be String and not
CarName, my structore works. - when i use raw type in the mongoose schema, it doesn't work as well
@Schema()
@ObjectType()
export class Car {
@Prop(
raw({
type: Map,
of: CarName,
}),
)
@Field(() => [CarName])
names: CarName[];
}
- Also, when i try to not use
SetCarParamsas an inputtype to the mutation, it doesn't work as well
@Mutation(() => Car)
setCar(
@Args('names', { type: () => [{ key: String, value: String }] })
names: [{ key: string; value: string }],
) {