This is tagged with Dgraph. Are you using GraphQL or DQL? With DQL, the answer is no, not on the database side. With GraphQL it is possible using a lambda field resolver
So in that case as @verneleem said you can use lambda resolver to run javascript before the data is commited to the db and edit it so it will always contain - instead of space
I am using subscriptions, and it seems lambdas do not support subscriptions. I sort of understand why (since one field basically equals a function), however, this is NOT my intended functionality.
I do feel lambdas should be added to subscriptions, but I digress…
I want to actually put data in the field, not run a function call every time the field is read.
Is there a way to set the field Post.nameKebab when a new Post is created? My goal here is to prevent a front-end user from setting this field when a new Post is created.
You can set a lambda mutation called newPostEntry and set the nameKebab field in that lambda mutation.That way you won’t have to call a function each time and save the data directly to your object instance. Check out the following link for understanding how to use lambdas in slashgql → https://dgraph.io/docs/graphql/lambda/overview/
Please let me know any way to simplify the mutation as there is only one real line of code that matters here.
This security part obviously does not work, as it locks any new adds… This is the question I do not understand. If I cannot prevent someone from running addPost directly, there is not point in creating any of this on the backend.
I tried changing my code to regular DQL, but I am not sure how. The previous query does not seem to work. What do I change this to, as I do not have an understanding of translating this to triples:
const results = await dql.query(`mutation addPost($course: AddPostInput!) {
addPost(input: [$post]) {
course {
id
name
description
published
}
}
}`, {"post": newArgs});
console.log(JSON.stringify(results.data));
return results.data.addPost.post[0].id
}
Option 2)
How do you override the authHeader with your own JWT? I cannot use the variables in:
# Dgraph.Authorization in regular javascript…
Would I change the global.USER variable? This does not work…
@verneleem FYI, I am also looking for this solution.
I want to know how can issue graphql mutations as an ADMIN from lambdas
One way I can think of is to generate a new token within the lambda calling firebaseadmin (as I am using firebase) and pass along that token but to do it inside the lambda seems overly costly to me.
Another thing I can think of is to have my own server running somewhere, send a request to that and get the admin token from there and then pass the new token in graphql requests,
but even then, how exactly to pass the authToken for graphql calls within the lambdas ?
The above is an example of how to pass a JWT. How to make a JWT really depends on your auth setup. If you are using a 3rd party provider for auth such as Auth0, then you should do a login over http to the 3rd party. If you are using a more static configuration, then you could generate the jwt within the lambda script. But keep in mind that if you need to use any 3rd party packages, you will need to compile the script with webpack before pushing to Dgraph Cloud Lambda.
This seems incredibly complicated. I also use firebase for my auth, which is already overly complicated until Field-level auth is implemented in the upcoming release.
I am going to stick with Option 2, which I am so happy it exists (despite the fact JSON is not accepted at this time in dql lambdas).
I have a feeling as slash-dgraph gets more mature, and I get more mature in my dgraph development, I will come back to this example needing more complicated security.