浏览代码

Parse directives in fragment fields;

bjorn 9 年之前
父节点
当前提交
449a574932
共有 2 个文件被更改,包括 46 次插入22 次删除
  1. 15 16
      parse.lua
  2. 31 6
      tests/parse.lua

+ 15 - 16
parse.lua

@@ -33,7 +33,7 @@ end
 local function list(pattern, min)
 local function list(pattern, min)
   if type(pattern) == 'string' then pattern = V(pattern) end
   if type(pattern) == 'string' then pattern = V(pattern) end
   min = min or 0
   min = min or 0
-  return Ct((pattern * comma * ws) ^ min)
+  return Ct((pattern * ws * comma * ws) ^ min)
 end
 end
 
 
 -- Formatters
 -- Formatters
@@ -122,10 +122,11 @@ local function cSelectionSet(selections)
   }
   }
 end
 end
 
 
-local function cFragmentSpread(name)
+local function cFragmentSpread(name, directives)
   return {
   return {
     kind = 'fragmentSpread',
     kind = 'fragmentSpread',
-    name = name
+    name = name,
+    directives = directives
   }
   }
 end
 end
 
 
@@ -199,18 +200,16 @@ end
 
 
 local function cInlineFragment(...)
 local function cInlineFragment(...)
   local args = pack(...)
   local args = pack(...)
-  if #args == 2 then
-    return {
-      kind = 'inlineFragment',
-      typeCondition = args[1],
-      selectionSet = args[2]
-    }
-  elseif #args == 1 then
-    return {
-      kind = 'inlineFragment',
-      selectionSet = args[1]
-    }
+  local result = { kind = 'inlineFragment' }
+  result.selectionSet = args[#args]
+  for i = 1, #args - 1 do
+    if args[i].kind == 'namedType' or args[i].kind == 'listType' or args[i].kind == 'nonNullType' then
+      result.typeCondition = args[i]
+    elseif args[i][1] and args[i][1].kind == 'directive' then
+      result.directives = args[i]
+    end
   end
   end
+  return result
 end
 end
 
 
 local function cVariable(name)
 local function cVariable(name)
@@ -268,8 +267,8 @@ local graphQL = P {
   selection = ws * (_'field' + _'fragmentSpread' + _'inlineFragment'),
   selection = ws * (_'field' + _'fragmentSpread' + _'inlineFragment'),
 
 
   field = ws * maybe(alias) * name * maybe('arguments') * maybe('directives') * maybe('selectionSet') / cField,
   field = ws * maybe(alias) * name * maybe('arguments') * maybe('directives') * maybe('selectionSet') / cField,
-  fragmentSpread = ws * '...' * ws * fragmentName / cFragmentSpread,
-  inlineFragment = ws * '...' * ws * maybe('typeCondition') * _'selectionSet' / cInlineFragment,
+  fragmentSpread = ws * '...' * ws * fragmentName * maybe('directives') / cFragmentSpread,
+  inlineFragment = ws * '...' * ws * maybe('typeCondition') * maybe('directives') * _'selectionSet' / cInlineFragment,
   typeCondition = 'on' * ws * _'namedType',
   typeCondition = 'on' * ws * _'namedType',
 
 
   argument = ws * name * ':' * _'value' / cArgument,
   argument = ws * name * ':' * _'value' / cArgument,

+ 31 - 6
tests/parse.lua

@@ -204,15 +204,27 @@ describe('parse', function()
     end)
     end)
   end)
   end)
 
 
-  test('fragmentSpread', function()
+  describe('fragmentSpread', function()
     local fragmentSpread
     local fragmentSpread
 
 
-    expect(function() parse('{..a}') end).to.fail()
-    expect(function() parse('{...}') end).to.fail()
+    test('name', function()
+      expect(function() parse('{..a}') end).to.fail()
+      expect(function() parse('{...}') end).to.fail()
+
+      fragmentSpread = parse('{...a}').definitions[1].selectionSet.selections[1]
+      expect(fragmentSpread.kind).to.equal('fragmentSpread')
+      expect(fragmentSpread.name.value).to.equal('a')
+    end)
 
 
-    fragmentSpread = parse('{...a}').definitions[1].selectionSet.selections[1]
-    expect(fragmentSpread.kind).to.equal('fragmentSpread')
-    expect(fragmentSpread.name.value).to.equal('a')
+    test('directives', function()
+      expect(function() parse('{...a@}') end).to.fail()
+
+      fragmentSpread = parse('{...a}').definitions[1].selectionSet.selections[1]
+      expect(fragmentSpread.directives).to_not.exist()
+
+      fragmentSpread = parse('{...a@skip}').definitions[1].selectionSet.selections[1]
+      expect(fragmentSpread.directives).to.exist()
+    end)
   end)
   end)
 
 
   describe('inlineFragment', function()
   describe('inlineFragment', function()
@@ -230,6 +242,19 @@ describe('parse', function()
       expect(inlineFragment.typeCondition.name.value).to.equal('a')
       expect(inlineFragment.typeCondition.name.value).to.equal('a')
     end)
     end)
 
 
+    test('directives', function()
+      expect(function() parse('{...on a @ {}}') end).to.fail()
+
+      inlineFragment = parse('{...{}}').definitions[1].selectionSet.selections[1]
+      expect(inlineFragment.directives).to_not.exist()
+
+      inlineFragment = parse('{...@skip{}}').definitions[1].selectionSet.selections[1]
+      expect(inlineFragment.directives).to.exist()
+
+      inlineFragment = parse('{...on a@skip {}}').definitions[1].selectionSet.selections[1]
+      expect(inlineFragment.directives).to.exist()
+    end)
+
     test('selectionSet', function()
     test('selectionSet', function()
       expect(function() parse('{... on a}') end).to.fail()
       expect(function() parse('{... on a}') end).to.fail()