diff --git a/Gemfile b/Gemfile index 8d7b3eb..ed4ed9f 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,7 @@ gem 'jbuilder', '~> 2.5' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +gem 'graphql' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console @@ -47,7 +48,10 @@ group :development do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' + gem 'graphiql-rails' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] + +gem 'graphiql-rails', group: :development \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 5576ce4..ca42281 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,6 +55,9 @@ GEM ffi (1.9.18) globalid (0.3.7) activesupport (>= 4.1.0) + graphiql-rails (1.4.1) + rails + graphql (1.5.5) i18n (0.8.1) jbuilder (2.6.3) activesupport (>= 3.0.0, < 5.2) @@ -156,6 +159,8 @@ PLATFORMS DEPENDENCIES byebug coffee-rails (~> 4.2) + graphiql-rails + graphql jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb new file mode 100644 index 0000000..6da8448 --- /dev/null +++ b/app/controllers/graphql_controller.rb @@ -0,0 +1,32 @@ +class GraphqlController < ApplicationController + def execute + variables = ensure_hash(params[:variables]) + query = params[:query] + context = { + # Query context goes here, for example: + # current_user: current_user, + } + result = ReproduceGraphqlHangSchema.execute(query, variables: variables, context: context) + 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 diff --git a/app/graphql/mutations/.keep b/app/graphql/mutations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/graphql/mutations/create_post.rb b/app/graphql/mutations/create_post.rb new file mode 100644 index 0000000..2d82cf4 --- /dev/null +++ b/app/graphql/mutations/create_post.rb @@ -0,0 +1,12 @@ +Mutations::CreatePost = GraphQL::Relay::Mutation.define do + name "CreatePost" + # TODO: define return fields + # return_field :post, Types::PostType + + # TODO: define arguments + # input_field :name, !types.String + + resolve ->(obj, args, ctx) { + # TODO: define resolve function + } +end diff --git a/app/graphql/reproduce_graphql_hang_schema.rb b/app/graphql/reproduce_graphql_hang_schema.rb new file mode 100644 index 0000000..cc38cf3 --- /dev/null +++ b/app/graphql/reproduce_graphql_hang_schema.rb @@ -0,0 +1,4 @@ +ReproduceGraphqlHangSchema = GraphQL::Schema.define do + query(Types::QueryType) + mutation(Types::MutationType) +end diff --git a/app/graphql/types/.keep b/app/graphql/types/.keep new file mode 100644 index 0000000..e69de29 diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb new file mode 100644 index 0000000..99d5c63 --- /dev/null +++ b/app/graphql/types/mutation_type.rb @@ -0,0 +1,5 @@ +Types::MutationType = GraphQL::ObjectType.define do + name 'Mutation' + + field :createPost, field: Mutations::CreatePost +end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb new file mode 100644 index 0000000..f38fb8b --- /dev/null +++ b/app/graphql/types/query_type.rb @@ -0,0 +1,13 @@ +Types::QueryType = GraphQL::ObjectType.define do + name "Query" + # Add root-level fields here. + # They will be entry points for queries on your schema. + + # TODO: remove me + field :testField, types.String do + description "An example field added by the generator" + resolve ->(obj, args, ctx) { + "Hello World!" + } + end +end diff --git a/config/routes.rb b/config/routes.rb index 787824f..1478e00 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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