Răsfoiți Sursa

Merge pull request #17 from bjornbytes/json-tables

Custom JSON Serialization of Empty Objects
Bjorn Swenson 8 ani în urmă
părinte
comite
6312c75b09
2 a modificat fișierele cu 6 adăugiri și 2 ștergeri
  1. 3 2
      graphql/execute.lua
  2. 3 0
      graphql/schema.lua

+ 3 - 2
graphql/execute.lua

@@ -175,7 +175,7 @@ local function completeValue(fieldType, result, subSelections, context)
       values[i] = completeValue(innerType, value, subSelections, context)
       values[i] = completeValue(innerType, value, subSelections, context)
     end
     end
 
 
-    return values
+    return next(values) and values or context.schema.__emptyList
   end
   end
 
 
   if fieldTypeName == 'Scalar' or fieldTypeName == 'Enum' then
   if fieldTypeName == 'Scalar' or fieldTypeName == 'Enum' then
@@ -183,7 +183,8 @@ local function completeValue(fieldType, result, subSelections, context)
   end
   end
 
 
   if fieldTypeName == 'Object' then
   if fieldTypeName == 'Object' then
-    return evaluateSelections(fieldType, result, subSelections, context)
+    local fields = evaluateSelections(fieldType, result, subSelections, context)
+    return next(fields) and fields or context.schema.__emptyObject
   elseif fieldTypeName == 'Interface' or fieldTypeName == 'Union' then
   elseif fieldTypeName == 'Interface' or fieldTypeName == 'Union' then
     local objectType = fieldType.resolveType(result)
     local objectType = fieldType.resolveType(result)
     return evaluateSelections(objectType, result, subSelections, context)
     return evaluateSelections(objectType, result, subSelections, context)

+ 3 - 0
graphql/schema.lua

@@ -5,6 +5,9 @@ local introspection = require(path .. '.introspection')
 local schema = {}
 local schema = {}
 schema.__index = schema
 schema.__index = schema
 
 
+schema.__emptyList = {}
+schema.__emptyObject = {}
+
 function schema.create(config)
 function schema.create(config)
   assert(type(config.query) == 'table', 'must provide query object')
   assert(type(config.query) == 'table', 'must provide query object')
   assert(not config.mutation or type(config.mutation) == 'table', 'mutation must be a table if provided')
   assert(not config.mutation or type(config.mutation) == 'table', 'mutation must be a table if provided')