Class: GraphQL::SchemaComparator::Diff::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema_comparator/diff/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(old_schema, new_schema) ⇒ Schema


5
6
7
8
9
10
11
12
13
14
# File 'lib/graphql/schema_comparator/diff/schema.rb', line 5

def initialize(old_schema, new_schema)
  @old_schema = old_schema
  @new_schema = new_schema

  @old_types = old_schema.types
  @new_types = new_schema.types

  @old_directives = old_schema.directives
  @new_directives = new_schema.directives
end

Instance Method Details

#changes_in_directivesObject


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/graphql/schema_comparator/diff/schema.rb', line 82

def changes_in_directives
  changes = []

  changes += removed_directives.map { |directive| Changes::DirectiveRemoved.new(directive) }
  changes += added_directives.map { |directive| Changes::DirectiveAdded.new(directive) }

  each_common_directive do |old_directive, new_directive|
    changes += Diff::Directive.new(old_directive, new_directive).diff
  end

  changes
end

#changes_in_schemaObject


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/graphql/schema_comparator/diff/schema.rb', line 64

def changes_in_schema
  changes = []

  if old_schema.query != new_schema.query
    changes << Changes::SchemaQueryTypeChanged.new(old_schema, new_schema)
  end

  if old_schema.mutation != new_schema.mutation
    changes << Changes::SchemaMutationTypeChanged.new(old_schema, new_schema)
  end

  if old_schema.subscription != new_schema.subscription
    changes << Changes::SchemaSubscriptionTypeChanged.new(old_schema, new_schema)
  end

  changes
end

#changes_in_type(old_type, new_type) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/graphql/schema_comparator/diff/schema.rb', line 37

def changes_in_type(old_type, new_type)
  changes = []

  if old_type.kind != new_type.kind
    changes << Changes::TypeKindChanged.new(old_type, new_type)
  else
    case old_type
    when GraphQL::EnumType
      changes += Diff::Enum.new(old_type, new_type).diff
    when GraphQL::UnionType
      changes += Diff::Union.new(old_type, new_type).diff
    when GraphQL::InputObjectType
      changes += Diff::InputObject.new(old_type, new_type).diff
    when GraphQL::ObjectType
      changes += Diff::ObjectType.new(old_type, new_type).diff
    when GraphQL::InterfaceType
      changes += Diff::Interface.new(old_type, new_type).diff
    end
  end

  if old_type.description != new_type.description
    changes << Changes::TypeDescriptionChanged.new(old_type, new_type)
  end

  changes
end

#diffObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graphql/schema_comparator/diff/schema.rb', line 16

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) }

  # Type Diff for common types
  each_common_type do |old_type, new_type|
    changes += changes_in_type(old_type, new_type)
  end

  # Diff Schemas
  changes += changes_in_schema

  # Diff Directives
  changes += changes_in_directives

  changes
end