Connecting Nodes

Voting for links

There’s only one mutation left to be added: allowing users to vote for links. This follows a familiar path:

This would create our vote model, which is used to represent an user vote.

.../graphql-ruby/app/models/vote.rb
class Vote < ActiveRecord::Base
  belongs_to :user, validate: true
  belongs_to :link, validate: true
end

Votes would be created by a mutation and represented by a GraphQL type.

Done! Now you can vote on links:

Relating links with their votes

You can already create votes, but there’s currently no way to fetch them yet! A typical use case would be to get votes for each link using the existing allLinks query.

For that to work, you just have to change the LinkType to have references to its votes.

Now every link, have access to its votes. But GraphQL still doesn’t know about those votes.

Now you can see all votes for links:

Relating users with their votes

Following these same steps, you could also add a new field to make it easier to find all the votes made by the same user.

Now you can see all votes for users:

Next Chapter

Error Handling

Learn best practices for validation of input arguments and implementing error handling in GraphQL with Ruby.

Go to next chapter