Skip to content

Instantly share code, notes, and snippets.

@mtsmfm
Created August 27, 2020 12:26
Show Gist options
  • Save mtsmfm/c6f51c56042b460a2b622f3fff64277c to your computer and use it in GitHub Desktop.
Save mtsmfm/c6f51c56042b460a2b622f3fff64277c to your computer and use it in GitHub Desktop.
class ArticleEntity < ArticleCommonEntity; end
def as_json
{id: @article.id, thumbnail_url: @article.thumbnail_url}
end
end
get "/articles" do
Article.all.map {|a| ArticleEntity.new(a) }.to_json
end
get "/articles/:id" do
ArticleEntity.new(Article.find(params[:id]).to_json
end
class ArticleListEntity < ArticleCommonEntity; end
class ArticleDetailEntity < ArticleCommonEntity;
def as_json
super.merge(thumbnail_url: @article.thumbnail_url)
end
end
get "/articles" do
Article.all.map {|a| ArticleListEntity.new(a) }.to_json
end
get "/articles/:id" do
ArticleDetailEntity.new(Article.find(params[:id]).to_json
end
post "/graphql" do
query = QueryStore.get(params[:query_id])
AppSchema.execute(query).to_json
end
post "/graphql" do
AppSchema.execute(params[:query]).to_json
end
class ArticleType < GraphQL::Schema::Object
field :id, ID, null: false
field :title, String, null: false
field :thumbnail_url, String, null: false
end
class QueryType < Types::BaseObject
field :articles, [ArticleType], null: true
field :article, ArticleType, null: true do
argument :id, ID, required: true
end
def articles; Article.all; end
def article(id:); Article.find(id); end
end
class AppSchema < GraphQL::Schema
query QueryType
end
post "/graphql" do
AppSchema.execute(params[:query], params[:variables]).to_json
end
get "/articles" do
AppSchema.execute(<<~GQL).to_json
query {
articles {
id, title
}
}
GQL
end
get "/articles/:id" do
AppSchema.execute(<<~GQL, {id: params[:id]}).to_json
query getArticle($id: ID!) {
article(id: $id) {
id, title, thumbnail_url
}
}
GQL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment