简体   繁体   English

无法从 TypeScript 反射系统推断 GraphQL 类型。 为“UpdateCoordinatesInput”类的“list_of_coordinate_ids”提供显式类型

[英]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.

相关问题 无法从 TypeScript 反射系统推断 GraphQL 类型。 您需要为“地址”class 的“id”提供显式类型 - Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'id' of 'Address' class 无法从 TypeScript 反射系统推断出 GraphQL 类型 - Unable to infer GraphQL type from TypeScript reflection system 推断TypeScript泛型类类型 - Infer TypeScript generic class type 打字稿推断类型而不使用显式类型变量进行回调 - Typescript infer type without using explicit type variable for callback 您需要为“InputType”class 的“文件”提供显式类型。 | 类型-graphql - You need to provide explicit type for 'file' of 'InputType' class. | Type-graphql Typescript:从数组推断类型 - Typescript: Infer type from an array TypeScript 无法从类型化的字符串变量中正确推断类型 - TypeScript is unable to infer type properly from a typed string variable typescript 从子 class 调用的构造函数参数推断类型 - typescript infer type from constructor argument called by sub class 为什么 TypeScript 无法正确推断类型? - Why is TypeScript unable to infer the type properly? 从打字稿中的基类通用类推断子类属性类型 - Infer subclass property type from base class generic in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM