如何通过graphql和typeorm更新或创建实体(具有关系)

时间:2019-10-14 08:58:37

标签: postgresql apollo-server typeorm type-graphql

在我的项目中,我使用typeorm,type-graphql和postgres,apollo-graphql-express。通过typeorm创建或更新实体时遇到一些麻烦。 示例:我进行了查询和更改(通过任务说明),例如

//静音 registerRestaurant(名称:字符串,地址:字符串,电话:字符串,类型:整数,restaurant_key:字符串,总部:整数):餐馆

//静音 updateRestaurant(restaurantId:Int !,名称:String,地址:String,电话:String,类型:Int,restaurant_key:String,总部:Int):餐厅

餐厅实体具有总部属性,该属性返回总部实体(对于关联案例),这是一个麻烦,那就是我无法使用id作为突变中的参数来更新或创建餐厅实体。 (例如:在突变更新餐厅中,我需要更改id = int的总部,但由于我认为JoinColumn,餐厅entry不允许我这样做))。 如果您对此有所注意,我将非常高兴,如果对我的代码有一些疑问,我会回答,谢谢!

@ObjectType()
@Entity("restaurants")
export class Restaurant extends BaseEntity {
@Field(() => Int)
@PrimaryGeneratedColumn()
id: number;

@Field()
@Column("text", { default: '' })
name: string;

@Field()
@Column("text", { default: '' })
address: string;

@Field()
@Column("text", { default: '' })
phone: string;

@Field(() => Int)
@Column("int", { default: 0 })
type: number;

@Field()
@Column("text", { default: '' })
restaurant_key: string; 

@OneToOne(() => Headquarter)
@Field(() => Headquarter)
@JoinColumn()
headquarter: Headquarter; 
}



@Resolver()
export class RestaurantResolver {
@Query(() => Restaurant)
async getRestaurant(
@Arg("restaurantId", { nullable: false }) restaurantId: number
) {
    console.log(await Restaurant.findOne(restaurantId, { relations: 
["headquarter"] }));
const restaurant = await Restaurant.findOne(restaurantId, { relations: 
["headquarter"] });
    if (restaurant) {
      return restaurant;
    } else throw new Error("restaurant not found");
  };

@Query(() => [Restaurant])
async getRestaurants() {
  try {
    return await Restaurant.find({ relations: ["headquarter"] });
  } catch (error) {
    console.log(error);
    throw new Error('restaurants not found');
  }
};

@Mutation(() => Restaurant)
async registerRestaurant(
  @Arg("name", { nullable: false }) name: string, // nullable: false means 
  REQUIRED FIELD
  @Arg("address", { nullable: false }) address: string,
  @Arg("phone", { nullable: false }) phone: string,
  @Arg("type", { nullable: false }) type: number,
  @Arg("restaurant_key", { nullable: false }) restaurant_key: string,
  // @Arg("headquarter", { nullable: false }) headquarter: number
) {
    const restaurant = new Restaurant();
    restaurant.name = name || restaurant.name;
    restaurant.address = address || restaurant.address;
    restaurant.phone = phone || restaurant.phone;
    restaurant.type = type || restaurant.type;
    restaurant.restaurant_key = restaurant_key || 
     restaurant.restaurant_key;
    // restaurant.headquarter = headquarter || restaurant.headquarter; 
    //??? how to update when in Restaurant Entity is OnetoOne and 
    JoinColumn

    try {
      return await Restaurant.save(restaurant);
    } catch (error) {
      console.log(error);
      return error.message;
    };   
  };

  @Mutation(() => Restaurant)
  async updateRestaurant(
  @Arg("restaurantId", { nullable: false }) restaurantId: number,
  @Arg("name", { nullable: true }) name?: string,
  @Arg("address", { nullable: true }) address?: string,
  @Arg("phone", { nullable: true }) phone?: string,
  @Arg("type", { nullable: true }) type?: number,
  @Arg("restaurant_key", { nullable: true }) restaurant_key?: string,
  @Arg("headquarter", { nullable: true }) headquarter?: number
  )  {
  console.log("phone",phone);
  const restaurantToUpdate = await Restaurant.findOne(restaurantId);
  console.log(restaurantToUpdate);
  if (restaurantToUpdate) {
    restaurantToUpdate.name = name || restaurantToUpdate.name;
    restaurantToUpdate.address = address || restaurantToUpdate.address;
    restaurantToUpdate.phone = phone || restaurantToUpdate.phone;
    restaurantToUpdate.type = type || restaurantToUpdate.type;
    restaurantToUpdate.restaurant_key = restaurant_key || 
    restaurantToUpdate.restaurant_key;
    restaurantToUpdate.headquarter = headquarter || 
    restaurantToUpdate.headquarter; // type number is not assignable to type 
    Headquarter
    return Restaurant.save(restaurantToUpdate);
  } else {
    throw new Error("restaurant not found");
  };
  }; 
  };

0 个答案:

没有答案