Skip to content

Instantly share code, notes, and snippets.

@harigopal
Last active November 6, 2019 19:27
Show Gist options
  • Save harigopal/d189b80d9d62acdde4bb6e4f8bd4d364 to your computer and use it in GitHub Desktop.
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
# 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