@hasInverse in GraphQL and @reverse in DQL: Compatibility and Best Practices

Context

I’m working on a project where I need to use both GraphQL and DQL with Dgraph. I’ve set up my GraphQL schema using @hasInverse for relationships, but I’m encountering some confusion and potential issues when trying to use DQL queries, particularly with reverse relationships.

Example GraphQL Schema

type Library {
  id: ID!
  name: String! @search(by: [hash])
  categories: [Category] @hasInverse(field: "library")
}

type Category {
  id: ID!
  name: String! @search(by: [hash])
  library: Library! @hasInverse(field: "categories")
  parentCategory: Category @hasInverse(field: "subcategories")
  subcategories: [Category] @hasInverse(field: "parentCategory")
  books: [Book] @hasInverse(field: "category")
}

type Book {
  id: ID!
  title: String! @search(by: [term])
  author: String! @search(by: [hash])
  category: Category! @hasInverse(field: "books")
}

Questions

  1. Compatibility: How compatible are @hasInverse (GraphQL) and @reverse (DQL) when used in the same Dgraph instance? Can they coexist, or should I choose one approach exclusively?

  2. DQL Queries with @hasInverse: When using @hasInverse in GraphQL, how should I structure my DQL queries to achieve the equivalent of reverse edge queries (which would normally use the ~ operator)?

  3. Performance Implications: Are there any performance differences between using @hasInverse in GraphQL vs. @reverse in DQL, especially for complex queries or large datasets?

  4. Best Practices: Given that I need to use both GraphQL (for simpler queries) and DQL (for more complex operations), what’s the recommended approach for setting up my schema and relationships?

  5. Mixing Approaches: Is it possible (or advisable) to use a mix of @hasInverse and @reverse in the same schema? If so, how would I go about implementing this?

  6. Trade-offs: What are the main trade-offs to consider when choosing between @hasInverse and @reverse? Are there specific use cases where one is clearly preferable over the other?

  7. Schema Evolution: If I start with one approach and later need to switch or incorporate the other, what kind of migration process would be involved?

I’m looking to understand the best way to structure my schema and queries to effectively use both GraphQL and DQL in Dgraph, while being aware of any limitations or incompatibilities between the two approaches. Any insights, best practices, or recommendations would be greatly appreciated.

Thank you in advance for your help!

P.S.: Reverse Edges when using both DQL and GraphQL possibly related, but still need answers after reading through it and linked discussions.

Long story short, they are two completely different things. You cannot use @reverse with GraphQL.

@hasInverse should be used instead. @reverse is not atomic either, so it is technically not any better from my understanding.

@reverse just enforces the edges as an index using ~ for backwards compatibility, while @hasInverse simply adds the node under the hood in both directions.

Keep your mutations on the GraphQL level, or learn exactly how they work under the hood if you need to do them manually at the DQL level to not break your GraphQL.

J

1 Like

Will do just this, DQL for complex queries but perform the mutation through graphql, seems more complex than ideally but we’ll see.

Is there any part of the docs that would be relevant for this?

Many thanks @jdgamble555

Believe it or not, doing these kind of complex mutations outside of graphql can be more complex.

I haven’t been through the docs since the re-write, but maybe.

You can kind of review some of my older ideas to see how this might work. It is basically what I said, just making a link both ways.

J

2 Likes
1 Like

Had the blog post on my to-read list, was good, and the other post too, sadly no one replied to you, seems to be common over here :frowning:

Interesting series, most flew over my head.

What client do you use for DQL queries on typescript? (dql.execute)

Many thanks!

It uses the Lambda Resolver, written in JS, but you could just call DQL directly. I’ve never done it, but I believe you just connect with the JS package or whatever language. It really depends on your app and language.

1 Like