Adding Facets with GraphQL Mutations #9731
-
|
When adding a relationship using a GraphQL mutation, how can I add a facet to that relationship? For example, I have the schema: type Person {
id: ID!
...
people: [Person] @hasInverse(field: people)
...
}When adding new relationships between people using GraphQL, how do I add facets to those relationships? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Hey @theguruofreason — short answer is that you can't, at least not through the GraphQL API directly. Facets are a DQL/RDF concept (key-value annotations on an edge), and the GraphQL layer doesn't generate any mutation input for setting them. So there's no field you can pass in an addPerson/updatePerson mutation to attach a facet to the people edge. There's a GraphQL idiomatic way to create a connector using model edge properties which is to promote the relationship to its own type — an intermediate node that holds the "facet" data as real fields. Not my favorite. Long story short, the GraphQL spec isn't all that "graph-y". Having said that, Dgraph's GraphQL is just a wrapper over DQL, so you could send this mutation via gRPC or the HTTP /mutate endpoint: In the above, it assumes you know the IDs of the nodes you're mutating. The values in the parens are the facets. Note, you wouldn't be able to query over GraphQL using the facets, you'd have to use DQL. And because you have an reverse edge you'd also have to manually stitch the edges (the Dgraph GraphQL system does this automagically thru the generated mutation bindings). Hope this helps. Probably not though. |
Beta Was this translation helpful? Give feedback.
-
|
Have a look at this very long discussion on the matter from six years ago: https://discuss.dgraph.io/t/facets-in-graphql/10429 In the end, there wasn't much appetite for shoe-horning facets into Dgraph's GraphQL. A PR is referenced in the end of that thread that was ultimately abandoned. Your mileage may vary. |
Beta Was this translation helpful? Give feedback.
Hey @theguruofreason — short answer is that you can't, at least not through the GraphQL API directly. Facets are a DQL/RDF concept (key-value annotations on an edge), and the GraphQL layer doesn't generate any mutation input for setting them. So there's no field you can pass in an addPerson/updatePerson mutation to attach a facet to the people edge. There's a GraphQL idiomatic way to create a connector using model edge properties which is to promote the relationship to its own type — an intermediate node that holds the "facet" data as real fields. Not my favorite. Long story short, the GraphQL spec isn't all that "graph-y".
Having said that, Dgraph's GraphQL is just a wrapper over DQL, so…