Removing nested entity

Suppose I have a Schema like:

type A {
  Id: ID
  propA: String
}

type B {
  Id: ID
  propB: String
  Links : [Link]
}

type Link {
  propLink : String
  propLink2 : String
  nestedA: A
}

So basically B can link to A’s but each link from B to A carries some extra properties.

I want to update one B and remove all of the links to As that it contains. I tried first setting its ‘Links’ property to an empty array, like so:

mutation MyMutation {
  updateB(input: {filter: {propB: {allofterms: "propB content"}}, set: {Links:[ ] }}) {
    B {
      Id
      Links {
        propLink
        propLink2
        A {
          Id
        }
      }
    }
  }
}

But all of the links are still there after this mutation. Then I tried passing the exact instance of an ‘A’ I’d like to remove:

mutation MyMutation {
  updateB(input: {filter: {propB: {allofterms: "propB content"}}, remove: {Links:[
{
  propLink : "prop1"
  propLink2: "prop2"
  A {
    Id: "0x4e12"

  }
}

 ] }}) {
    B {
      Id
      Links {
        propLink
        propLink2
        A {
          Id
        }
      }
    }
  }
}

And this responds with a panic:

{
  "errors": [
    {
      "message": "Internal Server Error - a panic was trapped.  This indicates a bug in the GraphQL server.  A stack trace was logged.  Please let us know by filing an issue with the stack trace."
    }
  ]
}

Whats the right way to do what I need? Is there a better schema def to achieve this? Does each Link property need to have its own ID?

Hi,
In order to update a nested predicate you should pass the whole object of B without A.
Your problem was that you pass only the empty links array

You can also use the remove keyword and set A to null like in the following example:

 name: "Update delete mutation with variables and null"
  gqlmutation: |
    mutation updatePost($patch: UpdatePostInput!) {
      updatePost(input: $patch) {
        post {
          postID
        }
      }
    }
  gqlvariables: |
    { "patch":
      { "filter": {
          "postID": ["0x123", "0x124"]
        },
        "remove": {
          "text": null
        }
      }
    }

Let me know if it works for you,
Or

1 Like

Hello, thanks for the reply.

First solution (pass the whole B obj on ‘set’ without the Links obj) does not result on a instance of B without Links (or an empty Links list which would also be ok).

Second suggestion (with variables and “remove” and to set to null) works!

Thank you!

If an empty list of links is ok you can pass the object B with an empty one.
The idea is that in order to updated a nested object you should pass it with its parent.

Great!

You’re welcome :slight_smile: