2017-06-29 53 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

哦,謝謝!我沒有意識到這一點! –