Last active
November 6, 2019 19:27
-
-
Save harigopal/d189b80d9d62acdde4bb6e4f8bd4d364 to your computer and use it in GitHub Desktop.
Conventions for setting up a GraphQL Server on Rails - Query a single user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/types/query_type.rb | |
module Types | |
class QueryType < Types::BaseObject | |
field :user, Types::UserType, null: false do | |
argument :id, ID, required: true | |
end | |
def user | |
resolver = UserResolver.new(context, args) | |
resolver.user | |
end | |
end | |
end | |
# app/queries/user_resolver.rb | |
class UserResolver < ApplicationQuery | |
property :id | |
def user | |
if current_user.admin? | |
User.find(id) | |
else | |
current_user.team.users.find(id) | |
end | |
end | |
def authorized? | |
return false if current_user.blank? | |
current_user.admin? || | |
current_user.id == id || | |
current_user.team.users.where(id: id).exists? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment