@Darryl_Naidu, Here is an example.
Schema with an embedded object.
type Address @embedded {
street: String!
city: String!
zipCode: String!
}
type User @collection(name: "users") {
name: String
address: Address
}
type Query {
findUsersByZipcode(zipCode: String!): [User]! @resolver(name: "findUsersByZipcode", paginated: true)
}
Embedded objects do not have a Collection on their own. Therefore you cannot create an Index on its fields from GraphQL API. They are a part of the document they are embedded in.
GraphQL mutation to create a new User with Address embedded into it.
The document would look like this in the Collection users
Map(Paginate(Documents(Collection('users'))),Lambda('x',Get(Var('x'))))
{
data: [
{
ref: Ref(Collection("users"), "291554958903345666"),
ts: 1614307326130000,
data: {
name: "jay",
address: {
street: "Washington Ave",
city: "New York",
zipCode: "19045"
}
}
}
]
}
Create an Index on the zipCode
field.
CreateIndex(
{
name: "users_by_zipcode",
source: Collection("users"),
terms: [{field: ["data", "address", "zipCode"]}]
})
Test the above Index
Map(Paginate(Match(Index('users_by_zipcode'),"19045")),Lambda('x',Get(Var('x'))))
{
data: [
{
ref: Ref(Collection("users"), "291554958903345666"),
ts: 1614307326130000,
data: {
name: "jay",
address: {
street: "Washington Ave",
city: "New York",
zipCode: "19045"
}
}
}
]
}
The Resolver(findUsersByZipcode(zipCode: String!): [User]! @resolver(name: "findUsersByZipcode", paginated: true)
in the Schema file should have created a UDF.
Paginate(Functions())
{
data: [Ref(Ref("functions"), "findUsersByZipcode")]
}
Update this UDF to use the above create Index.
Update(
Function("findUsersByZipcode"),
{
body: Query(Lambda(["zipCode", "size", "after", "before"],
Let(
{
match: Match(Index("users_by_zipcode"),Var("zipCode")),
page: If(
Equals(Var("before"), null),
If(
Equals(Var("after"), null),
Paginate(Var("match"), { size: Var("size") }),
Paginate(Var("match"), { size: Var("size"), after: Var("after") })
),
Paginate(Var("match"), { size: Var("size"), before: Var("before") }),
)
},
Map(Var("page"), Lambda("ref", Get(Var("ref"))))
)
))
}
)
Test this UDF (zipcode, size, after, before)
Call(Function("findUsersByZipcode"), ["19045", 3, null, null])
{
data: [
{
ref: Ref(Collection("users"), "291554958903345666"),
ts: 1614307326130000,
data: {
name: "jay",
address: {
street: "Washington Ave",
city: "New York",
zipCode: "19045"
}
}
}
]
}
Finally, call from this UDF from the GraphQL query.