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

Returns a new instance of 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



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/graphql/schema_comparator/diff/schema.rb', line 100

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/graphql/schema_comparator/diff/schema.rb', line 64

def changes_in_schema
  changes = []

  if old_schema.query&.graphql_name != new_schema.query&.graphql_name
    if old_schema.query.nil?
      changes << Changes::RootOperationTypeAdded.new(new_schema: new_schema, operation_type: :query)
    elsif new_schema.query.nil?
      changes << Changes::RootOperationTypeRemoved.new(old_schema: old_schema, operation_type: :query)
    else
      changes << Changes::RootOperationTypeChanged.new(old_schema: old_schema, new_schema: new_schema, operation_type: :query)
    end
  end

  if old_schema.mutation&.graphql_name != new_schema.mutation&.graphql_name
    if old_schema.mutation.nil?
      changes << Changes::RootOperationTypeAdded.new(new_schema: new_schema, operation_type: :mutation)
    elsif new_schema.mutation.nil?
      changes << Changes::RootOperationTypeRemoved.new(old_schema: old_schema, operation_type: :mutation)
    else
      changes << Changes::RootOperationTypeChanged.new(old_schema: old_schema, new_schema: new_schema, operation_type: :mutation)
    end
  end

  if old_schema.subscription&.graphql_name != new_schema.subscription&.graphql_name
    if old_schema.subscription.nil?
      changes << Changes::RootOperationTypeAdded.new(new_schema: new_schema, operation_type: :subscription)
    elsif new_schema.subscription.nil?
      changes << Changes::RootOperationTypeRemoved.new(old_schema: old_schema, operation_type: :subscription)
    else
      changes << Changes::RootOperationTypeChanged.new(old_schema: old_schema, new_schema: new_schema, operation_type: :subscription)
    end
  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.kind.name
    when "ENUM"
      changes += Diff::Enum.new(old_type, new_type, enum_usage(new_type)).diff
    when "UNION"
      changes += Diff::Union.new(old_type, new_type).diff
    when "INPUT_OBJECT"
      changes += Diff::InputObject.new(old_type, new_type).diff
    when "OBJECT"
      changes += Diff::ObjectType.new(old_type, new_type).diff
    when "INTERFACE"
      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