2017-06-05 117 views
1

我想在我的GraphQL查詢中做2個創建。 (我知道我的查詢結構是不正確的,但它說明我的問題)GraphQL - 是否有可能設置一個變量結果爲突變

mutation { 
    affiliateCreate(company: "test mutation") { 
    $id: id, 
    affiliateUserCreate(affiliate_id: $id, name: "test name") { 
     id, 
     name 
    }, 
    company 
    } 
} 

我想我的第一個ID結果是在可變誰我傳遞給第二個創建調用?我對GraphQL非常陌生,我想知道是否有可能。

有沒有其他辦法可以做這樣的事情?或者我必須做2突變呼叫?第一個是affiliateCreate,第二個呢?

謝謝

回答

5

你想做的事情不被GraphQL支持。在Graphcool API中,我們用我們所謂的嵌套變異來處理這種情況。我也聽說它被稱爲複雜的突變

嵌套創建突變的特徵是嵌套的輸入對象參數。如果添加輸入對象authoraffiliateCreate突變,你可以使用它像:

mutation createAffiliateAndUser { 
    affiliateCreate(
    company: "test company" 
    author: { 
     name: "test user" 
    } 
) { 
    id 
    } 
} 

這將創建一個聯盟,一個用戶,然後鏈接兩個在一起。相若方式,如果添加輸入對象affiliatesuserCreate突變,它可能是這樣的:

mutation createUserAndAffiliates { 
    userCreate(
    name: "test user" 
    affiliates: [{ 
     company: "first company" 
    }, { 
     company: "second company" 
    }] 
) { 
    id 
    } 
} 

瞭解更多關於嵌套突變in this article

+0

我想,在你的解決方法的回調中,你傳遞用戶ID來創建關聯公司? –

相關問題