util.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. local util = {}
  2. function util.map(t, fn)
  3. local res = {}
  4. for k, v in pairs(t) do res[k] = fn(v, k) end
  5. return res
  6. end
  7. function util.find(t, fn)
  8. local res = {}
  9. for k, v in pairs(t) do
  10. if fn(v, k) then return v end
  11. end
  12. end
  13. function util.compose(f, g)
  14. return function(...) return f(g(...)) end
  15. end
  16. function util.bind1(func, x)
  17. return function(y)
  18. return func(x, y)
  19. end
  20. end
  21. function util.getParentField(context, name, step_back)
  22. local obj = context.objects[#context.objects - step_back]
  23. if obj.__type == 'List' then
  24. return obj.ofType.fields[name]
  25. else
  26. return obj.fields[name]
  27. end
  28. end
  29. function util.coerceValue(node, schemaType, variables)
  30. variables = variables or {}
  31. if schemaType.__type == 'NonNull' then
  32. return util.coerceValue(node, schemaType.ofType, variables)
  33. end
  34. if not node then
  35. return nil
  36. end
  37. if node.kind == 'variable' then
  38. return variables[node.name.value]
  39. end
  40. if schemaType.__type == 'Union' then
  41. local matched, result
  42. for k, v in ipairs(schemaType.types) do
  43. matched, result = pcall(util.coerceValue, node, v, variables)
  44. if matched then
  45. return result
  46. end
  47. end
  48. local valid_types = util.map(schemaType.types, function (t) return t.name end)
  49. --TODO! how to better report this error?
  50. error('Could not coerce "' .. tostring(node.kind) .. '" to one of union types ['..table.concat(valid_types, ",")..']')
  51. end
  52. if schemaType.__type == 'List' then
  53. if node.kind ~= 'list' then
  54. error('Expected a list')
  55. end
  56. return util.map(node.values, function(value)
  57. return util.coerceValue(node.values[i], schemaType.ofType, variables)
  58. end)
  59. end
  60. if schemaType.__type == 'InputObject' then
  61. if node.kind ~= 'inputObject' then
  62. error('Expected an input object')
  63. end
  64. return util.map(node.values, function(field)
  65. if not schemaType.fields[field.name] then
  66. error('Unknown input object field "' .. field.name .. '"')
  67. end
  68. return util.coerceValue(field.value, schemaType.fields[field.name].kind, variables)
  69. end)
  70. end
  71. if schemaType.__type == 'Enum' then
  72. if node.kind ~= 'enum' then
  73. error('Expected enum value, got ' .. node.kind)
  74. end
  75. if not schemaType.values[node.value] then
  76. error('Invalid enum value "' .. node.value .. '"')
  77. end
  78. return node.value
  79. end
  80. if schemaType.__type == 'Scalar' then
  81. if schemaType.parseLiteral(node) == nil then
  82. error('Could not coerce "' .. tostring(node.value) .. '" to "' .. schemaType.name .. '"')
  83. end
  84. return schemaType.parseLiteral(node)
  85. end
  86. end
  87. return util