0

I have this query, where messageTags is a joining table for a many to many relation between messages and tags

messageTagsByMessageId {
    __id
    edges {
        node {
            tagId
            tagByTagId {
                id
                name
            }
        }
    }
}

I want to run a deleteTag mutation, and use Relay's @deleteEdge to update the connection automatically. The problem is the mutation returns id, not tagId or tagByTagId.id:

I tried to do it out of the box:

mutation TagDeleteMutation($input:DeleteTagInput!, $connections: [ID!]!) {
  deleteTag(input: $input) {
    tag {
      id @deleteEdge(connections: $connections)
    }
  }
}

I also tried adding to the payload out of the box:

mutation TagDeleteMutation($input:DeleteTagInput!, $connections: [ID!]!) {
  deleteTag(input: $input) {
    tag {
      id @deleteEdge(connections: $connections)
    }
    messageTag {
      tagId @deleteEdge(connections: $connections)
    }
  }
}

No luck. Now I'm working on the Postgres function which graphile uses to build the mutation API. Something like:

CREATE FUNCTION public.delete_tag(tag_id Int)
RETURNS *
AS $$
  DELETE FROM public.tag
    WHERE id=tag_id
  RETURNING id, user_id, name, category_id, id as tag_id;
$$ LANGUAGE sql VOLATILE STRICT;
steve76
  • 302
  • 2
  • 9

1 Answers1

0

What I did was create a function in graphile for the joining table. This provides a mutation on that table.

Then I just combined both mutation in one. One updates the connection for the direct connections, the other updates the relations:

mutation TagDeleteTagMutation($tag:DeleteTagInput!, $messageTag:DeleteMessageTagInput!, $connections: [ID!]!) {
    deleteMessageTag(input: $messageTag) {
        messageTag {
          id @deleteEdge(connections: $connections)
      }
    }
    deleteTag(input: $tag) {
      tag {
        id @deleteEdge(connections: $connections)
      }
    }
}
steve76
  • 302
  • 2
  • 9