Browse Source

implement suggestions and remove Union handling in coerceValue

Ruslan Talpa 9 years ago
parent
commit
7ba7b5f30c
2 changed files with 12 additions and 25 deletions
  1. 4 4
      graphql/rules.lua
  2. 8 21
      graphql/util.lua

+ 4 - 4
graphql/rules.lua

@@ -40,7 +40,7 @@ end
 
 
 function rules.argumentsDefinedOnType(node, context)
 function rules.argumentsDefinedOnType(node, context)
   if node.arguments then
   if node.arguments then
-    local parentField = util.getParentField(context, node.name.value, 1)
+    local parentField = util.getParentField(context, node.name.value)
     for _, argument in pairs(node.arguments) do
     for _, argument in pairs(node.arguments) do
       local name = argument.name.value
       local name = argument.name.value
       if not parentField.arguments[name] then
       if not parentField.arguments[name] then
@@ -178,7 +178,7 @@ end
 
 
 function rules.argumentsOfCorrectType(node, context)
 function rules.argumentsOfCorrectType(node, context)
   if node.arguments then
   if node.arguments then
-    local parentField = util.getParentField(context, node.name.value, 1)
+    local parentField = util.getParentField(context, node.name.value)
     for _, argument in pairs(node.arguments) do
     for _, argument in pairs(node.arguments) do
       local name = argument.name.value
       local name = argument.name.value
       local argumentType = parentField.arguments[name]
       local argumentType = parentField.arguments[name]
@@ -189,7 +189,7 @@ end
 
 
 function rules.requiredArgumentsPresent(node, context)
 function rules.requiredArgumentsPresent(node, context)
   local arguments = node.arguments or {}
   local arguments = node.arguments or {}
-  local parentField = util.getParentField(context, node.name.value, 1)
+  local parentField = util.getParentField(context, node.name.value)
   for name, argument in pairs(parentField.arguments) do
   for name, argument in pairs(parentField.arguments) do
     if argument.__type == 'NonNull' then
     if argument.__type == 'NonNull' then
       local present = util.find(arguments, function(argument)
       local present = util.find(arguments, function(argument)
@@ -448,7 +448,7 @@ function rules.variableUsageAllowed(node, context)
     if not arguments then return end
     if not arguments then return end
 
 
     for field in pairs(arguments) do
     for field in pairs(arguments) do
-      local parentField = util.getParentField(context, field, 1)
+      local parentField = util.getParentField(context, field)
       for i = 1, #arguments[field] do
       for i = 1, #arguments[field] do
         local argument = arguments[field][i]
         local argument = arguments[field][i]
         if argument.value.kind == 'variable' then
         if argument.value.kind == 'variable' then

+ 8 - 21
graphql/util.lua

@@ -23,13 +23,14 @@ function util.bind1(func, x)
   end
   end
 end
 end
 
 
-function util.getParentField(context, name, step_back)
-  local obj = context.objects[#context.objects - step_back]
-    if obj.__type == 'List' then
-      return obj.ofType.fields[name]
-    else
-      return obj.fields[name]
-    end
+function util.getParentField(context, name, count)
+  count = count == nil and 1 or count
+  local obj = context.objects[#context.objects - count]
+  if obj.__type == 'List' then
+    return obj.ofType.fields[name]
+  else
+    return obj.fields[name]
+  end
 end
 end
 
 
 function util.coerceValue(node, schemaType, variables)
 function util.coerceValue(node, schemaType, variables)
@@ -47,20 +48,6 @@ function util.coerceValue(node, schemaType, variables)
     return variables[node.name.value]
     return variables[node.name.value]
   end
   end
 
 
-  if schemaType.__type == 'Union' then
-    local matched, result
-    for k, v in ipairs(schemaType.types) do
-        matched, result = pcall(util.coerceValue, node, v, variables)
-        if matched then
-          return result
-        end
-    end
-
-    local valid_types = util.map(schemaType.types, function (t) return t.name end)
-    --TODO! how to better report this error?
-    error('Could not coerce "' .. tostring(node.kind) .. '" to one of union types ['..table.concat(valid_types, ",")..']')
-  end
-
   if schemaType.__type == 'List' then
   if schemaType.__type == 'List' then
     if node.kind ~= 'list' then
     if node.kind ~= 'list' then
       error('Expected a list')
       error('Expected a list')