Skip to content

Commit

Permalink
fix: exclude invalid fields in analyzer (#44)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
al-yanna authored Dec 3, 2024
1 parent 938e7ce commit 6b2b430
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
graphql-hive (0.5.1)
graphql-hive (0.5.2)
graphql (>= 2.3, < 3)

GEM
Expand Down
7 changes: 5 additions & 2 deletions lib/graphql-hive/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql-hive/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Graphql
module Hive
VERSION = "0.5.1"
VERSION = "0.5.2"
end
end
23 changes: 23 additions & 0 deletions spec/graphql/graphql-hive/analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6b2b430

Please sign in to comment.