0

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. I would like to somehow pass lineItems list to this query. I did the following:

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

C# code:

    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

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). All i'm trying to do is pass in a list of 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:

{
  "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
      }
    ]
  }
}
4

3 に答える 3

1

変数はオブジェクトであり、単一の値です。

疑似コードを使用するとvariables = lineItems、 がありますが、必要ですvariables = { lineItems: lineItems }

于 2019-03-04T23:11:58.437 に答える