Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @appendNode and @prependNode directives for updating the store #3155

Closed
wants to merge 8 commits into from

Conversation

zth
Copy link
Contributor

@zth zth commented Jul 31, 2020

This adds two new directives, @appendNode and @prependNode, for updating the store declaratively. The directives will create an edge for the annotated object and prepend/append that edge to the provided connections. It works with single nodes as well as lists of nodes.

Example:

mutation AddComments(
  $input: AddCommentsInput!, 
  $connections: [String!]!, 
  $edgeTypeName: String!
) {
  addComments(input: $input) {
    # It works for lists of nodes as well as for single nodes
    comments @appendNode(connections: $connections, edgeTypeName: $edgeTypeName) {
      id
    }

    # This mutation naturally doesn't make sense, but is just here to illustrate that this works for a single node as well
    comment @appendNode(connections: $connections, edgeTypeName: $edgeTypeName) {
      id
    }
  }
}

@josephsavona
Copy link
Contributor

cc @kassens @tyao1

Copy link
Contributor

@tyao1 tyao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.
Does it work if the edgeType is a constant in the directive?

@zth
Copy link
Contributor Author

zth commented Aug 27, 2020

@tyao1 Thanks for having a look. Yes it does. I added a test to show it, please see the latest commit.

@zth zth requested a review from tyao1 August 27, 2020 17:28
Copy link
Contributor

@tyao1 tyao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks for the extra tests!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyao1 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@tyao1 merged this pull request in 0fe732d.

@zth zth deleted the append-node-directive branch September 4, 2020 10:46
@anatooly
Copy link

ERROR:
Variable '$connections' was defined as type '[String!]!' but used in a location expecting the type '[ID!]!'

@tobias-tengler
Copy link
Contributor

Would be great if these directives could be documented!

@anatooly
Copy link

How do this variant?

mutation CommentCreateMutation(
  $connections: [String!]!
  $input: CommentCreateInput
) {
  commentCreate(input: $input) @appendNode(
      connections: $connections
      edgeTypeName: "CommentEdge"
    ) {
      id
      ...Comment_comment
    }
  }
}

index.js:1 Warning: MutationHandlers: Expected target node to exist.

@giautm
Copy link
Contributor

giautm commented Apr 12, 2021

Hey @anatooly , I found if I use directives on a field with arguments they will not work. Please retry with a field without agrument, I believe there is a bug in implement of those directives

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants