From 6b2b430ce1cdf1e3382c5282301622d03a70e344 Mon Sep 17 00:00:00 2001 From: Alyanna Santos <72548456+al-yanna@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:02:44 -0500 Subject: [PATCH] fix: exclude invalid fields in analyzer (#44) * fix: exclude invalid fields in analyzer * fix: lint * fix: update test * fix: lint * fix: log warning on unsuccesful response * fix: only log client-level errors * fix: remove log --- Gemfile.lock | 2 +- lib/graphql-hive/analyzer.rb | 7 +++++-- lib/graphql-hive/version.rb | 2 +- spec/graphql/graphql-hive/analyzer_spec.rb | 23 ++++++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index e220e00..1385083 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - graphql-hive (0.5.1) + graphql-hive (0.5.2) graphql (>= 2.3, < 3) GEM diff --git a/lib/graphql-hive/analyzer.rb b/lib/graphql-hive/analyzer.rb index 36df1ba..0472d44 100644 --- a/lib/graphql-hive/analyzer.rb +++ b/lib/graphql-hive/analyzer.rb @@ -10,8 +10,11 @@ def initialize(query_or_multiplex) end def on_enter_field(node, _parent, visitor) - @used_fields.add(visitor.parent_type_definition.graphql_name) - @used_fields.add(make_id(visitor.parent_type_definition.graphql_name, node.name)) + parent_type = visitor.parent_type_definition + if parent_type&.respond_to?(:graphql_name) && parent_type.fields[node.name] + @used_fields.add(parent_type.graphql_name) + @used_fields.add(make_id(parent_type.graphql_name, node.name)) + end end # Visitor also calls 'on_enter_argument' when visiting input object fields in arguments diff --git a/lib/graphql-hive/version.rb b/lib/graphql-hive/version.rb index 8b2d4b9..bcfc611 100644 --- a/lib/graphql-hive/version.rb +++ b/lib/graphql-hive/version.rb @@ -2,6 +2,6 @@ module Graphql module Hive - VERSION = "0.5.1" + VERSION = "0.5.2" end end diff --git a/spec/graphql/graphql-hive/analyzer_spec.rb b/spec/graphql/graphql-hive/analyzer_spec.rb index d0778e0..16b6022 100644 --- a/spec/graphql/graphql-hive/analyzer_spec.rb +++ b/spec/graphql/graphql-hive/analyzer_spec.rb @@ -349,4 +349,27 @@ ) end end + + context "with an invalid field" do + let(:query_string) do + <<~GQL + query getGatewayProjects { + projectsByManyTypes(type: [STITCHING]), + nonExistentField { + subField + } + } + GQL + end + + it "collects all valid fields and excludes invalid fields" do + expect(used_fields).to contain_exactly( + "Query", + "Query.projectsByManyTypes", + "ProjectType", + "Query.projectsByManyTypes.type", + "ProjectType.STITCHING" + ) + end + end end