微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 FaunaDB 在 graphQL 中按日期范围查询

如何解决使用 FaunaDB 在 graphQL 中按日期范围查询

我试图复制为 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 测试该函数,它运行良好……或者至少返回正确的数据:

enter image description here

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

enter image description here

会发生什么??

完整架构:

#################### 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,paginated: true)
}

解决方法

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

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

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

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

我解决了这个问题,它有效。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。