Как передать переменные в этот график
У меня есть этот GraphQL, и он работает, но когда я пытаюсь поставить переменные не будет работать
mutation serviceOrder {
serviceOrderCreate(input: {data: {
needsTo: "string",
problemDescription: "string",
profession: {},
responseTime: "2018-05-22T05:08:04.154Z",
address: {},
insurance: "string",
insurance_code: "string",
personId: 1,
userId: 1
}}) {
clientMutationId
obj {
id
user {
firstName
}
}
}
}
Это с переменными, но не работает, любая помощь? Моя мутация выглядит так:
mutation serviceOrder(
$needsTo:String!
$problemDescription:String!
$profession:String!
$responseTime:String!
$address:String!
$insurance:String!
$insurance_code:String!
$personId:ID!
$userId:ID!
) {
serviceOrderCreate(input: {
data:{
needsTo: $needsTo,
problemDescription:$problemDescription,
profession: $profession ,
responseTime: $responseTime,
address:$address,
insurance:$insurance ,
insurance_code:$insurance_code ,
personId:$personId,
userId: $userId
}
}) {
clientMutationId
obj {
id
profession
user{
lastName
}
}
}
}
Но это дает мне ошибку: serviceOrder
экземпляр недействителен Подробности: needsTo
не может быть пустым (значение: ноль); problemDescription
не может быть пустым (значение: ноль); profession
не может быть пустым (значение: ноль); responseTime
не может быть пустым (значение: ноль); address
не может быть пустым (значение: ноль); insurance
не может быть пустым (значение: ноль).",
Обновление: так я исправляю свою мутацию
mutation serviceOrder($input: JSON) {
serviceOrderCreate(input: {data: $input}) {
obj {
problemDescription
user{
lastName
}
}
}
}
передавая это как переменную
{
"input": {
"needsTo": "Clinical embryologist",
"problemDescription": "Father fear wish head laugh rule physical TV.",
"profession": "Accommodation manager",
"responseTime": "2018-05-22T19:03:05.229Z",
"address": "3070 Patrick Overpass Apt. 070\nJamesfort, SC 14607",
"insurance": "true",
"insuranceCode": "string",
"personId": 1,
"userId": 1
}
}
1 ответ
Есть два способа сделать это. Во-первых, вы можете вообще не использовать переменные, как вы и пытаетесь сделать. Ваша мутация выглядит так:
mutation serviceOrder {
serviceOrderCreate(input: {
data: {
needsTo: "string"
problemDescription: "string"
profession: {}
responseTime: $responseTime
address: {}
insurance: "string"
insurance_code: "string"
personId: 1
userId: 1
}
}) {
clientMutationId
obj {
id
profession
user{
lastName
}
}
}
}
Или, во-вторых, вы передаете переменные как JSON вместе с вашим запросом. Это означает, что ваш запрос к конечной точке будет включать не только query
параметр с вашим исходным запросом (ниже), но также variables
параметр с переменными, которые вы включали.
mutation serviceOrder(
$needsTo:String!
$problemDescription:String!
$profession:String!
$responseTime:String!
$address:String!
$insurance:String!
$insurance_code:String!
$personId:ID!
$userId:ID!
) {
serviceOrderCreate(input: {
data:{
needsTo: $needsTo,
problemDescription:$problemDescription,
profession: $profession ,
responseTime: $responseTime,
address:$address,
insurance:$insurance ,
insurance_code:$insurance_code ,
personId:$personId,
userId: $userId
}
}) {
clientMutationId
obj {
id
profession
user{
lastName
}
}
}
}
переменные JSON:
{
"needsTo": "string",
"problemDescription": "string",
"profession": {}
// and so on
}
То, как вы на самом деле отправляете эти переменные вместе с вашим запросом, зависит от клиента, которого вы используете (если вы вообще используете его), что не ясно из вашего вопроса.