[英]Unable to infer GraphQL type from TypeScript reflection system. Provide explicit type for 'list_of_coordinate_ids' of 'UpdateCoordinatesInput' class
This error is arising solely because of my incorrect usage of list_of_coordinate_ids
as an "array of strings".这个错误完全是因为我错误地将
list_of_coordinate_ids
用作“字符串数组”。 Rest of the code is working fine.其余代码工作正常。
This is the entity aka model file.这是实体又名模型文件。
import { Entity, BaseEntity, PrimaryGeneratedColumn, Column, CreateDateColumn } from "typeorm";
import { ObjectType, Field, ID, Float } from "type-graphql";
@Entity("current_routes")
@ObjectType()
export class CurrentRoutes extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
unique_route_id:number;
@Field(() => [String])
@Column({type: "varchar", length: 45,array:true, default:[] })
list_of_coordinate_ids:string[];
@Field(() => Date)
@CreateDateColumn()
time_of_route_generation:Date;
//TODO : Add as carriers_on_platform -> OneToMany -> current_routes
@Field(()=>Number)
@Column()
carrier_id :number
}
this is the resolver file这是解析器文件
import { Resolver, Query, Mutation, Arg } from "type-graphql";
import { Coordinates } from "../entity/Coordinate";
import { CurrentRoutes } from "../entity/CurrentRoutes";
import { CreateCoordinatesInput } from "../inputs/CoordinatesInputs/CreateCoordinatesInput";
import { UpdateCoordinatesInput } from "../inputs/CoordinatesInputs/UpdateCoordinatesInput";
import { CreateCurrentRoutesInput } from "../inputs/CurrentRoutesInput/CreateCurrentRoutesInput";
@Resolver()
export class CurrentRoutesResolver {
@Query(() => [CurrentRoutes])
allCurrentRoutes() {
return CurrentRoutes.find()
}
@Mutation(() => CurrentRoutes)
async createCurrentRoutes(@Arg("data") data: CreateCurrentRoutesInput) {
const currentRoutes = CurrentRoutes.create(data);
await currentRoutes.save();
return currentRoutes;
}
@Query(() => CurrentRoutes)
particularCurrentRoute(@Arg("unique_route_id") unique_route_id: string) {
return CurrentRoutes.findOne({ where: { unique_route_id } });
}
@Mutation(() => CurrentRoutes)
async updateCoordinates(@Arg("unique_route_id") unique_route_id: string, @Arg("data") data: UpdateCoordinatesInput) {
const currentRoute = await CurrentRoutes.findOne({ where: { unique_route_id } });
if (!currentRoute) throw new Error("Current route not found!");
Object.assign(currentRoute, data);
await currentRoute.save();
return currentRoute;
}
@Mutation(() => CurrentRoutes)
async deleteCoordinates(@Arg("unique_route_id") unique_route_id: string) {
const currentRoute = await CurrentRoutes.findOne({ where: { unique_route_id } });
if (!currentRoute) throw new Error("Current Route not found!");
await currentRoute.remove();
return currentRoute;
}
}
these are the input files这些是输入文件
import { InputType, Field } from "type-graphql";
@InputType()
export class CreateCurrentRoutesInput {
@Field()
list_of_coordinate_ids:string[]
@Field()
carrier_id:number;
}
import { InputType, Field } from "type-graphql";
@InputType()
export class UpdateCurrentRoutesInput {
@Field({nullable:true})
list_of_coordinate_ids?:string[]
@Field({nullable:true})
carrier_id?:number;
}
you should defined the types also in your input types您还应该在输入类型中定义类型
@InputType()
export class CreateCurrentRoutesInput {
@Field(()=>[String]) <---see here
list_of_coordinate_ids:string[]
@Field()
carrier_id:number;
}
same here同样在这里
@InputType()
export class UpdateCurrentRoutesInput {
@Field(()=>[String], {nullable:true})
list_of_coordinate_ids?:string[]
@Field({nullable:true})
carrier_id?:number;
}
BTW it also seems to me that the typeorm column is not correct and might be type of array or simple-array ?顺便说一句,在我看来 typeorm 列不正确,可能是数组类型或简单数组?
@Field(() => [String])
@Column({
type: "simple-array",
length: 45,
default:[]
})
list_of_coordinate_ids:string[];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.