【问题标题】:Can you append values to a GraphQL query?您可以将值附加到 GraphQL 查询吗?
【发布时间】:2022-01-29 04:08:17
【问题描述】:

我正在使用带有 Oracle Db 的 GraphQL 和 Typeorm,并且我正在尝试选择特定表中的所有数据。正在执行的查询是

SELECT "inventory"."id" AS "inventory_id", "inventory"."value" AS "inventory_value", "inventory"."description" AS "inventory_description" FROM "inventory" "inventory"

应该是什么时候

SELECT "inventory"."id" AS "inventory_id", "inventory"."value" AS "inventory_value", "inventory"."description" AS "inventory_description" FROM "dbname.inventory"

数据库“dbname”通过公共 Oracle 数据库链接。

无论如何我可以附加“dbname”。到 GraphQL 查询的表部分,还是有更好的方法来修复?

附件是我的模型和解析器,如果您有任何问题/建议,请告诉我。提前致谢,如果有任何需要澄清的地方,请告诉我。

// Inventory.ts
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
import { ObjectType, Field } from "type-graphql";

@Entity()
@ObjectType()
export class inventory extends BaseEntity {
  @PrimaryGeneratedColumn("uuid") id: number;

  @Field()
  @Column({ type: "varchar" })
  value: number;

  @Field()
  @Column({ type: "varchar" })
  description: string;
  
}
// InventoryResolver.ts
import { Resolver, Query } from "type-graphql";
import { inventory } from "../../models/Inventory";

@Resolver(inventory)
export class InventoryResolver {
  constructor() {}

  @Query(() => inventory, { nullable: true })
  async queryInventory() {
    return inventory.find();
  }
}

【问题讨论】:

    标签: typescript oracle graphql typeorm


    【解决方案1】:

    该问题与type-graphql 解析器无关。您只需在 @Entity 装饰器的配置中指定数据库名称。所以根据typeorm decorator reference

    ...
    @Entity({
      name: "inventory"
      database: "dbname", // <-- 
    })
    @ObjectType()
    export class inventory extends BaseEntity {
    ...
    

    【讨论】:

    • 我添加了您的建议,并且可以在使用装饰器时看到更改。尝试查询时,我收到查询错误并且表不存在。我的问题是因为我需要通过dbname.inventory 的数据库链接而出现吗​​?
    猜你喜欢
    • 2010-09-08
    • 2011-06-12
    • 2010-10-24
    • 2021-10-31
    • 1970-01-01
    • 2018-11-10
    • 2020-05-21
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多