Best way to model relationship properties in GraphQL until facets are available

Hi team,

I am building an application on top of Dgraph GraphQL that will allow users to post content and validate each other’s contributions. Here’s a (vastly simplified) version of what my schema looks like:

type User {
  userName: String! @id
  locationPost: [LocationPost] @hasInverse(field: author)
}

type LocationPost  {
  locationPostId: ID!
  name: String!
  content: String
  country: Country @hasInverse(field: hasLocation)
  author: User! @hasInverse(field: locationPost)
  dateCreated: DateTime
}

type Country {
  countryId: ID!
  name: String!
  hasLocation: [LocationPost] @hasInverse(field: country)
}

Here, I would like users to be able to validate the relationship between LocationPost and Country to verify whether the location that the post pertains to is indeed in a given country. This could take the form of upvotes/downvotes. I would also like to be able to trace these upvotes/downvotes back to the Users that supplied them.

What would be the best way to model this using the current capabilities of Dgraph GraphQL? I’m essentially looking for an equivalent of the @relation directive in Neo4j.

It is my underestanding that Dgraph GraphQL will support facets in the near future, but until that time I am looking for a workable solution. Thanks in advance!

Hey @RoaldSchuring

Could you try and see if the schema below works for your use-case?

type User {
  userName: String! @id
  locationPost: [LocationPost] @hasInverse(field: author)
  upvoted: [LocationPost!]
  downvoted: [LocationPost!]
}

type LocationPost  {
  locationPostId: ID!
  name: String!
  content: String
  country: Country
  author: User!
  upvotedBy: [User!] @hasInverse(field: upvoted)
  downvotedBy: [User!] @hasInverse(field: downvoted)
  dateCreated: DateTime
}

type Country {
  countryId: ID!
  name: String!
  hasLocation: [LocationPost] @hasInverse(field: country)
}

Note - You only need to specify @hasInverse for one of the fields and it is automatically also added in the opposite direction in the generated schema.

Thank you Pawan! It doesn’t completely solve the problem, but in the meantime I have found a different workaround. A complicating factor that I maybe didn’t explain clearly was that I want to allow users to upvote not only the locationpost itself, but the relationship between the locationpost and a country.

In the end, I have created a Relationship node type that connects to both the locationpost and the country. This Relationship node type then needs to be created whenever a node-node connection is made in the graph. Upvote is then a predicate of the Relationship node type.

This obviously leads to a lot of nodes and connections in the graph, but it seems to be working fine for my purposes.

Thanks again for the response - and the tip re: @inverse.

1 Like