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
      }
    ]
  }
}
Dr. Oste
  • 40
  • 1
  • 9
ShaneKm
  • 20,823
  • 43
  • 167
  • 296

3 Answers3

1

Variables is an object, a single value.

With pseudocode, you have variables = lineItems, but you need variables = { lineItems: lineItems }

galkin
  • 5,264
  • 3
  • 34
  • 51
1

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; 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. The raw JSON request should look something like

{
  "query": "mutation CreateCheckout ...",
  "variables": {
    "input": [
      {
        "variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80",
        "quantity" 2
      }
    ]
  }
}
David Maze
  • 130,717
  • 29
  • 175
  • 215
0

Thanks to @galkin and @David Maze, I solved it like this:

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>
                    }
            };
ShaneKm
  • 20,823
  • 43
  • 167
  • 296