2016-09-19 111 views
0

我試圖用graphql mutation創建一個對象列表,但一直不成功。我發現錯誤,請查看代碼片段並評論錯誤傳播的位置。如何使用石墨烯的突變方法從一系列的字典中提取數據?

注:我使用Graphene上瓶與Python 2.7

下面是一個例子有效載荷:

mutation UserMutation { 
    createUser(
     phones: [ 
      { 
       「number」: 「609-777-7777」, 
       「label」: 「home" 
      }, 
      { 
       「number」: 「609-777-7778」, 
       「label」: 「mobile" 
      } 
     ] 
    ) 
} 

在架構,我有以下幾點:

class CreateUser(graphene.Mutation): 
    ok = graphene.Boolean() 
    ... 
    phones = graphene.List(graphene.String()) # this is a list of string but what I need is a list of dicts! 

回答

3

要將字典用作輸入,您需要使用InputObjectType。 (InputObjectType s就像對象類型,但僅用於輸入數據)。

這個例子應該與石墨烯1.0一起使用。

class PhoneInput(graphene.InputObjectType): 
    number = graphene.String() 
    label = graphene.String() 

class CreateUser(graphene.Mutation): 
    class Input: 
     phones = graphene.List(PhoneInput) 
    ok = graphene.Boolean() 

class Mutation(graphene.ObjectType): 
    create_user = CreateUser.Field() 
+0

嗨,Syrus,如何將手機保存到分貝。我使用文檔代碼,但數據庫不受影響 –