2
0
Эх сурвалжийг харах

Fix issue with getParentField; Introspection cleanup;

bjorn 9 жил өмнө
parent
commit
9ddf276c88

+ 16 - 35
graphql/introspection.lua

@@ -11,27 +11,6 @@ local function instanceof(t, s)
   return t.__type == s
   return t.__type == s
 end
 end
 
 
-local function resolveDirective(directive)
-  local res = {}
-  if directive.onQuery then table.insert(res, 'QUERY') end
-  if directive.onMutation then table.insert(res, 'MUTATION') end
-  if directive.onField then table.insert(res, 'FIELD') end
-  if directive.onFragmentDefinition then table.insert(res, 'FRAGMENT_DEFINITION') end
-  if directive.onFragmentSpread then table.insert(res, 'FRAGMENT_SPREAD') end
-  if directive.onInlineFragment then table.insert(res, 'INLINE_FRAGMENT') end
-  return res
-end
-
-local function mapToList(m)
-  local r = {}
-
-  for k,v in pairs(m) do
-    table.insert(r, v)
-  end
-
-  return r
-end
-
 local __Directive, __DirectiveLocation, __Type, __Field, __InputValue,__EnumValue, TypeKind, __TypeKind, SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef, astFromValue, printAst, printers
 local __Directive, __DirectiveLocation, __Type, __Field, __InputValue,__EnumValue, TypeKind, __TypeKind, SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef, astFromValue, printAst, printers
 
 
 local __Schema = types.object({
 local __Schema = types.object({
@@ -49,14 +28,7 @@ local __Schema = types.object({
         description = 'A list of all types supported by this server.',
         description = 'A list of all types supported by this server.',
         kind = types.nonNull(types.list(types.nonNull(__Type))),
         kind = types.nonNull(types.list(types.nonNull(__Type))),
         resolve = function(schema)
         resolve = function(schema)
-          local typeMap = schema:getTypeMap()
-          local res = {}
-
-          for k, v in pairs(typeMap) do
-            table.insert(res, typeMap[k])
-          end
-
-          return res
+          return util.values(schema:getTypeMap())
         end
         end
       },
       },
 
 
@@ -268,9 +240,9 @@ __Type = types.object({
         if instanceof(type, 'Enum') then
         if instanceof(type, 'Enum') then
           local values = type.values;
           local values = type.values;
           if not args.includeDeprecated then
           if not args.includeDeprecated then
-            values = util.filter(values, function(value) return not value.deprecationReason end);
+            values = util.filter(values, function(value) return not value.deprecationReason end)
           end
           end
-          return mapToList(values);
+          return util.values(values)
         end
         end
       end
       end
     },
     },
@@ -279,13 +251,22 @@ __Type = types.object({
       resolve = function(type)
       resolve = function(type)
         if instanceof(type, 'InputObject') then
         if instanceof(type, 'InputObject') then
           local fieldMap = type.fields;
           local fieldMap = type.fields;
-          local fields = {}; for k,v in pairs(fieldMap) do table.insert(fields, fieldMap[k]) end; return fields
+          local fields = {}
+
+          for k, v in pairs(fieldMap) do
+            table.insert(fields, fieldMap[k])
+          end
+
+          return fields
         end
         end
       end
       end
     },
     },
-    ofType = { kind = __Type }
+
+    ofType = {
+      kind = __Type
+    }
   } end
   } end
-});
+})
 
 
 __Field = types.object({
 __Field = types.object({
   name = '__Field',
   name = '__Field',
@@ -439,7 +420,7 @@ SchemaMetaFieldDef = {
   description = 'Access the current type schema of this server.',
   description = 'Access the current type schema of this server.',
   arguments = {},
   arguments = {},
   resolve = function(source, args, context, obj) return context.schema  end
   resolve = function(source, args, context, obj) return context.schema  end
-};
+}
 
 
 TypeMetaFieldDef = {
 TypeMetaFieldDef = {
   name = '__type',
   name = '__type',

+ 12 - 11
graphql/schema.lua

@@ -114,22 +114,23 @@ function schema:getPossibleTypes(abstractType)
 end
 end
 
 
 function schema.getParentField(context, name, count)
 function schema.getParentField(context, name, count)
-  local parent = nil
-
   if name == '__schema' then
   if name == '__schema' then
-    parent = introspection.SchemaMetaFieldDef
+    return introspection.SchemaMetaFieldDef
   elseif name == '__type' then
   elseif name == '__type' then
-    parent = introspection.TypeMetaFieldDef
+    return introspection.TypeMetaFieldDef
   elseif name == '__typename' then
   elseif name == '__typename' then
-    parent = introspection.TypeNameMetaFieldDef
-  else
-    count = count or 1
-    local obj = context.objects[#context.objects - count]
-    if obj.ofType then obj = obj.ofType end
-    parent = obj.fields[name]
+    return introspection.TypeNameMetaFieldDef
+  end
+
+  count = count or 1
+  local parent = context.objects[#context.objects - count]
+
+  -- Unwrap lists and non-null types
+  while parent.ofType do
+    parent = parent.ofType
   end
   end
 
 
-  return parent
+  return parent.fields[name]
 end
 end
 
 
 return schema
 return schema

+ 8 - 0
graphql/util.lua

@@ -23,6 +23,14 @@ function util.filter(t, fn)
   return res
   return res
 end
 end
 
 
+function util.values(t)
+  local res = {}
+  for _, value in pairs(t) do
+    table.insert(res, value)
+  end
+  return res
+end
+
 function util.compose(f, g)
 function util.compose(f, g)
   return function(...) return f(g(...)) end
   return function(...) return f(g(...)) end
 end
 end

+ 2 - 1
graphql/validate.lua

@@ -63,6 +63,7 @@ local visitors = {
     enter = function(node, context)
     enter = function(node, context)
       local field, parentField
       local field, parentField
       local name = node.name.value
       local name = node.name.value
+
       if name == '__schema' then
       if name == '__schema' then
         field = introspection.SchemaMetaFieldDef.kind
         field = introspection.SchemaMetaFieldDef.kind
       elseif name == '__type' then
       elseif name == '__type' then
@@ -74,7 +75,7 @@ local visitors = {
         -- false is a special value indicating that the field was not present in the type definition.
         -- false is a special value indicating that the field was not present in the type definition.
         field = parentField and parentField.kind or false
         field = parentField and parentField.kind or false
       end
       end
-      
+
       table.insert(context.objects, field)
       table.insert(context.objects, field)
     end,
     end,