Skip to content

Instantly share code, notes, and snippets.

@harigopal
Last active November 6, 2019 19:43
Show Gist options
  • Save harigopal/124000d16b0188d283c6ed92763b1f4e to your computer and use it in GitHub Desktop.
Save harigopal/124000d16b0188d283c6ed92763b1f4e to your computer and use it in GitHub Desktop.
Conventions for setting up a GraphQL Server on Rails - List users
# app/graphql/types/query_type.rb
module Types
class QueryType < Types::BaseObject
field :users, [Types::UserType], null: false
def users
resolver = UsersResolver.new(context)
resolver.users
end
end
end
# app/graphql/types/user_type.rb
module Types
class UserType < Types::BaseObject
field :id, ID, null: false
field :name, String, null: false
field :avatar_path, String, null: false
def avatar_path
# This won't cause an N+1 - see resolver below.
Rails.application.routes.url_helpers.rails_blob_path(user.avatar, only_path: true)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment