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

fix: exclude invalid fields in analyzer #44

Merged
merged 7 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 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
4 changes: 4 additions & 0 deletions lib/graphql-hive/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def send(path, body, _log_type)
request = build_request(uri, body)
response = http.request(request)

if response.code.to_i >= 400 && response.code.to_i < 500
@options[:logger].warn("Unsuccessful response: #{response.code} - #{response.body}")
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this get added to this PR? Are you certain response.body is safe to log without exposing tokens?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its mostly for debugging purposes. to be safe, we can parse and log specific fields instead of the entire body. i'll remove this for now and address it in a separate PR!


@options[:logger].debug(response.inspect)
@options[:logger].debug(response.body.inspect)
rescue => e
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
Loading