【问题标题】:Query by date range in graphQL with FaunaDB使用 FaunaDB 在 graphQL 中按日期范围查询
【发布时间】:2021-07-11 16:03:36
【问题描述】:

我试图复制为this question 给出的解决方案,大致相同。我做了所有描述的事情,我的实现看起来像这样:

架构:

type Factura @collection(name: "facturas") {
  number: String! @unique
  date: Date!
  due: Date!
  user: String!
  agent: Agent! @relation
  // More unrelated fields...
}

type query {
  facturasByDateRange(before: Date, after: Date): [Factura!]!
    @resolver(name: "facturas_range", paginated: true)
}

UDF:

Query(
  Lambda(
    ["before", "after", "size", "afterCursor", "beforeCursor"],
    Map(
      Paginate(
        Range(Match(Index("facturas_by_date")), Var("before"), Var("after"))
      ),
      Lambda(["date", "ref"], Let({ factura: Get(Var("ref")) }, Var("factura")))
    )
  )
)

我创建了相应的索引并用几个项目填充了数据库。请注意,我选择将解析器 pagination 参数设置为 true,与我引用的答案的最后一部分形成对比。

我可以通过 Shell 测试该函数,它运行良好……或者至少返回正确的数据:

但是,我通过 graphQL 尝试查询,它返回一个空数组!

会发生什么??

完整架构:

#################### Accounts and centers ####################
type Account @collection(name: "accounts") {
  name: String!
  code: String! @unique
  imputable: Boolean!
  balance: Boolean!
  active: Boolean!
  facturas: [Factura] @relation
}

type Center @collection(name: "centros") {
  code: String! @unique
  name: String!
  active: Boolean!
  facturas: [Factura] @relation
}

#################### Documents ####################

type Detail {
  # Centro y cuenta son solo strings, ya que se usan solo para representar en UI
  centro: String!
  cuenta: String!
  desc: String!
  imponible: Int!
  import: Boolean!
  origin: String!
  q: Int!
  totEx: Int!
  tot5: Int!
  tot10: Int!
  factura: Factura! @relation
}

type Factura @collection(name: "facturas") {
  number: String! @unique
  date: Date!
  due: Date!
  printed: Boolean!
  state: String!
  pay: String!
  user: String!
  agent: Agent! @relation
  client: Client! @relation
  currency: String!
  exchange: Int!
  totEx: Int!
  tot5: Int!
  tot10: Int!
  tot: Int!
  totG: Int!
  tax5: Int!
  tax10: Int!
  centers: [Center] @relation
  accounts: [Account] @relation
  details: [Detail!] @relation
}

type Numbering @collection(name: "numbering") {
  suc: Int!
  boca: Int!
  from: Int!
  to: Int!
  last: Int!
  type: String!
  curr: String!
  number: Int!
  start: Date!
  end: Date!
  state: Boolean!
  tag: String
  comm: String
}

#################### Usuarios y personas ####################

type Client @collection(name: "clients") {
  name: String!
  ruc: String! @unique
  ret: Boolean!
  address: String!
  city: String!
  country: String!
  ext: Boolean!
  email: String
  tel: String!
  fax: String
  contact: String
  facturas: [Factura] @relation
}

type Agent @collection(name: "agents") {
  name: String!
  email: String
  tel: String
  ci: String @unique
  facturas: [Factura] @relation
}

type Query {
  # Accounts
  allAccounts: [Account!]! @index(name: "all_accounts")
  getAccount(code: String!): Account
  activeAccounts(active: Boolean!): [Account!]! @index(name: "active_accounts")
  impAccounts(active: Boolean!, imputable: Boolean!): [Account!]!
    @index(name: "imp_accounts")
  # Centers
  allCenters: [Center!]! @index(name: "all_centers")
  activeCenters(active: Boolean!): [Center!]! @index(name: "active_centers")
  # Clients
  allClients: [Client!]! @index(name: "all_clients")
  # Numbering
  allNumbering: [Numbering!]! @index(name: "all_numbering")
  lastNumbering(type: String!, tag: String!, state: Boolean!): [Numbering!]!
    @index(name: "last_numbering")
  # Facturas
  allFacturas: [Factura!]! @index(name: "all_facturas")
  facturasByDateRange(before: Date, after: Date): [Factura!]!
    @resolver(name: "facturas_range", paginated: true)
}

【问题讨论】:

  • 你能分享完整的架构吗?您最初包含的内容在尝试导入时会导致错误:Target type 'Factura' has no field that can be used in the relationship with the type 'query'. (line 9, column 3): facturasByDateRange(before: Date, after: Date): [Factura!]!(这是在删除与未指定代理类型的关系之后)。
  • 我添加了完整的架构,它应该可以工作......但是我尝试了我最初共享的片段并且遇到了你提到的同样的问题。我不知道为什么!
  • 我刚刚复制了我提到的问题的答案中所做的事情,以及海报的架构,它工作正常。那么我的架构一定有问题......

标签: graphql faunadb


【解决方案1】:

所以,这是一个典型的复制粘贴案例……问题出在这一行:

Range(Match(Index("facturas_by_date")), Var("before"), Var("after"))

请注意,Range 函数的文档说明了这一点:Range( set, start, end )

因此,在我的 UDF 代码中,"before" 变量对应于 start 参数。所以当我说一月之后和三月之前,我是说从三月到一月。

我解决了这个问题。

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 2018-04-10
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多