65685

React Relay: Complex mutation fat query

Question:

I'm creating a voting applications with polls that can be voted. I'm currently stuck on voting for a poll (essentially, creating a vote).

My schema:

(Long story short: polls are accessible from the main store, but the viewer's polls and votes can be accessed directly by him (as fields). votes also are accessible from the min store, but the poll's votes can be accessed also directly by it.

query { store { polls { //poll connection edges { node { //poll votes { //vote connection ... } } } } votes { //vote connection edges { node { //vote ... } } } } viewer { polls { //poll connection edges { node { //poll votes { //vote connection ... } } } } votes { //vote connection edges { node { //vote ... } } } } }

This complex schema makes me confused about how should I define my fat query, since it should define all that could be changing.

<strong>Here's what can change:</strong>

<ol><li>A vote is created (so voteEdge in the fat query).</li> <li>A vote is added to the votes connection under store.</li> <li>A vote is added to the votes connection under viewer.</li> <li>A vote is added to the votes connection under some poll in the polls connection under store.</li> <li>(only if the viewer is also the poll's creator, which is possible): A vote is added to the votes connection under some poll in the polls connection under viewer.</li> </ol>

So my question is how should I express this in my fat query, should this be suffice?

getFatQuery() { return Relay.QL` fragment on CreateVoteMutation { voteEdge, viewer{ polls, voteCount votes, }, store{ polls, voteCount, votes, } } `; }

Or should I somehow include votes under polls?

getFatQuery() { return Relay.QL` fragment on CreateVoteMutation { voteEdge, viewer{ polls{ edges{ node{ voteCount, votes } } }, voteCount votes, }, store{ polls{ edges{ node{ voteCount, votes } } }, voteCount, votes, } } `; }

Thanks!

Answer1:

Looks like you have the right idea. When defining your FatQuery in Relay, you should keep your return fields as optimal as possible, since that's the benefit of GraphQL (you don't need to return more than you will use in your client). So your FatQuery should look like this:

getFatQuery() { return Relay.QL` fragment on CreateVoteMutation { voteEdge, viewer{ polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later name timestamp } voteCount // scalar integer votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type name timestamp } }, store{ polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later name timestamp } voteCount // scalar integer votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type name timestamp } } } `; }

The idea is that you could technically return votes again as a sub-selection of polls; however, there's no need to since you return is under viewer already. this will give you the total voteCount and all Votes in your system. If you wanted to filter the Votes and VoteCount by Polls, then you could do the opposite, which would look something like this:

getFatQuery() { return Relay.QL` fragment on CreateVoteMutation { voteEdge, viewer{ polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later name timestamp voteCount // scalar integer votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type name timestamp } } }, store{ polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later name timestamp voteCount // scalar integer votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type name timestamp } } } } `; }

Hope this helps!

Recommend

  • RewriteCond and RewriteRule in .htaccess
  • Use WPF object to 'punch' hole in another?
  • Retrieve google contact based on contact Id
  • Visual Studio extension: Change the hint path of an assembly reference
  • JUnit testing package private class in Maven formatted project
  • A limitation of Sqlite3's full text search doesn't allow ORs with MATCHes. Workaround?
  • Trying to get mvc resources to serve my static resources
  • Thrust filter by key value
  • How to name a 'group' check box in Adobe Reader when wanting to fill form by FDF / XFDF
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Calculating ratio of reciprocated ties for each node in igraph
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Simple linked list-C
  • Check for zero lines output from command over SSH
  • CakePHP ACL tutorial initDB function warnings
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • How does this usort cmp function actually work?
  • D3 get axis values on zoom event
  • Custom validator control occupying space even though display set to dynamic
  • ActiveRecord query for a count of new users by day
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • JSON response opens as a file, but I can't access it with JavaScript
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Can Jackson SerializationFeature be overridden per field or class?
  • Is possible to count alias result on mysql
  • Display Images one by one with next and previous functionality
  • Calling of Constructors in a Java
  • retrieve vertices with no linked edge in arangodb
  • SQL merge duplicate rows and join values that are different
  • how does django model after text[] in postgresql [duplicate]
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • How can i traverse a binary tree from right to left in java?