使用 FaunaDB 在 graphQL 中按日期范围查询
Query by date range in graphQL with FaunaDB
我试图复制为 给出的关于同一件事的解决方案。我做了所有描述的事情,我的实现看起来像这样:
架构:
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 测试函数,它运行良好......或者至少 returns 正确的数据:
但是,我尝试通过 graphQL 进行查询,它 returns 是一个空数组!
会发生什么??
完整架构:
#################### 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)
}
所以,这是复制粘贴的典型案例...问题出在这一行:
Range(Match(Index("facturas_by_date")), Var("before"), Var("after"))
请注意,Range 函数的文档说明了这一点:Range( set, start, end )
。
因此,在我的 UDF 代码中,"before"
变量对应于 start
参数。所以当我说一月之后和三月之前时,我说的是从三月到一月。
我修复了它,它起作用了。
我试图复制为
架构:
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 测试函数,它运行良好......或者至少 returns 正确的数据:
但是,我尝试通过 graphQL 进行查询,它 returns 是一个空数组!
会发生什么??
完整架构:
#################### 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)
}
所以,这是复制粘贴的典型案例...问题出在这一行:
Range(Match(Index("facturas_by_date")), Var("before"), Var("after"))
请注意,Range 函数的文档说明了这一点:Range( set, start, end )
。
因此,在我的 UDF 代码中,"before"
变量对应于 start
参数。所以当我说一月之后和三月之前时,我说的是从三月到一月。
我修复了它,它起作用了。