@@ -74,19 +74,22 @@ def to_hash(fields: nil, include: {}, name_chain: [], graphql: false)
7474 name_chain << k unless name_chain . last == k
7575
7676 unless remote_resource? && serializers . nil?
77- attrs [ name . to_sym ] = if serializers . is_a? ( Array )
78- serializers . map do |rr |
77+ payload = if serializers . is_a? ( Array )
78+ data = serializers . map { |rr |
7979 rr . to_hash ( fields : fields , include : nested_include , graphql : graphql , name_chain : name_chain )
80- end
80+ }
81+ graphql ? { nodes : data } : data
8182 elsif serializers . nil?
8283 if @resource . class . respond_to? ( :sideload )
8384 if @resource . class . sideload ( k ) . type . to_s . include? ( "_many" )
84- [ ]
85+ graphql ? { nodes : [ ] } : [ ]
8586 end
8687 end
8788 else
8889 serializers . to_hash ( fields : fields , include : nested_include , graphql : graphql , name_chain : name_chain )
8990 end
91+
92+ attrs [ name . to_sym ] = payload
9093 end
9194 end
9295
@@ -133,29 +136,58 @@ def render(options)
133136 serializers = options [ :data ]
134137 opts = options . slice ( :fields , :include )
135138 opts [ :graphql ] = @graphql
136- to_hash ( serializers , opts ) . tap do |hash |
137- hash . merge! ( options . slice ( :meta ) ) unless options [ :meta ] . empty?
138- end
139+ top_level_key = get_top_level_key ( @resource , serializers . is_a? ( Array ) )
140+
141+ hash = { top_level_key => { } }
142+ nodes = get_nodes ( serializers , opts )
143+ add_nodes ( hash , top_level_key , options , nodes , @graphql )
144+ add_stats ( hash , top_level_key , options , @graphql )
145+ hash
139146 end
140147
141148 private
142149
143- def to_hash ( serializers , opts )
144- { } . tap do |hash |
145- top_level_key = :data
146- if @graphql
147- top_level_key = @resource . graphql_entrypoint
148- unless serializers . is_a? ( Array )
149- top_level_key = top_level_key . to_s . singularize . to_sym
150- end
150+ def get_top_level_key ( resource , is_many )
151+ key = :data
152+
153+ if @graphql
154+ key = @resource . graphql_entrypoint
155+ key = key . to_s . singularize . to_sym unless is_many
156+ end
157+
158+ key
159+ end
160+
161+ def get_nodes ( serializers , opts )
162+ if serializers . is_a? ( Array )
163+ serializers . map do |s |
164+ s . to_hash ( **opts )
151165 end
166+ else
167+ serializers . to_hash ( **opts )
168+ end
169+ end
170+
171+ def add_nodes ( hash , top_level_key , opts , nodes , graphql )
172+ payload = nodes
173+ if graphql && nodes . is_a? ( Array )
174+ payload = { nodes : nodes }
175+ end
152176
153- hash [ top_level_key ] = if serializers . is_a? ( Array )
154- serializers . map do |s |
155- s . to_hash ( **opts )
177+ # Don't render nodes if we only requested stats
178+ unless graphql && opts [ :fields ] . values == [ [ :stats ] ]
179+ hash [ top_level_key ] = payload
180+ end
181+ end
182+
183+ def add_stats ( hash , top_level_key , options , graphql )
184+ if options [ :meta ] && !options [ :meta ] . empty?
185+ if @graphql
186+ if ( stats = options [ :meta ] [ :stats ] )
187+ hash [ top_level_key ] [ :stats ] = stats
156188 end
157189 else
158- serializers . to_hash ( ** opts )
190+ hash . merge! ( options . slice ( :meta ) )
159191 end
160192 end
161193 end
0 commit comments