Last active
November 6, 2019 19:43
-
-
Save harigopal/124000d16b0188d283c6ed92763b1f4e to your computer and use it in GitHub Desktop.
Conventions for setting up a GraphQL Server on Rails - List users
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 :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