How to update or create entities(with relations) via graphql and typeorm

Viewed 828

I use typeorm, type-graphql and postgres, apollo-graphql-express at my project. I have some troubles with creating or updating entities via typeorm. Example: I made queries and mutations(by my task explanation) such as

//Mutation registerRestaurant(name: String, address: String, phone: String, type: Int, restaurant_key: String, headquarter: Int): Restaurant

//Mutation updateRestaurant(restaurantId: Int!, name: String, address: String, phone: String, type: Int, restaurant_key: String, headquarter: Int): Restaurant

Restaurant entity has headquarter property which returns Headquarter entity(for relation cases), here is an trouble, that then i can't update or create Restaurant entity using id as an argument in mutation. (example: in mutation update restaurant i need to change headquarter with id = int, but Restaurant entiry doesn't allow me to do that because of JoinColumn i think)). I will be very happy if you pay attention to this, if some questions about my code, i will answer, THANKS!

@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 Answers
Related