Hello everyone.
The polite context to my question is at the bottom of this post but I’d like to value peoples time and put my question first.
I’ve never worked with triples, or the RDF N-Quad format. I’ve been writing Go on and off for about 3 years and only just decided to go all in on it for my backend services and I’m now using dgo. This question is in that context.
Question:
What are the best practices for generating the string literals for both the JSON and triples for queries and mutations?
The thoughts I have:
1.) I’ve gone through the docs extensively and had no problem with mutations and queries with JSON and the default marshalling libraries until I came across doing conditional upserts as described in the dgo readme (example included for clarity):
query = `
query {
user as var(func: eq(email, "wrong_email@dgraph.io"))
}`
mu := &api.Mutation{
Cond: `@if(eq(len(user), 1))`, // Only mutate if "wrong_email@dgraph.io" belongs to single user.
SetNquads: []byte(`uid(user) <email> "correct_email@dgraph.io" .`),
}
req := &api.Request{
Query: query,
Mutations: []*api.Mutation{mu},
CommitNow:true,
}
// Update email only if exactly one matching uid is found.
if _, err := dg.NewTxn().Do(ctx, req); err != nil {
log.Fatal(err)
}
This was fine as it is but my problems started with the fact there was only an example with setNquads
and I wasnt sure how to include the uid(your_var)
in JSON based on the result of the query portion of the upsert. Is there a way to do this and can someone please show me an example? In the meantime I decided to use n-Quads instead which lead me to my next question. (I actually prefer n-Quads I think. So I’d love to know the answer to the next one).
3.) How do I do variable subsitution for triples? Is there a good/bad way to do it like with SQL queries to avoid injections etc?
- Queries have
txn.QueryWithVars
which works for simple stuff. - Mutations dont as far as I can tell.
- Do I use normal
fmt.Sprintf(`query text %s`, cast all my vars from a type here to strings)
(what I’ve done for now) and it works - just seems quite… tiresome to do that for all my queries.
4.) Can i use fmt.Sprintf
for the JSON queries too? I found it hard to do understand how to use the Vars:
property of Txn.do
at the same time as setting a var in my query at the top level. So i just did it myself -_-’
Combine:
q := `query all($a: string) {
all(func: eq(name, $a)) {
name
}
}`
and
query = `
query {
user as var(func: eq(email, "wrong_email@dgraph.io"))
}`
I’ve included what I hacked together (working!) below to show where I got to.
Is there a better way? Are there any good marshalling tools for n-Quads? Should dGraph have some built in or have something like mongoose was for MongoDB but for n-Quads? Sorry, I’ve stored up all my questions 0_0’ around this area.
What I will say though is that if I had this knowledge I feel I can build anything on dgraph - docs are solid for the most part (types could do with some work on removing the old documetation pertaining to the older versions dgraph (or with a discalimer)).
Background and Hello!
I assessed dgraph to use about a year and half ago for my businesses and am extremely excited to see how far the project has come since then. Awesome work from all the team!
I’m using dgraph as the main database for my new business (a management platform for high value collectable items) - we’re launching in the classic car market in about 6 months.
Since the platform heavily weighs on the interactions between all players in the market a graph database was a must, and I’m taking the plunge on using one (dGraph) as the main DB (as inspired by an old talk by @mrjn).