简体   繁体   English

创建 GraphQL 突变

[英]Creating GraphQL mutation

I have the following sample mutation:我有以下样本突变:

mutation {
  checkoutCreate(input: {
    lineItems: [{ variantId: "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80", quantity: 1 }]
  }) {
    checkout {
       id
       webUrl
       lineItems(first: 5) {
         edges {
           node {
             title
             quantity
           }
         }
       }
    }
  }
}

I'm using .net GraphQL client .我正在使用 .net GraphQL 客户端 I would like to somehow pass lineItems list to this query.我想以某种方式将 lineItems 列表传递给这个查询。 I did the following:我做了以下事情:

mutation {
  checkoutCreate(input: {
    $lineItems
  }) {
    checkout {
       id
       webUrl
       lineItems(first: 5) {
         edges {
           node {
             title
             quantity
           }
         }
       }
    }
  }
}

C# code: 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);

I keep getting:我不断得到:

GraphQLHttpException: Unexpected HttpResponseMessage with code: BadRequest GraphQLHttpException:意外的 HttpResponseMessage 代码:BadRequest

Is there a way to do this?有没有办法做到这一点? or do I have to replace within a string?还是我必须在字符串中替换?

EDIT:编辑:

I've tried this (and 20 other ways and still getting errors).我已经尝试过这个(以及其他 20 种方法,但仍然出现错误)。 All i'm trying to do is pass in a list of LineItems.我要做的就是传入一个 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 request looks like this: 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
      }
    ]
  }
}

Variables is an object, a single value.变量是一个对象,一个单一的值。

With pseudocode, you have variables = lineItems , but you need variables = { lineItems: lineItems }使用伪代码,您有variables = lineItems ,但您需要variables = { lineItems: lineItems }

In the query itself, you need to declare the variable and its type .在查询本身中,您需要声明变量及其类型 In your case this might look like在你的情况下,这可能看起来像

mutation CreateCheckout($lineItems: [LineItem!]!) {
  checkoutCreate(input: {
    $lineItems
  }) { ... FieldsFromTheQuestion }

The name of the operation ( CreateCheckout ) can be anything that's meaningful to you;操作名称 ( CreateCheckout ) 可以是任何对您有意义的名称; it's not specified in the schema.它没有在架构中指定。

@galkin's answer is probably relevant too: when you make the request you need to pass the line item list under the key "input" , matching the variable name in the query. @galkin 的回答也可能是相关的:当您发出请求时,您需要传递键"input"下的行项目列表,匹配查询中的变量名称。 The raw JSON request should look something like原始 JSON 请求应该类似于

{
  "query": "mutation CreateCheckout ...",
  "variables": {
    "input": [
      {
        "variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80",
        "quantity" 2
      }
    ]
  }
}

Thanks to @galkin and @David Maze, I solved it like this:感谢@galkin 和@David Maze,我是这样解决的:

mutation checkoutCreate($input: CheckoutCreateInput!) {
  checkoutCreate(input: $input) {
    checkout {
      id
    }
    checkoutUserErrors {
      code
      field
      message
    }
  }
}

c# code:代码:

            var request = new GraphQLRequest
            {
                Query = m_resource.Resource.GetString("CheckoutCreate"),
                Variables = new
                {
                    input = new
                    {
                        LineItems = lineItems // List<LineItem>
                    }
            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM