Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shift validation to Request object #109

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/graphql/stitching/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def execute(query:, variables: nil, operation_name: nil, context: nil, validate:
)

if validate
validation_errors = @supergraph.schema.validate(request.document)
validation_errors = request.validate
return error_result(validation_errors) if validation_errors.any?
end

Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/stitching/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def fragment_definitions
end
end

def validate
@supergraph.schema.validate(@document, context: @context)
end

def prepare!
operation.variables.each do |v|
@variables[v.name] = v.default_value if @variables[v.name].nil? && !v.default_value.nil?
Expand Down
16 changes: 15 additions & 1 deletion test/graphql/stitching/request_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# frozen_string_literal: true

require "test_helper"
require_relative "../../schemas/example"

describe "GraphQL::Stitching::Request" do
def setup
@supergraph = {}
@supergraph = GraphQL::Stitching::Supergraph.new(
schema: Schemas::Example::Products,
fields: {},
boundaries: {},
executables: {},
)
end

def test_builds_with_pre_parsed_ast
Expand Down Expand Up @@ -226,6 +232,14 @@ def test_applies_skip_and_include_directives_via_variables
assert_equal expected, squish_string(request.document.to_query_string)
end

def test_validates_the_request
request1 = GraphQL::Stitching::Request.new(@supergraph, %|{ product(upc: "1") { upc} }|)
assert request1.validate.none?

request2 = GraphQL::Stitching::Request.new(@supergraph, %|{ invalidSelection }|)
assert_equal 1, request2.validate.length
end

def test_assigns_a_plan_for_the_request
plan = GraphQL::Stitching::Plan.new(ops: [])
request = GraphQL::Stitching::Request.new(@supergraph, "{ widget { id } }")
Expand Down