Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ready to deploy, I think
- Loading branch information
pletcher
committed
Aug 1, 2017
1 parent
5d0f8a9
commit afab0f5
Showing
23 changed files
with
279 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class GraphqlController < ApplicationController | ||
def execute | ||
variables = ensure_hash(params[:variables]) | ||
query = params[:query] | ||
operation_name = params[:operationName] | ||
context = { | ||
# Query context goes here, for example: | ||
# current_user: current_user, | ||
} | ||
result = TextsServerSchema.execute(query, variables: variables, context: context, operation_name: operation_name) | ||
render json: result | ||
end | ||
|
||
private | ||
|
||
# Handle form data, JSON body, or a blank value | ||
def ensure_hash(ambiguous_param) | ||
case ambiguous_param | ||
when String | ||
if ambiguous_param.present? | ||
ensure_hash(JSON.parse(ambiguous_param)) | ||
else | ||
{} | ||
end | ||
when Hash, ActionController::Parameters | ||
ambiguous_param | ||
when nil | ||
{} | ||
else | ||
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}" | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Functions::Pagination < GraphQL::Function | ||
attr_reader :type | ||
|
||
description "Provide pagination boilerplate for common lists of records" | ||
|
||
argument :limit, types.Int, default_value: 20, prepare: -> (limit, ctx) { [limit, 50].min } | ||
argument :offset, types.Int, default_value: 0 | ||
|
||
def initialize(children: :itself, type:) | ||
@children = children | ||
@type = type | ||
end | ||
|
||
def call(obj, args, ctx) | ||
obj.send(@children).limit(args[:limit]).offset(args[:offset]) | ||
end | ||
end |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TextsServerSchema = GraphQL::Schema.define do | ||
# mutation(Types::MutationType) | ||
query(Types::QueryType) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Types::AuthorType = GraphQL::ObjectType.define do | ||
name "Author" | ||
|
||
field :id, !types.ID | ||
field :name, !types.String | ||
field :slug, !types.String | ||
field :works, | ||
function: Functions::Pagination.new(children: :works, type: types[Types::WorkType]) | ||
field :work_by_id, Types::WorkType do | ||
argument :id, !types.ID | ||
|
||
description "Find a work by this author with the given (integer) ID" | ||
resolve -> (obj, args, ctx) do | ||
puts args[:id] | ||
puts obj.id | ||
Work.find_by(author_id: obj.id, id: args[:id].to_i) | ||
end | ||
end | ||
field :work_by_slug, Types::WorkType do | ||
argument :slug, !types.String | ||
|
||
description "Find a work by this author with the given slug" | ||
resolve -> (obj, args, ctx) { Work.find_by(author_id: obj.id, slug: args[:slug]) } | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Types::CorpusType = GraphQL::ObjectType.define do | ||
name "Corpus" | ||
|
||
field :id, !types.ID | ||
field :link, types.String | ||
field :slug, !types.String | ||
field :title, !types.String | ||
field :works, | ||
function: Functions::Pagination.new(children: :works, type: types[Types::WorkType]) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Types::JsonType = GraphQL::ScalarType.define do | ||
name "JSON" | ||
|
||
coerce_input -> (json, ctx) { JSON.parse(json) } | ||
coerce_result -> (json, ctx) { JSON.dump(json) } | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Types::LanguageType = GraphQL::ObjectType.define do | ||
name "Language" | ||
|
||
field :id, !types.ID | ||
field :authors, types[Types::AuthorType] | ||
field :corpora, types[Types::CorpusType] | ||
field :slug, !types.String | ||
field :title, !types.String | ||
field :works, | ||
function: Functions::Pagination.new(children: :works, type: types[Types::WorkType]) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Types::MutationType = GraphQL::ObjectType.define do | ||
name "Mutation" | ||
|
||
# TODO: Add Mutations as fields | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
Types::QueryType = GraphQL::ObjectType.define do | ||
name "Query" | ||
|
||
field :authors do | ||
type types[Types::AuthorType] | ||
|
||
argument :limit, types.Int, default_value: 20 | ||
argument :offset, types.Int, default_value: 0 | ||
|
||
description "List all authors" | ||
resolve -> (obj, args, ctx) { Author.limit(args[:limit]).offset(args[:offset]) } | ||
end | ||
|
||
field :author_by_id do | ||
type Types::AuthorType | ||
|
||
argument :id, !types.ID | ||
description "Find an author by its (integer) ID" | ||
resolve -> (obj, args, ctx) { Author.find(args[:id]) } | ||
end | ||
|
||
field :author_by_slug do | ||
type Types::AuthorType | ||
|
||
argument :slug, !types.String | ||
description "Find an author by its slug" | ||
resolve -> (obj, args, ctx) { Author.find_by(slug: args[:slug]) } | ||
end | ||
|
||
field :corpora do | ||
type types[Types::CorpusType] | ||
|
||
description "List all corpora" | ||
resolve -> (obj, args, ctx) { Corpus.all } | ||
end | ||
|
||
field :corpus_by_id do | ||
type Types::CorpusType | ||
|
||
argument :id, !types.ID | ||
description "Find a corpus by its (integer) ID" | ||
resolve -> (obj, args, ctx) { Corpus.find(args[:id]) } | ||
end | ||
|
||
field :corpus_by_slug do | ||
type Types::CorpusType | ||
|
||
argument :slug, !types.String | ||
description "Find a corpus by its slug" | ||
resolve -> (obj, args, ctx) { Corpus.find_by(slug: args[:slug]) } | ||
end | ||
|
||
field :languages do | ||
type types[Types::LanguageType] | ||
|
||
description "List all languages" | ||
resolve -> (obj, args, ctx) { Language.all } | ||
end | ||
|
||
field :language_by_id do | ||
type Types::LanguageType | ||
|
||
argument :id, !types.ID | ||
description "Find a language by its (integer) ID" | ||
resolve -> (obj, args, ctx) { Language.find(args[:id]) } | ||
end | ||
|
||
field :language_by_slug do | ||
type Types::LanguageType | ||
|
||
argument :slug, !types.String | ||
description "Find a language by its slug" | ||
resolve -> (obj, args, ctx) { Language.find_by(slug: args[:slug]) } | ||
end | ||
|
||
field :work_by_id do | ||
type Types::WorkType | ||
|
||
argument :id, !types.ID | ||
description "Find a work by its (integer) ID" | ||
resolve -> (obj, args, ctx) { Work.find(args[:id]) } | ||
end | ||
|
||
field :work_by_slug do | ||
type Types::WorkType | ||
|
||
argument :slug, !types.String | ||
description "Find a work by its slug" | ||
resolve -> (obj, args, ctx) { Work.find_by(slug: args[:slug]) } | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Types::TextNodeType = GraphQL::ObjectType.define do | ||
name "TextNode" | ||
|
||
field :id, !types.ID | ||
field :index, !types.Int | ||
field :location, !types[types.Int] | ||
field :data, Types::JsonType | ||
field :entity_ranges, types[Types::JsonType] | ||
field :inline_style_ranges, types[Types::JsonType] | ||
field :text_node_type, !types.String | ||
field :key, !types.String | ||
field :text, types.String | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Types::WorkType = GraphQL::ObjectType.define do | ||
name "Work" | ||
|
||
field :id, !types.ID | ||
field :english_title, types.String | ||
field :form, types.String | ||
field :original_title, !types.String | ||
field :slug, !types.String | ||
field :structure, types.String | ||
field :urn, types.String | ||
field :text_nodes, | ||
function: Functions::Pagination.new(children: :text_nodes, type: types[Types::TextNodeType]) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Author < ApplicationRecord | ||
has_many :works | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Corpus < ApplicationRecord | ||
has_many :works | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Language < ApplicationRecord | ||
has_many :works | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
class Work < ApplicationRecord | ||
has_many :text_nodes | ||
|
||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
Rails.application.routes.draw do | ||
if Rails.env.development? | ||
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql" | ||
end | ||
|
||
post "/graphql", to: "graphql#execute" | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
end |
This file contains 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
Oops, something went wrong.