Skip to content

Commit

Permalink
fix: remove parent_type.fields check in analyzer (#46)
Browse files Browse the repository at this point in the history
* fix: remove parent_type.fields check

* fix: update to v0.5.3

* fix: lint

* fix: update check

Co-authored-by: Arya Bhimani <[email protected]>

* fix: update test

* fix: lint

---------

Co-authored-by: Arya Bhimani <[email protected]>
  • Loading branch information
al-yanna and aryascripts authored Dec 6, 2024
1 parent 6b2b430 commit 980ea7d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 62 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.2)
graphql-hive (0.5.3)
graphql (>= 2.3, < 3)

GEM
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql-hive/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(query_or_multiplex)

def on_enter_field(node, _parent, visitor)
parent_type = visitor.parent_type_definition
if parent_type&.respond_to?(:graphql_name) && parent_type.fields[node.name]
if parent_type&.respond_to?(:graphql_name) && node&.respond_to?(:name)
@used_fields.add(parent_type.graphql_name)
@used_fields.add(make_id(parent_type.graphql_name, node.name))
end
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.2"
VERSION = "0.5.3"
end
end
167 changes: 108 additions & 59 deletions spec/graphql/graphql-hive/analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,75 @@
end

let(:schema) do
GraphQL::Schema.from_definition(%|
type Query {
project(selector: ProjectSelectorInput!): Project
projectsByType(type: ProjectType!): [Project!]!
projectsByManyTypes(type: [ProjectType!]!): [Project!]!
projects(filter: FilterInput): [Project!]!
}
GraphQL::Schema.from_definition(
<<~GQL
type Query {
project(selector: ProjectSelectorInput!): Project
projectsByType(type: ProjectType!): [Project!]!
projectsByManyTypes(type: [ProjectType!]!): [Project!]!
projects(filter: FilterInput): [Project!]!
searchResult(query: String!): SearchResult
}
type Mutation {
deleteProject(selector: ProjectSelectorInput!): DeleteProjectPayload!
}
type Mutation {
deleteProject(selector: ProjectSelectorInput!): DeleteProjectPayload!
}
input ProjectSelectorInput {
organization: ID!
project: ID!
}
input ProjectSelectorInput {
organization: ID!
project: ID!
}
input FilterInput {
type: ProjectType
pagination: PaginationInput
order: [ProjectOrderByInput!]
}
input FilterInput {
type: ProjectType
pagination: PaginationInput
order: [ProjectOrderByInput!]
}
input PaginationInput {
limit: Int
offset: Int
}
input PaginationInput {
limit: Int
offset: Int
}
input ProjectOrderByInput {
field: String!
direction: OrderDirection
}
input ProjectOrderByInput {
field: String!
direction: OrderDirection
}
enum OrderDirection {
ASC
DESC
}
enum OrderDirection {
ASC
DESC
}
type ProjectSelector {
organization: ID!
project: ID!
}
type ProjectSelector {
organization: ID!
project: ID!
}
type DeleteProjectPayload {
selector: ProjectSelector!
deletedProject: Project!
}
type DeleteProjectPayload {
selector: ProjectSelector!
deletedProject: Project!
}
type Project {
id: ID!
cleanId: ID!
name: String!
type: ProjectType!
buildUrl: String
validationUrl: String
}
type Project {
id: ID!
cleanId: ID!
name: String!
type: ProjectType!
buildUrl: String
validationUrl: String
}
enum ProjectType {
FEDERATION
STITCHING
SINGLE
CUSTOM
}
|)
enum ProjectType {
FEDERATION
STITCHING
SINGLE
CUSTOM
}
union SearchResult = Project | ProjectSelector
GQL
)
end

let(:query_string) do
Expand Down Expand Up @@ -350,11 +355,51 @@
end
end

context "with a query containing a union type" do
let(:query_string) do
<<~GQL
query searchProjects {
searchResult(query: $query) {
__typename
... on Project {
id
}
... on ProjectSelector {
organization
}
}
}
GQL
end

it "collects all valid fields from union types" do
expect(used_fields).to contain_exactly(
"Query",
"Query.searchResult",
"Query.searchResult.query",
"SearchResult",
"SearchResult.__typename",
"Project",
"Project.id",
"ProjectSelector",
"ProjectSelector.organization",
"String"
)
end
end

context "with an invalid field" do
let(:query_string) do
<<~GQL
query getGatewayProjects {
projectsByManyTypes(type: [STITCHING]),
searchResult(query: $query) {
nonExistentField {
subField
}
... on Project {
id
}
}
nonExistentField {
subField
}
Expand All @@ -365,10 +410,14 @@
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"
"Query.searchResult",
"Query.searchResult.query",
"SearchResult",
"SearchResult.nonExistentField",
"Project",
"Project.id",
"Query.nonExistentField",
"String"
)
end
end
Expand Down

0 comments on commit 980ea7d

Please sign in to comment.