I had some thoughts on how the @default directive was implemented in v21.12 (https://github.com/dgraph-io/dgraph/pull/8017) and wondered if it could be more expressive to address diverse use cases. Here are my thoughts…
Why not use [gotemplate](https://pkg.go.dev/text/template)
for templated default values to satisfy the expression use cases? With this, we can use [sprg](http://masterminds.github.io/sprig/)
functions like now
etc. We can even provide the non-defaulted predicate values, headers, or auth values as additional context to render.
For example, the default value can be {{ .parent.name }}
, which will result in the predicate field called name
.
Or {{ .auth.workspace }}
will default to the workspace
value in the auth token.
Or {{ .headers.workspace }}
will default to the workspace
value in the request headers.
Or {{ uuidv4 }}
will default to a UUID.
Or {{ now }}
will result in the current timestamp.
Or an expression {{ now | date_modify "+2h" }}
will result in 2hrs later than the time of evaluation.
This may even support edges
whose IDs
can be templated. I.e.,
{ "id": "{{ .parent.name }}" }
will create or link an edge that shares the same ID as the parent.
Or { "id": "{{ uuidv4 }}" }
will create or link an edge with a unique identifier.
Any thoughts @amaster507 or this does not make any sense?