Skip to content

Commit 71b7252

Browse files
committed
rails g graphql:install
1 parent 9eba92f commit 71b7252

21 files changed

+254
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
class GraphqlController < ApplicationController
4+
# If accessing from outside this domain, nullify the session
5+
# This allows for outside API access while preventing CSRF attacks,
6+
# but you'll have to authenticate your user separately
7+
# protect_from_forgery with: :null_session
8+
9+
def execute
10+
variables = prepare_variables(params[:variables])
11+
query = params[:query]
12+
operation_name = params[:operationName]
13+
context = {
14+
# Query context goes here, for example:
15+
# current_user: current_user,
16+
}
17+
result = GraphqlRubyIssueReproductionSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
18+
render json: result
19+
rescue StandardError => e
20+
raise e unless Rails.env.development?
21+
handle_error_in_development(e)
22+
end
23+
24+
private
25+
26+
# Handle variables in form data, JSON body, or a blank value
27+
def prepare_variables(variables_param)
28+
case variables_param
29+
when String
30+
if variables_param.present?
31+
JSON.parse(variables_param) || {}
32+
else
33+
{}
34+
end
35+
when Hash
36+
variables_param
37+
when ActionController::Parameters
38+
variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables.
39+
when nil
40+
{}
41+
else
42+
raise ArgumentError, "Unexpected parameter: #{variables_param}"
43+
end
44+
end
45+
46+
def handle_error_in_development(e)
47+
logger.error e.message
48+
logger.error e.backtrace.join("\n")
49+
50+
render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500
51+
end
52+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
class GraphqlRubyIssueReproductionSchema < GraphQL::Schema
4+
mutation(Types::MutationType)
5+
query(Types::QueryType)
6+
7+
# For batch-loading (see https://graphql-ruby.org/dataloader/overview.html)
8+
use GraphQL::Dataloader
9+
10+
# GraphQL-Ruby calls this when something goes wrong while running a query:
11+
def self.type_error(err, context)
12+
# if err.is_a?(GraphQL::InvalidNullError)
13+
# # report to your bug tracker here
14+
# return nil
15+
# end
16+
super
17+
end
18+
19+
# Union and Interface Resolution
20+
def self.resolve_type(abstract_type, obj, ctx)
21+
# TODO: Implement this method
22+
# to return the correct GraphQL object type for `obj`
23+
raise(GraphQL::RequiredImplementationMissingError)
24+
end
25+
26+
# Limit the size of incoming queries:
27+
max_query_string_tokens(5000)
28+
29+
# Stop validating when it encounters this many errors:
30+
validate_max_errors(100)
31+
32+
# Relay-style Object Identification:
33+
34+
# Return a string UUID for `object`
35+
def self.id_from_object(object, type_definition, query_ctx)
36+
# For example, use Rails' GlobalID library (https://github.com/rails/globalid):
37+
object.to_gid_param
38+
end
39+
40+
# Given a string UUID, find the object
41+
def self.object_from_id(global_id, query_ctx)
42+
# For example, use Rails' GlobalID library (https://github.com/rails/globalid):
43+
GlobalID.find(global_id)
44+
end
45+
end

app/graphql/mutations/.keep

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
module Mutations
4+
class BaseMutation < GraphQL::Schema::RelayClassicMutation
5+
argument_class Types::BaseArgument
6+
field_class Types::BaseField
7+
input_object_class Types::BaseInputObject
8+
object_class Types::BaseObject
9+
end
10+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module Resolvers
4+
class BaseResolver < GraphQL::Schema::Resolver
5+
end
6+
end

app/graphql/types/.keep

Whitespace-only changes.

app/graphql/types/base_argument.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class BaseArgument < GraphQL::Schema::Argument
5+
end
6+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class BaseConnection < Types::BaseObject
5+
# add `nodes` and `pageInfo` fields, as well as `edge_type(...)` and `node_nullable(...)` overrides
6+
include GraphQL::Types::Relay::ConnectionBehaviors
7+
end
8+
end

app/graphql/types/base_edge.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class BaseEdge < Types::BaseObject
5+
# add `node` and `cursor` fields, as well as `node_type(...)` override
6+
include GraphQL::Types::Relay::EdgeBehaviors
7+
end
8+
end

app/graphql/types/base_enum.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class BaseEnum < GraphQL::Schema::Enum
5+
end
6+
end

0 commit comments

Comments
 (0)