Module: GraphQL::Compatibility::ExecutionSpecification::SpecificationSchema

Defined in:
lib/graphql/compatibility/execution_specification/specification_schema.rb

Defined Under Namespace

Modules: TestMiddleware Classes: CustomCollection

Constant Summary

BOGUS_NODE =
OpenStruct.new({ bogus: true })
DATA =
{
  "1001" => OpenStruct.new({
    name: "Fannie Lou Hamer",
    birthdate: Time.new(1917, 10, 6),
    organization_ids: [],
  }),
  "1002" => OpenStruct.new({
    name: "John Lewis",
    birthdate: Time.new(1940, 2, 21),
    organization_ids: ["2001"],
  }),
  "1003" => OpenStruct.new({
    name: "Diane Nash",
    birthdate: Time.new(1938, 5, 15),
    organization_ids: ["2001", "2002"],
  }),
  "1004" => OpenStruct.new({
    name: "Ralph Abernathy",
    birthdate: Time.new(1926, 3, 11),
    organization_ids: ["2002"],
  }),
  "2001" => OpenStruct.new({
    name: "SNCC",
    leader_id: nil, # fail on purpose
  }),
  "2002" => OpenStruct.new({
    name: "SCLC",
    leader_id: "1004",
  }),
  "2003" => BOGUS_NODE,
}

Class Method Summary collapse

Class Method Details

.build(execution_strategy) ⇒ Object



59
60
61
62
63
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/graphql/compatibility/execution_specification/specification_schema.rb', line 59

def self.build(execution_strategy)
  organization_type = nil

  timestamp_type = GraphQL::ScalarType.define do
    name "Timestamp"
    coerce_input ->(value, _ctx) { Time.at(value.to_i) }
    coerce_result ->(value, _ctx) { value.to_i }
  end

  named_entity_interface_type = GraphQL::InterfaceType.define do
    name "NamedEntity"
    field :name, !types.String
  end

  person_type = GraphQL::ObjectType.define do
    name "Person"
    interfaces [named_entity_interface_type]
    field :name, !types.String
    field :birthdate, timestamp_type
    field :age, types.Int do
      argument :on, !timestamp_type
      resolve ->(obj, args, ctx) {
        if obj.birthdate.nil?
          nil
        else
          age_on = args[:on]
          age_years = age_on.year - obj.birthdate.year
          this_year_birthday = Time.new(age_on.year, obj.birthdate.month, obj.birthdate.day)
          if this_year_birthday > age_on
            age_years -= 1
          end
        end
        age_years
      }
    end
    field :organizations, types[organization_type] do
      resolve ->(obj, args, ctx) {
        CustomCollection.new(obj.organization_ids.map { |id| DATA[id] })
      }
    end
    field :first_organization, !organization_type do
      resolve ->(obj, args, ctx) {
        DATA[obj.organization_ids.first]
      }
    end
  end

  organization_type = GraphQL::ObjectType.define do
    name "Organization"
    interfaces [named_entity_interface_type]
    field :name, !types.String
    field :leader, !person_type do
      resolve ->(obj, args, ctx) {
        DATA[obj.leader_id] || (ctx[:return_error] ? ExecutionError.new("Error on Nullable") : nil)
      }
    end
    field :returnedError, types.String do
      resolve ->(o, a, c) {
        GraphQL::ExecutionError.new("This error was returned")
      }
    end
    field :raisedError, types.String do
      resolve ->(o, a, c) {
        raise GraphQL::ExecutionError.new("This error was raised")
      }
    end

    field :nodePresence, !types[!types.Boolean] do
      resolve ->(o, a, ctx) {
        [
          ctx.irep_node.is_a?(GraphQL::InternalRepresentation::Node),
          ctx.ast_node.is_a?(GraphQL::Language::Nodes::AbstractNode),
          false, # just testing
        ]
      }
    end
  end

  node_union_type = GraphQL::UnionType.define do
    name "Node"
    possible_types [person_type, organization_type]
  end

  query_type = GraphQL::ObjectType.define do
    name "Query"
    field :node, node_union_type do
      argument :id, !types.ID
      resolve ->(obj, args, ctx) {
        obj[args[:id]]
      }
    end

    field :requiredNode, node_union_type.to_non_null_type do
      argument :id, !types.ID
      resolve ->(obj, args, ctx) {
        obj[args[:id]]
      }
    end

    field :organization, !organization_type do
      argument :id, !types.ID
      resolve ->(obj, args, ctx) {
        if args[:id].start_with?("2")
          obj[args[:id]]
        else
          # test context.skip
          ctx.skip
        end
      }
    end

    field :organizations, types[organization_type] do
      resolve ->(obj, args, ctx) {
        [obj["2001"], obj["2002"]]
      }
    end
  end

  GraphQL::Schema.define do
    query_execution_strategy execution_strategy
    query query_type

    resolve_type ->(type, obj, ctx) {
      if obj.respond_to?(:birthdate)
        person_type
      elsif obj.respond_to?(:leader_id)
        organization_type
      else
        nil
      end
    }

    type_error ->(err, ctx) {
      ctx[:type_errors] && (ctx[:type_errors] << err.value)
      ctx[:gobble] || GraphQL::Schema::DefaultTypeError.call(err, ctx)
    }
    middleware(TestMiddleware)
  end
end