2017-07-30 72 views
0

我想用變量實現突變。但我得到以下錯誤:graphql突變給出語法錯誤:預期名稱

"Syntax Error GraphQL request (3:22) Expected Name, found $ 

2:  mutation { 
3:  createProperty($property) { 
         ^
4:   id 
" 

我的模式絕對不說的名字什麼,這就是爲什麼我認爲這個錯誤就是這麼奇怪。我也不會去想graphql的單證/阿波羅是很好。

從客戶端調用突變:

const property = { 
    title: 'First house', 
    cost: 849, 
    bedrooms: 3, 
    bathrooms: 2, 
    car_spaces: 1, 
    house_size: 60, 
    }; 

    const createPropertyQuery = 
    graphql(gql` 
    mutation { 
     createProperty($property) { 
     id 
     } 
    } 
    `, { 
     options: { 
     variables: { 
      property, 
     }, 
     }, 
    }); 


    const { data } = await apolloClient.query({ 
    query: createPropertyQuery, 
    }); 

模式:

type Property { 
    title: String! 
    cost: Float 
    user: User 
    bedrooms: Int! 
    bathrooms: Int! 
    car_spaces: Int! 
    house_size: Int! 
} 
input propertyInput { 
    title: String! 
    cost: Float 
    bedrooms: Int! 
    bathrooms: Int! 
    car_spaces: Int! 
    house_size: Int! 
} 

type RootMutation { 
    createProperty (
     property: propertyInput 
    ): Property 
} 
+0

您需要修改您的查詢並實際聲明您在操作定義中發送的變量。請參閱[本答案](https://stackoverflow.com/a/45131876/6024220)以獲取詳細解釋。 –

+1

[GraphQL - Syntax Error GraphQL request(5:15)Expected Name]可能重複(https://stackoverflow.com/questions/45130386/graphql-syntax-error-graphql-request-515-expected-name) –

回答

3

你首先應該提到的參數的名字!

mutation CreatePropertyMutatuin($property: propertyInput){ 
    createProperty(property: $property) { 
    id 
    } 
}