lib/graph_ql/query.rb in graphql-0.1.0 vs lib/graph_ql/query.rb in graphql-0.2.0

- old
+ new

@@ -1,25 +1,23 @@ class GraphQL::Query + # If a resolve function returns `GraphQL::Query::DEFAULT_RESOLVE`, + # The executor will send the field's name to the target object + # and use the result. DEFAULT_RESOLVE = :__default_resolve - extend ActiveSupport::Autoload - autoload(:Arguments) - autoload(:FieldResolutionStrategy) - autoload(:FragmentSpreadResolutionStrategy) - autoload(:InlineFragmentResolutionStrategy) - autoload(:OperationResolver) - autoload(:SelectionResolver) - autoload(:TypeResolver) attr_reader :schema, :document, :context, :fragments, :params - def initialize(schema, query_string, context: nil, params: {}) + def initialize(schema, query_string, context: nil, params: {}, debug: true, validate: true) @schema = schema - @document = GraphQL.parse(query_string) + @debug = debug + @query_string = query_string @context = context @params = params + @validate = validate @fragments = {} @operations = {} + @document = GraphQL.parse(@query_string) @document.parts.each do |part| if part.is_a?(GraphQL::Nodes::FragmentDefinition) @fragments[part.name] = part elsif part.is_a?(GraphQL::Nodes::OperationDefinition) @operations[part.name] = part @@ -27,24 +25,49 @@ end end # Get the result for this query, executing it once def result + if validation_errors.any? + return { "errors" => validation_errors } + end + @result ||= { "data" => execute, } rescue StandardError => err - message = "Something went wrong during query execution: #{err}" - {"errors" => [{"message" => message}]} + if @debug + raise err + else + message = "Something went wrong during query execution: #{err}" # \n #{err.backtrace.join("\n ")}" + {"errors" => [{"message" => message}]} + end end private def execute - response = {} - @operations.each do |name, operation| + @operations.reduce({}) do |memo, (name, operation)| resolver = OperationResolver.new(operation, self) - response[name] = resolver.result + memo[name] = resolver.result + memo end - response end + + def validation_errors + @validation_errors ||= begin + if @validate + @schema.static_validator.validate(@document) + else + [] + end + end + end end + +require 'graph_ql/query/arguments' +require 'graph_ql/query/field_resolution_strategy' +require 'graph_ql/query/fragment_spread_resolution_strategy' +require 'graph_ql/query/inline_fragment_resolution_strategy' +require 'graph_ql/query/operation_resolver' +require 'graph_ql/query/selection_resolver' +require 'graph_ql/query/type_resolver'