Universal node (uid) in @hasInverse object in schema

Here is my dilema:

type Comercial {
  id: ID!
  name: String!
  video: Video @hasInverse(field: videoOfObject1)
}

type Promotion {
  id: ID!
  name: String!
  video: Video @hasInverse(field: videoOfObject2)
}
# ...

type Video {
  id: ID!
  url: String!
  videoOfComercial: Comercial
  videoOfPromotion: Promotion
  # ...
}

Since video will be in many types this looks like a bit verbose solution.

I have solve this in DQL with revese:

type <Node> {
}

type <Comercial> {
	name
        video
}

type <Promotion> {
	name
        video
}

type <Video> {
	url
	belongs_to_node
}
<belongs_to_node>: uid @reverse .
<video>: uid @reverse .

What would be elegant way to have only one field in the type Video object, like “belongsToNode”, in GraphQL, or I have to create all the edge types in Video type that it can belongs to in schema?

I don’t get the question.

First, are you using GraphQL or DQL in your project/app? Stick to one only, if you gonna use GraphQL, use DQL only in especial cases.

Why you are using reverse and hasInverse at the same time? both are completely different things.

Why your GraphQL Video Type has so many predicates and your DQL doesn’t?

You say “elegant”, which sounds like a Schema Design question. Designing a Graph DB is completely free to inspiration. I would recommend keeping it simple, as things can get complex fast(with our bias).

I think I understand. You can extract the video on Commercial and Promotion into an interface.

interface VideoContainer {
    video: Video
}

type Commercial implements VideoContainer {
    id: ID!
    name: String!
}

type Promotion implements VideoContainer {
    id: ID!
    name: String!
}

type Video {
    id: ID!
    url: String!
    container: VideoContainer @hasInverse(field: video)
}

I don’t have expierence with DQL-Schemas if you were to use it, but I would stick to a GraphQL-Schema anyways.

1 Like

I am not using DQL and GraphQL in paralel. I am now developing only GraphQl schema. I was using DQL.
Yes, I want to avoid so many predicates, that is my question here.

How I can extract Promotion or Commercial type node id from Video?

Do you mean when querying?
With the schema as mentioned before you could do something like this:

query {
  queryVideo {
    container {
      __typename # Will tell you if the container is a Commercial or Promotion
      ... on Commercial {
        id
        name
      }
      ... on Promotion {
        id
        name
      }
    }
  }
}

Alternatively you could put the id (and name) into the interface as well, would probably fit your case better:

interface VideoContainer {
    id: ID!
    name: String!
    video: Video
}

type Commercial implements VideoContainer {
    commercialProperty: String!
}

type Promotion implements VideoContainer {
    promotionProperty: String!
}

type Video {
    id: ID!
    url: String!
    container: VideoContainer @hasInverse(field: video)
}

And query like this

query {
  queryVideo {
    id
    url
    container {
      id
      name
      __typename # Will tell you if the container is a Commercial or Promotion
      ... on Commercial {
        commercialProperty # Here you could extract Commercial only predicates
      }
      ... on Promotion {
        promotionProperty # Here you could extract Promotion only predicates
      }
    }
  }
}