# Is there a way of assigning a UID to a field with type string OR can I sort the parent node by an edge UID in GraphQL?

**URL:** <https://discuss.dgraph.io/t/is-there-a-way-of-assigning-a-uid-to-a-field-with-type-string-or-can-i-sort-the-parent-node-by-an-edge-uid-in-graphql/18385>\
**Category:** Dgraph Cloud\
**Tags:** graphql, kind:question\
**Created:** [March 15, 2023, 12:39pm UTC](https://discuss.dgraph.io/t/is-there-a-way-of-assigning-a-uid-to-a-field-with-type-string-or-can-i-sort-the-parent-node-by-an-edge-uid-in-graphql/18385 "2023-03-15T12:39:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Poolshark](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/poolshark/32/10074_2.png) [@Poolshark](https://discuss.dgraph.io/u/Poolshark)\
**Post date:** [March 15, 2023, 12:39pm UTC](https://discuss.dgraph.io/t/is-there-a-way-of-assigning-a-uid-to-a-field-with-type-string-or-can-i-sort-the-parent-node-by-an-edge-uid-in-graphql/18385/1 "2023-03-15T12:39:40Z")

</div>

Consider the following schema

```gql
interface Sortable {
  id: ID!
  sortings: [Sorting!]!
}

type Sorting {
  id: ID!
  sequence: Int!
  contextRef: String! @search(by: [exact])
}

type Course {
  id: ID!
  title: String!
  chapters: [Chapter!]
}

type Chapter implements Sortable {
  id: ID!
  title: String!
  
  # from Sortable
  # sortings: [Sorting!]!
}

```

So one `Course` can have multiple `Chapters` which again have multiple `Sortings`. I have multiple sortings because I want to allow that **one** chapter can also belong to many courses but with a **different** sorting. Here’s a sketch to visualise the data model:

 ![Schematic data model](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/3/373a59eb8b706d352df67c217341ada03d160bab.png)

Hence the `contextRef` basically tells me in which context _(in this case type `Course`)_ one specific sorting is valid. I should mention that the context reference not necessarily needs to be the parent node!

I have `contextRef` of type `String` because I need the ability to filter _sortings_ according to the scope I’m dealing with.

```gql
query Course($id: ID!, $string_id: String!) {
  queryCourse(filter: { id: $id }) {
    id
    title
    chapters {
      id
      title
      sortings(filter: { contextRef: { eq: $string_id } }) {
        sequence
      }
   }
  }
}

```

Note that `$id` and `$string_id` are **the same values** - specifically the course `UID`.

So far so good. This will work but I have problems with assigning `contextRef` properly in a single `upsert` block.

### 1. Assiging a UID as a string

If I create a new chapter inside a course, I would run this DQL upsert mutation. I have to run the query since the parent node of `Chapter` is not necessarily the reference node! Thus, I cannot rely on getting the correct reference UID submitted in my custom resolver.

```json
{
  "query": "{ qCourse(func: uid(<ID>)) { c as uid } }",
  "set": {
    "uid": "uid(c)",
    "Course.chapters": {
      "dgraph.type": ["Chapter", "Sortable"],
      "Chapter.title": "Chapter Z",
      "Sorting.sequence": 0,
      "Sorting.contextRef": "uid(c)"
    }
  }
}

```

Unfortunately extracting “uid(c)” for a field of type string is prohibited and thus this is not an option.

> Is there a way to assign a `UID` to a _value variable_ which can be extracted with `val()`?

### 2. Filtering by an edge UID

The other approach would be having a proper **edge** to the node which serves as reference. Thus, I’d extend the schema like so

```gql
interface Node {
  id: ID!
}

interface Sortable {
  id: ID!
  sortings: [Sorting!]!
}

type Sorting {
  id: ID!
  sequence: Int!
  contextRef: Node!
}

type Course implements Node {
  id: ID!
  title: String!
  chapters: [Chapter!]
}

type Chapter implements Sortable {
  id: ID!
  title: String!
  
  # from Sortable
  # sortings: [Sorting!]!
}

```

Now I can easily assign the reference node to the new chapter by

```json
{
  "query": "{ qCourse(func: uid(<ID>)) { c as uid } }",
  "set": {
    "uid": "uid(c)",
    "Course.chapters": {
      "dgraph.type": ["Chapter", "Sortable"],
      "Chapter.title": "Chapter Z",
      "Sortable.sortings": {
        "dgraph.type": "Sorting",
        "Sorting.sequence": 0,
        "Sorting.contextRef": "uid(c)"
      }
    }
  }
}

```

The problem is that I have no clue if I could filter by an edge UID but I guess this is not possible.

```gql
query Course($id: ID!) {
  queryCourse(filter: { id: $id }) {
    id
    title
    chapters {
      id
      title
      sortings(filter: { contextRef: {id: $id} }) {
        sequence
      }
   }
  }
}

```

> Is there a way of filtering by an edge UID?

The only solution that I found so far, is having an additional `@id` field for all nodes which are referenceable and either mapping the `UID` to this field or introducing `UUIDs`. The concept with UUIDs would allow a single DQL mutation on create, whereas the “mapping” of the UID would require to generate the node first and afterwards assign the UID (as string) in a separate mutation.

Any help appreciated!

---

<div class="post-metadata">

**Author:** ![Poolshark](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/poolshark/32/10074_2.png) [@Poolshark](https://discuss.dgraph.io/u/Poolshark)\
**Post date:** [March 17, 2023, 6:07am UTC](https://discuss.dgraph.io/t/is-there-a-way-of-assigning-a-uid-to-a-field-with-type-string-or-can-i-sort-the-parent-node-by-an-edge-uid-in-graphql/18385/2 "2023-03-17T06:07:57Z")

</div>

I found the solution. Totally forgot about `@cascade`. 🙈 I can implement a `Node` interface to all types _(or at least the types I want to be referencable by `Sorting`)_. Sorting then references to Node and I a combination of `@cascade` on Sorting and `@filter` on `contextRef` does the job.

Additionally, introducing the `Node` interface and applying it to **all** types has additional advantages in case you use Relay. I’ve wrote a post about this in case someone is interested

> **[Support for Relay Modern](https://discuss.dgraph.io/t/support-for-relay-modern/12807/28)**
>
> Hi! I know this is a little old but I found a (admittedly very questionable) workaround for Assumption 1 (also part of the GraphQL specs) that a server must provide a root field called node which returns an interface Node. Doing this, will check...

### Full example

##### Schema

```auto
 interface Node {
  id: ID!
}

interface Sortable {
  id: ID!
  sortings: [Sorting!]!
}

type Sorting {
  id: ID!
  sequence: Int!
  contextRef: Node!
}

type Course implements Node {
  id: ID!
  title: String!
  chapters: [Chapter!]
}

type Chapter implements Sortable {
  id: ID!
  title: String!
  
  # from Sortable
  # sortings: [Sorting!]!
}

```

##### Filtered query

```auto
query CourseQuery($id: ID!) {
  queryCourse(filter: { id: [$id] }) {
    chapters {
      sortings @cascade(fields: ["contextRef"]) {
        sequence
        contextRef(filter: { id: [$id] }) {
          id
        }
      }
    }
  }
}

```
