2017-06-29 52 views
1

如何在nodeJS中实现具有可选参数的GraphQL突变?GraphQL-js突变可选参数

我目前的突变有一个args字段,但所有的参数都是强制性的。由于我在文档中找不到任何东西,我不知道这是可能的。

这是我当前的代码:

const fakeDB = { 
    count: 0 
}; 

const schema = new GraphQLSchema({ 
    query: //... 
    mutation: new GraphQLObjectType({ 
     name: 'adsF', 
     fields: { 
      updateCount: { 
       type: GraphQLInt, 
       args: { 
        count: { type: GraphQLInt } // I want to make this argument optional 
       }, 
       resolve: (value, { count }) => { 
        // Catch if count is null or undefined 
        if (count == null) { 
         // If so just update with default 
         fakeDB.count = 5; 
        } else { 
         fakeDB.count = count 
        } 
        return fakeDB.count; 
       }) 
      } 
     } 
    }) 
}); 

感谢您的帮助!

回答

2

默认情况下,GraphQL中的类型可以为空。这意味着您此刻指定突变的方式可使计数成为可选项。如果你想要一个字段是强制性的,你需要将其标记为non null

+0

哦,谢谢!我没有意识到这一点! –