Class: Graphiti::OpenAPI::Resource

Inherits:
ResourceData show all
Includes:
Parameters
Defined in:
app/models/graphiti/open_api/resource.rb

Instance Method Summary collapse

Methods included from Parameters

#array_enum, #parameter, #path_parameter, #query_parameter

Methods inherited from ResourceData

#relationships, #relationships?

Instance Method Details

#all_attributesObject



52
53
54
# File 'app/models/graphiti/open_api/resource.rb', line 52

def all_attributes
  attributes.merge(extra_attributes)
end

#attribute_schemasObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/models/graphiti/open_api/resource.rb', line 237

def attribute_schemas
  types = {
    "#{type}_readable_attribute" => {
      description: "#{human} readable attributes",
      type: :string,
      enum: readable_attributes.map(&:name),
    },
    "#{type}_sortable_attributes_list" => {
      description: "#{human} sortable attributes",
      type: :array,
      items: {
        type: :string,
        enum: sortable_attribute_names.map { |name| %W[#{name} -#{name}] }.flatten,
      },
      uniqueItems: true,
    },
    "#{type}_related" => {
      description: "#{human} relationships available for inclusion",
      type: :array,
      items: {type: :string},
      uniqueItems: true,
    },

  }
  if relationship_names.any?
    types["#{type}_related"][:items][:enum] = relationship_names
  else
    types["#{type}_related"][:nullable] = true
  end
  types
end

#attributesObject



44
45
46
# File 'app/models/graphiti/open_api/resource.rb', line 44

def attributes
  Attributes.load(self)
end

#attributes_schemaObject



134
135
136
137
138
139
140
141
142
143
# File 'app/models/graphiti/open_api/resource.rb', line 134

def attributes_schema
  {
    type => {
      type: :object,
      description: "#{human} attributes",
      properties: resource_attributes.map(&:to_property).inject(&:merge),
      additionalProperties: false,
    },
  }
end

#extra_attributesObject



48
49
50
# File 'app/models/graphiti/open_api/resource.rb', line 48

def extra_attributes
  Attributes.load(self, __attributes__[:extra_attributes])
end

#model_nameObject



34
35
36
# File 'app/models/graphiti/open_api/resource.rb', line 34

def model_name
  ActiveModel::Name.new(self.class, nil, name.gsub(/Resource/, ""))
end

#plural_human(**options) ⇒ Object



40
41
42
# File 'app/models/graphiti/open_api/resource.rb', line 40

def plural_human(**options)
  human(**options).pluralize
end

#query_fields_parameterObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/graphiti/open_api/resource.rb', line 80

def query_fields_parameter
  schema = {
    description: "#{human} readable attributes list",
    type: :array,
    items: {'$ref': "#/components/schemas/#{type}_readable_attribute"},
    uniqueItems: true,
  }

  query_parameter("fields[#{type}]",
                  desc: "[Include only specified fields of #{human} in response](https://jsonapi.org/format/#fetching-sparse-fieldsets)",
                  schema: schema,
                  explode: false)
end

#query_include_parameterObject



94
95
96
97
98
# File 'app/models/graphiti/open_api/resource.rb', line 94

def query_include_parameter
  return unless relationships?

  query_parameter(:include, desc: "[Include related resources](https://jsonapi.org/format/#fetching-includes)", schema: {'$ref': "#/components/schemas/#{type}_related"}, explode: false)
end

#query_parametersObject



73
74
75
76
77
78
# File 'app/models/graphiti/open_api/resource.rb', line 73

def query_parameters
  [].tap do |result|
    result << query_include_parameter
    result << query_fields_parameter
  end
end

#query_sort_parameter(relationships: false) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/graphiti/open_api/resource.rb', line 100

def query_sort_parameter(relationships: false)
  return unless sorts.any?
  orderings = sorts.keys.map { |id| %W[#{id} -#{id}] }.flatten
  if relationships
    relationships.each do |name, relationship|
      relationship.resources.each do |resource|
        orderings += resource.sorts.keys.map { |id| %W[#{name}.#{id} -#{name}.#{id}] }.flatten
      end
    end
  end
  query_parameter(:sort,
                  desc: "[Sort #{model_name.plural} according to one or more criteria](https://jsonapi.org/format/#fetching-sorting)\n\n" \
                        "You should not include both ascending `id` and descending `-id` fields the same time\n\n",
                  schema: {"$ref" => "#/components/schemas/#{type}_sortable_attributes_list"}, explode: false)
end

#readable_attributesObject



60
61
62
# File 'app/models/graphiti/open_api/resource.rb', line 60

def readable_attributes
  all_attributes.values.select(&:readable)
end

#relationships_schemaObject



145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/graphiti/open_api/resource.rb', line 145

def relationships_schema
  schema_name = "#{type}_relationships"
  return {schema_name => {'$ref': "#/components/schemas/jsonapi_relationships"}} unless relationships?
  {
    schema_name => {
      type: :object,
      properties: relationships.values.map(&:to_schema).inject(&:merge),
      additionalProperties: false,
    },
  }
end

#request_schemaObject



225
226
227
228
229
230
231
232
233
234
235
# File 'app/models/graphiti/open_api/resource.rb', line 225

def request_schema
  {
    "#{type}_request" => {
      type: :object,
      properties: {
        data: {'$ref': "#/components/schemas/#{type}_resource"},
      },
      # xml: {name: :data},
    },
  }
end

#resource_attributesObject



56
57
58
# File 'app/models/graphiti/open_api/resource.rb', line 56

def resource_attributes
  all_attributes.except(:id).values
end

#resource_schemaObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/models/graphiti/open_api/resource.rb', line 157

def resource_schema
  {
    "#{type}_resource" => {
      type: :object,
      properties: {
        id: {type: :string, example: rand(100).to_s},
        type: {type: :string, enum: [type]},
        attributes: {'$ref': "#/components/schemas/#{type}"},
        relationships: {'$ref': "#/components/schemas/#{type}_relationships"},
        links: {'$ref': "#/components/schemas/jsonapi_links"},
      },
      additionalProperties: false,
    },
  }
end

#response_schemaObject



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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/models/graphiti/open_api/resource.rb', line 173

def response_schema
  {
    "#{type}_single" => {
      type: :object,
      properties: {
        data: {'$ref': "#/components/schemas/#{type}_resource"},
        included: {
          description: "To reduce the number of HTTP requests, servers **MAY** allow responses that include related resources along with the requested primary resources. Such responses are called \"compound documents\".",
          type: "array",
          items: {'$ref': "#/components/schemas/jsonapi_resource"},
          uniqueItems: true
        },
        meta: {'$ref': "#/components/schemas/jsonapi_meta"},
        links: {
          description: "Link members related to the primary data.",
          allOf: [
            {"$ref": "#/components/schemas/jsonapi_links"},
            {"$ref": "#/components/schemas/jsonapi_pagination"},
          ]
        },
        "jsonapi": {"$ref": "#/components/schemas/jsonapi_jsonapi"},
      },
      additionalProperties: false
    },
    "#{type}_collection" => {
      type: :object,
      properties: {
        data: {
          type: "array",
          items: {'$ref': "#/components/schemas/#{type}_resource"},
        },
        included: {
          description: "To reduce the number of HTTP requests, servers **MAY** allow responses that include related resources along with the requested primary resources. Such responses are called \"compound documents\".",
          type: "array",
          items: {'$ref': "#/components/schemas/jsonapi_resource"},
          uniqueItems: true
        },
        meta: {'$ref': "#/components/schemas/jsonapi_meta"},
        links: {
          description: "Link members related to the primary data.",
          allOf: [
            {"$ref": "#/components/schemas/jsonapi_links"},
            {"$ref": "#/components/schemas/jsonapi_pagination"},
          ]
        },
        "jsonapi": {"$ref": "#/components/schemas/jsonapi_jsonapi"},
      },
      additionalProperties: false
    },
  }
end

#sortable_attributesObject



64
65
66
67
# File 'app/models/graphiti/open_api/resource.rb', line 64

def sortable_attributes
  sortable_attribute_names = sorts.keys
  all_attributes.values.select { |attribute| sortable_attribute_names.include?(attribute.name) }
end


297
298
299
300
301
302
303
304
305
306
307
# File 'app/models/graphiti/open_api/resource.rb', line 297

def to_links
  %i[get update delete].inject({}) do |result, method|
    operation_id = "#{method}_#{model_name.singular}".camelize(:lower)
    result.merge(
      "#{operation_id}Id": {
        operationId: operation_id,
        parameters: {id: "$response.body#/data/id"},
      },
    )
  end
end

#to_parametersObject



116
117
118
119
120
121
122
123
# File 'app/models/graphiti/open_api/resource.rb', line 116

def to_parameters
  {
    "#{type}_id": path_parameter(:id, schema: {type: :string}, desc: "ID of the resource"),
    "#{type}_include": query_include_parameter,
    "#{type}_fields": query_fields_parameter,
    "#{type}_sort": query_sort_parameter,
  }.keep_if { |name, value| value }
end

#to_responsesObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'app/models/graphiti/open_api/resource.rb', line 269

def to_responses
  {
    "#{type}_200" => {
      description: "OK: #{human} resource",
      content: {
        "application/vnd.api+json" => {schema: {"$ref": "#/components/schemas/#{type}_single"}},
        # "application/xml" => {schema: {"$ref": "#/components/schemas/#{type}_single"}},
      },
      links: link_refs,
    },
    "#{type}_200_collection" => {
      description: "OK: #{plural_human} collection",
      content: {
        "application/vnd.api+json" => {schema: {"$ref": "#/components/schemas/#{type}_collection"}},
        # "application/xml" => {schema: {"$ref": "#/components/schemas/#{type}_collection"}},
      },
    },
    "#{type}_201" => {
      description: "Created",
      content: {
        "application/vnd.api+json" => {schema: {"$ref": "#/components/schemas/#{type}_single"}},
        # "application/xml" => {schema: {"$ref": "#/components/schemas/#{type}_single"}},
      },
      links: link_refs,
    },
  }
end

#to_schemaObject



125
126
127
128
129
130
131
132
# File 'app/models/graphiti/open_api/resource.rb', line 125

def to_schema
  attributes_schema
    .merge(relationships_schema)
    .merge(resource_schema)
    .merge(response_schema)
    .merge(request_schema)
    .merge(attribute_schemas)
end

#writable_attributesObject



69
70
71
# File 'app/models/graphiti/open_api/resource.rb', line 69

def writable_attributes
  all_attributes.values.select(&:writable)
end