Browse Source

Clean up helper functions;

bjorn 9 years ago
parent
commit
31db246c73
2 changed files with 39 additions and 40 deletions
  1. 35 40
      graphql/introspection.lua
  2. 4 0
      graphql/util.lua

+ 35 - 40
graphql/introspection.lua

@@ -2,20 +2,12 @@ local path = (...):gsub('%.[^%.]+$', '')
 local types = require(path .. '.types')
 local types = require(path .. '.types')
 local util = require(path .. '.util')
 local util = require(path .. '.util')
 
 
-local function instanceof(t, s)
-  return t.__type == s
-end
-
-local function trim(s)
-  return s:gsub('^%s+', ''):gsub('%s$', ''):gsub('%s%s+', ' ')
-end
-
 local __Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue,__EnumValue, __TypeKind
 local __Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue,__EnumValue, __TypeKind
 
 
 __Schema = types.object({
 __Schema = types.object({
   name = '__Schema',
   name = '__Schema',
 
 
-  description = trim [[
+  description = util.trim [[
     A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types
     A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types
     and directives on the server, as well as the entry points for query and mutation operations.
     and directives on the server, as well as the entry points for query and mutation operations.
   ]],
   ]],
@@ -60,7 +52,7 @@ __Schema = types.object({
 __Directive = types.object({
 __Directive = types.object({
   name = '__Directive',
   name = '__Directive',
 
 
-  description = trim [[
+  description = util.trim [[
     A Directive provides a way to describe alternate runtime execution and type validation behavior
     A Directive provides a way to describe alternate runtime execution and type validation behavior
     in a GraphQL document.
     in a GraphQL document.
 
 
@@ -126,7 +118,7 @@ __Directive = types.object({
 __DirectiveLocation = types.enum({
 __DirectiveLocation = types.enum({
   name = '__DirectiveLocation',
   name = '__DirectiveLocation',
 
 
-  description = trim [[
+  description = util.trim [[
     A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation
     A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation
     describes one such possible adjacencies.
     describes one such possible adjacencies.
   ]],
   ]],
@@ -167,7 +159,7 @@ __DirectiveLocation = types.enum({
 __Type = types.object({
 __Type = types.object({
   name = '__Type',
   name = '__Type',
 
 
-  description = trim [[
+  description = util.trim [[
     The fundamental unit of any GraphQL Schema is the type. There are
     The fundamental unit of any GraphQL Schema is the type. There are
     many kinds of types in GraphQL as represented by the `__TypeKind` enum.
     many kinds of types in GraphQL as represented by the `__TypeKind` enum.
 
 
@@ -186,37 +178,40 @@ __Type = types.object({
 
 
       kind = {
       kind = {
         kind = __TypeKind.nonNull,
         kind = __TypeKind.nonNull,
-        resolve = function(type)
-          if instanceof(type, 'Scalar') then
+        resolve = function(kind)
+          if kind.__type == 'Scalar' then
             return 'SCALAR'
             return 'SCALAR'
-          elseif instanceof(type, 'Object') then
+          elseif kind.__type == 'Object' then
             return 'OBJECT'
             return 'OBJECT'
-          elseif instanceof(type, 'Interface') then
+          elseif kind.__type == 'Interface' then
             return 'INTERFACE'
             return 'INTERFACE'
-          elseif instanceof(type, 'Union') then
+          elseif kind.__type == 'Union' then
             return 'UNION'
             return 'UNION'
-          elseif instanceof(type, 'Enum') then
+          elseif kind.__type == 'Enum' then
             return 'ENUM'
             return 'ENUM'
-          elseif instanceof(type, 'InputObject') then
+          elseif kind.__type == 'InputObject' then
             return 'INPUT_OBJECT'
             return 'INPUT_OBJECT'
-          elseif instanceof(type, 'List') then
+          elseif kind.__type == 'List' then
             return 'LIST'
             return 'LIST'
-          elseif instanceof(type, 'NonNull') then
+          elseif kind.__type == 'NonNull' then
             return 'NON_NULL'
             return 'NON_NULL'
           end
           end
 
 
-          error('Unknown kind of kind = ' .. type)
+          error('Unknown type ' .. kind)
         end
         end
       },
       },
 
 
       fields = {
       fields = {
         kind = types.list(types.nonNull(__Field)),
         kind = types.list(types.nonNull(__Field)),
         arguments = {
         arguments = {
-          includeDeprecated = { kind = types.boolean, defaultValue = false }
+          includeDeprecated = {
+            kind = types.boolean,
+            defaultValue = false
+          }
         },
         },
-        resolve = function(type, arguments)
-          if instanceof(type, 'Object') or instanceof(type, 'Interface') then
-            return util.filter(util.values(type.fields), function(field)
+        resolve = function(kind, arguments)
+          if kind.__type == 'Object' or kind.__type == 'Interface' then
+            return util.filter(util.values(kind.fields), function(field)
               return arguments.includeDeprecated or field.deprecationReason == nil
               return arguments.includeDeprecated or field.deprecationReason == nil
             end)
             end)
           end
           end
@@ -227,18 +222,18 @@ __Type = types.object({
 
 
       interfaces = {
       interfaces = {
         kind = types.list(types.nonNull(__Type)),
         kind = types.list(types.nonNull(__Type)),
-        resolve = function(type)
-          if instanceof(type, 'Object') then
-            return type.interfaces
+        resolve = function(kind)
+          if kind.__type == 'Object' then
+            return kind.interfaces
           end
           end
         end
         end
       },
       },
 
 
       possibleTypes = {
       possibleTypes = {
         kind = types.list(types.nonNull(__Type)),
         kind = types.list(types.nonNull(__Type)),
-        resolve = function(type, arguments, context)
-          if instanceof(type, 'Interface') or instanceof(type, 'Union') then
-            return context.schema:getPossibleTypes(type)
+        resolve = function(kind, arguments, context)
+          if kind.__type == 'Interface' or kind.__type == 'Union' then
+            return context.schema:getPossibleTypes(kind)
           end
           end
         end
         end
       },
       },
@@ -248,9 +243,9 @@ __Type = types.object({
         arguments = {
         arguments = {
           includeDeprecated = { kind = types.boolean, defaultValue = false }
           includeDeprecated = { kind = types.boolean, defaultValue = false }
         },
         },
-        resolve = function(type, arguments)
-          if instanceof(type, 'Enum') then
-            return util.filter(util.values(type.values), function(value)
+        resolve = function(kind, arguments)
+          if kind.__type == 'Enum' then
+            return util.filter(util.values(kind.values), function(value)
               return arguments.includeDeprecated or not value.deprecationReason
               return arguments.includeDeprecated or not value.deprecationReason
             end)
             end)
           end
           end
@@ -259,9 +254,9 @@ __Type = types.object({
 
 
       inputFields = {
       inputFields = {
         kind = types.list(types.nonNull(__InputValue)),
         kind = types.list(types.nonNull(__InputValue)),
-        resolve = function(type)
-          if instanceof(type, 'InputObject') then
-            return util.values(type.fields)
+        resolve = function(kind)
+          if kind.__type == 'InputObject' then
+            return util.values(kind.fields)
           end
           end
         end
         end
       },
       },
@@ -276,7 +271,7 @@ __Type = types.object({
 __Field = types.object({
 __Field = types.object({
   name = '__Field',
   name = '__Field',
 
 
-  description = trim [[
+  description = util.trim [[
     Object and Interface types are described by a list of Fields, each of
     Object and Interface types are described by a list of Fields, each of
     which has a name, potentially a list of arguments, and a return type.
     which has a name, potentially a list of arguments, and a return type.
   ]],
   ]],
@@ -332,7 +327,7 @@ __Field = types.object({
 __InputValue = types.object({
 __InputValue = types.object({
   name = '__InputValue',
   name = '__InputValue',
 
 
-  description = trim [[
+  description = util.trim [[
     Arguments provided to Fields or Directives and the input fields of an
     Arguments provided to Fields or Directives and the input fields of an
     InputObject are represented as Input Values which describe their type
     InputObject are represented as Input Values which describe their type
     and optionally a default value.
     and optionally a default value.

+ 4 - 0
graphql/util.lua

@@ -41,6 +41,10 @@ function util.bind1(func, x)
   end
   end
 end
 end
 
 
+function util.trim(s)
+  return s:gsub('^%s+', ''):gsub('%s$', ''):gsub('%s%s+', ' ')
+end
+
 function util.coerceValue(node, schemaType, variables)
 function util.coerceValue(node, schemaType, variables)
   variables = variables or {}
   variables = variables or {}