Class: Graphiti::Renderer

Inherits:
Object show all
Defined in:
lib/graphiti/renderer.rb

Constant Summary collapse

CONTENT_TYPE =
"application/vnd.api+json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy, options) ⇒ Renderer

Returns a new instance of Renderer.



7
8
9
10
# File 'lib/graphiti/renderer.rb', line 7

def initialize(proxy, options)
  @proxy = proxy
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/graphiti/renderer.rb', line 5

def options
  @options
end

#proxyObject (readonly)

Returns the value of attribute proxy.



5
6
7
# File 'lib/graphiti/renderer.rb', line 5

def proxy
  @proxy
end

Class Method Details

.graphql_renderer(proxy) ⇒ Object



53
54
55
56
# File 'lib/graphiti/renderer.rb', line 53

def self.graphql_renderer(proxy)
  implementation = Graphiti::HashRenderer.new(proxy.resource, graphql: true)
  JSONAPI::Serializable::Renderer.new(implementation)
end

.hash_renderer(proxy) ⇒ Object



48
49
50
51
# File 'lib/graphiti/renderer.rb', line 48

def self.hash_renderer(proxy)
  implementation = Graphiti::HashRenderer.new(proxy.resource)
  JSONAPI::Serializable::Renderer.new(implementation)
end

.jsonapi_rendererObject



43
44
45
46
# File 'lib/graphiti/renderer.rb', line 43

def self.jsonapi_renderer
  @jsonapi_renderer ||= JSONAPI::Serializable::Renderer
    .new(JSONAPI::Renderer.new)
end

Instance Method Details

#as_graphqlObject



23
24
25
# File 'lib/graphiti/renderer.rb', line 23

def as_graphql
  render(self.class.graphql_renderer(@proxy))
end

#as_jsonObject



35
36
37
# File 'lib/graphiti/renderer.rb', line 35

def as_json
  render(self.class.hash_renderer(@proxy))
end

#recordsObject



12
13
14
# File 'lib/graphiti/renderer.rb', line 12

def records
  @records ||= @proxy.data
end

#to_graphqlObject



27
28
29
# File 'lib/graphiti/renderer.rb', line 27

def to_graphql
  as_graphql.to_json
end

#to_jsonObject



31
32
33
# File 'lib/graphiti/renderer.rb', line 31

def to_json
  as_json.to_json
end

#to_jsonapiObject

JSON:API requires the key even when the include turns up nothing.



17
18
19
20
21
# File 'lib/graphiti/renderer.rb', line 17

def to_jsonapi
  rendered = render(self.class.jsonapi_renderer)
  rendered[:included] ||= [] if proxy.query.includes_requested?
  rendered.to_json
end

#to_xmlObject



39
40
41
# File 'lib/graphiti/renderer.rb', line 39

def to_xml
  render(self.class.hash_renderer(@proxy)).to_xml(root: :data)
end