diff --git a/lib/graphql/stitching/client.rb b/lib/graphql/stitching/client.rb index c9c4de25..b83f89e5 100644 --- a/lib/graphql/stitching/client.rb +++ b/lib/graphql/stitching/client.rb @@ -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 diff --git a/lib/graphql/stitching/request.rb b/lib/graphql/stitching/request.rb index ffc9ebcc..e9d3d962 100644 --- a/lib/graphql/stitching/request.rb +++ b/lib/graphql/stitching/request.rb @@ -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? diff --git a/test/graphql/stitching/request_test.rb b/test/graphql/stitching/request_test.rb index 26664f47..4f076e6a 100644 --- a/test/graphql/stitching/request_test.rb +++ b/test/graphql/stitching/request_test.rb @@ -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 @@ -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 } }")