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

Detect new interface implementation for new types #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions lib/graphql/schema_comparator/diff/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def diff
changes = []

# Removed and Added Types
changes += removed_types.map { |type| Changes::TypeRemoved.new(type) }
changes += added_types.map { |type| Changes::TypeAdded.new(type) }
removed_types.each { |type| changes += changes_in_removed_type(type) }
added_types.each { |type| changes += changes_in_added_type(type) }

# Type Diff for common types
each_common_type do |old_type, new_type|
Expand All @@ -34,6 +34,22 @@ def diff
changes
end

def changes_in_added_type(type)
changes = [Changes::TypeAdded.new(type)]
if type.graphql_definition.is_a?(GraphQL::ObjectType)
type.interfaces.each do |interface|
if old_types[interface.graphql_name]
changes << Changes::ObjectTypeInterfaceAdded.new(interface, type)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess ideally this would only be dangerous for existing types right? It's good to capture this change for new types, but it's not actually dangerous at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly. It's only dangerous when implementing an existing interface.

end
end
end
changes
end

def changes_in_removed_type(type)
[Changes::TypeRemoved.new(type)]
end

def changes_in_type(old_type, new_type)
changes = []

Expand Down
17 changes: 15 additions & 2 deletions test/lib/graphql/schema_comparator/diff/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ def setup
type WithInterfaces implements AnInterface {
a: String!
}
interface ANewInterface {
c: Int
}
type AnotherWithInterfaces implements AnInterface, ANewInterface {
interfaceField: Int!
c: Int
}
type WithArguments {
a(
# Description for a
Expand Down Expand Up @@ -175,6 +182,9 @@ def test_changes_kitchensink
"Field `b` was added to object type `AnotherInterface`",
"`WithInterfaces` object type no longer implements `AnotherInterface` interface",
"Field `anotherInterfaceField` was removed from object type `WithInterfaces`",
"Type `ANewInterface` was added",
"Type `AnotherWithInterfaces` was added",
"`AnotherWithInterfaces` object implements `AnInterface` interface",
"Description for argument `a` on field `WithArguments.a` changed from `Meh` to `Description for a`",
"Type for argument `b` on field `WithArguments.a` changed from `String` to `String!`",
"Default value for argument `arg` on field `WithArguments.b` changed from `1` to `2`",
Expand All @@ -192,7 +202,7 @@ def test_changes_kitchensink
"Argument `willBeRemoved` was removed from directive `yolo`",
"Description for argument `someArg` on directive `yolo` changed from `Included when true.` to `someArg does stuff`",
"Type for argument `someArg` on directive `yolo` changed from `Boolean!` to `String!`",
].sort, @differ.diff.map(&:message).sort
].sort.join("\n"), @differ.diff.map(&:message).sort.join("\n")

assert_equal [
"WillBeRemoved",
Expand Down Expand Up @@ -220,6 +230,9 @@ def test_changes_kitchensink
"AnotherInterface.b",
"WithInterfaces",
"WithInterfaces.anotherInterfaceField",
"ANewInterface",
"AnotherWithInterfaces",
"AnotherWithInterfaces",
"WithArguments.a.a",
"WithArguments.a.b",
"WithArguments.b.arg",
Expand All @@ -238,6 +251,6 @@ def test_changes_kitchensink
"@yolo.someArg",
"@yolo.someArg",
"@yolo.anotherArg",
].sort, @differ.diff.map(&:path).sort
].sort.join("\n"), @differ.diff.map(&:path).sort.join("\n")
end
end