我有以下样本突变:
mutation {
checkoutCreate(input: {
lineItems: [{ variantId: "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80", quantity: 1 }]
}) {
checkout {
id
webUrl
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
我正在使用 .net GraphQL 客户端。我想以某种方式将 lineItems 列表传递给此查询。我做了以下事情:
mutation {
checkoutCreate(input: {
$lineItems
}) {
checkout {
id
webUrl
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
C#代码:
dynamic lineItems = new List<dynamic>
{
new
{
variantId = "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80",
quantity = 2
}
};
var request = new GraphQLRequest
{
Query = m_resource.Resource.GetString("CheckoutCreate"), // Gets from resource file the above string
Variables = lineItems
};
var response = await m_client.PostAsync(request);
我不断得到:
GraphQLHttpException:意外的 HttpResponseMessage 代码:BadRequest
有没有办法做到这一点?还是我必须在字符串中替换?
编辑:
我已经尝试过了(以及其他 20 种方法,但仍然出现错误)。我要做的就是传入一个 LineItems 列表。
mutation CreateCheckout($input: LineItemsInput!) {
checkoutCreate(input: $input) {
checkout {
id
webUrl
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
var request = new GraphQLRequest
{
Query = m_resource.Resource.GetString("CheckoutCreate"),
Variables = new
{
LineItemsInput = new List<dynamic>
{
new
{
variantId = "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80",
quantity = 2
}
}
}
};
Json 请求如下所示:
{
"Query": "mutation CreateCheckout($input: LineItemsInput!) {\r\n checkoutCreate(input: $input) {\r\n checkout {\r\n id\r\n webUrl\r\n lineItems(first: 5) {\r\n edges {\r\n node {\r\n title\r\n quantity\r\n }\r\n }\r\n }\r\n }\r\n }\r\n}",
"OperationName": null,
"Variables": {
"LineItemsInput": [
{
"variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80",
"quantity": 2
}
]
}
}