【问题标题】:Unable to infer GraphQL type from TypeScript reflection system. Provide explicit type for 'list_of_coordinate_ids' of 'UpdateCoordinatesInput' class无法从 TypeScript 反射系统推断 GraphQL 类型。为“UpdateCoordinatesInput”类的“list_of_coordinate_ids”提供显式类型
【发布时间】:2021-12-04 06:41:41
【问题描述】:

这个错误完全是因为我错误地将list_of_coordinate_ids 用作“字符串数组”。其余代码工作正常。

这是实体又名模型文件。


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
}
 

这是解析器文件


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;
  }
}

这些是输入文件


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;
}

【问题讨论】:

    标签: typescript api graphql typegraphql


    【解决方案1】:

    你也应该在你的输入类型中定义类型

    @InputType()
    export class CreateCurrentRoutesInput {
      @Field(()=>[String]) <---see here
      list_of_coordinate_ids:string[]
    
      @Field()
      carrier_id:number;
    }
    

    这里也一样

    @InputType()
    export class UpdateCurrentRoutesInput {
      @Field(()=>[String], {nullable:true})
      list_of_coordinate_ids?:string[]
    
      @Field({nullable:true})
      carrier_id?:number;
    }
    

    顺便说一句,在我看来 typeorm 列也不正确,可能是数组类型或简单数组?

    @Field(() => [String])
      @Column({
            type: "simple-array", 
            length: 45,
            default:[]
          }) 
      list_of_coordinate_ids:string[];
    

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2021-06-15
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2021-08-18
      • 2019-09-17
      • 2020-11-01
      • 2017-03-08
      相关资源
      最近更新 更多