for the sake of simplicity let’s say i have a schema with 2D vectors.
type Vector
{
id: ID!
x: Float!
y: Float!
}
I’m trying to come up with a simple query fetching vectors for which y > x. How do i do it ? Seems trivial but really i haven’t found any simple solution…
Hi @lumi
As far as I’m concerned there is not a clean and very efficient way to do that. One way is to use a Lambda Query:
In your schema use this:
type Vector {
VectorID: ID!
X: Int
Y: Int
}
type Query {
findXbiggerThanY(name: String): [Vector] @lambda
}
And in your Lambda Script add this function:
async function findXbiggerThanY({args, dql, graphql}) {
let { data: { queryVector } } = await graphql(`
{
queryVector {
VectorID
X
Y
}
}
`);
queryVector = queryVector.filter(({ X, Y })=> X > Y);
return queryVector;
}
self.addGraphQLResolvers({
"Query.findXbiggerThanY": findXbiggerThanY,
})
Now if you make this query you can get the results:
query MyQuery {
findXbiggerThanY {
VectorID
X
Y
}
}
The better choice is to store the result when you are changing X or Y in another field(I know that if you want to do various math operations this would not be a good option).
If you decided to store extra information in your type using @lambdaOnMutate will be helpful to automate the calculations and store them.
I did try your solution with Lambda Query but am getting this error: Evaluation of custom field failed because external request returned an error: unexpected error with: 400 for field: findXbiggerThanY within type: Query.
Any ideas as to why it fails? or how to debug it ?
If you are using dgraph cloud please add your schema and lambda function here maybe I can help.
Also the example I explained is still in this url you can give it a try: https://green-pine.us-east-1.aws.cloud.dgraph.io/graphql
Thanks, i tried your url. It seems to work fine on your side. On my side, however, i get the error i mentioned. I actually locally am using the schema lambda and query that you posted so literally doing copy paste. I’m using dockers for dgraph and lambda server so issuing commands:
sudo docker run -it -p 8080:8080 dgraph/standalone:master
sudo docker run -it --rm -p 8686:8686 -v /path/to/script/myLmbd.js -e DGRAPH_URL=http://localhost:8080 dgraph/dgraph-lambda
if any ideas as to what’s wrong, do let me know please. Thank you.