我有以下样本突变:
mutation {
checkoutCreate(input: {
lineItems: [{ variantId: "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80", quantity: 1 }]
}) {
checkout {
id
webUrl
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
我正在使用.net GraphQL client。我想以某种方式将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
}
]
}
}
发布于 2019-03-06 04:31:41
感谢@galkin和@David Maze,我是这样解决的:
mutation checkoutCreate($input: CheckoutCreateInput!) {
checkoutCreate(input: $input) {
checkout {
id
}
checkoutUserErrors {
code
field
message
}
}
}
c#代码:
var request = new GraphQLRequest
{
Query = m_resource.Resource.GetString("CheckoutCreate"),
Variables = new
{
input = new
{
LineItems = lineItems // List<LineItem>
}
};
发布于 2019-03-05 07:11:58
变量是一个对象,是一个单一的值。
使用伪代码时,您有variables = lineItems
,但需要variables = { lineItems: lineItems }
发布于 2019-03-05 07:28:43
在查询本身中,您需要declare the variable and its type。在您的例子中,这可能看起来像这样
mutation CreateCheckout($lineItems: [LineItem!]!) {
checkoutCreate(input: {
$lineItems
}) { ... FieldsFromTheQuestion }
操作的名称(CreateCheckout
)可以是对您有意义的任何东西;它没有在模式中指定。
@galkin的答案可能也是相关的:当您发出请求时,您需要在键"input"
下传递行项目列表,并与查询中的变量名相匹配。raw JSON request应该看起来像这样
{
"query": "mutation CreateCheckout ...",
"variables": {
"input": [
{
"variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80",
"quantity" 2
}
]
}
}
https://stackoverflow.com/questions/54992592
复制相似问题