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: remove parent_type.fields check in analyzer #46

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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.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
Loading