Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data type should Variables be when making GraphQL queries/mutations?

Tags:

c#

graphql

How would one execute a graphQL query/mutation with variables? I'm using this client: https://github.com/graphql-dotnet/graphql-client

I'm trying to perform the following:

https://help.shopify.com/en/api/custom-storefronts/storefront-api/guides/updating-customers#creating-the-customer

mutation customerCreate($input: CustomerCreateInput!) {
      customerCreate(input: $input) {
        userErrors {
          field
          message
        }
        customer {
          id
        }
      }
    }

Variables:

{
  "input": {
    "email": "[email protected]",
    "password": "HiZqFuDvDdQ7"
  }
}

my code:

    var request = new GraphQLRequest();
    request.Query = @"mutation customerCreate($input: CustomerCreateInput!) {
          customerCreate(input: $input) {
            userErrors {
              field
              message
            }
            customer {
              id
            }
          }
        }";

    request.OperationName = "customerCreate";
    request.Variables = new
    {
        email = "[email protected]",
        password = "HiZqFuDvDdQ7"
    };

    var client = new GraphQLClient("https://kitkatco.myshopify.com/api/graphql");
    client.DefaultRequestHeaders.Add("X-Shopify-Storefront-Access-Token", new List<string> { "d021132503b1357116d5f1e51abd9f39" });
    var response = await client.PostAsync(request);
like image 879
ShaneKm Avatar asked Apr 01 '26 08:04

ShaneKm


1 Answers

Found the solution. It is supposed to be an anonymous type. Like so:

    var v = new { email = "[email protected]", password = "fdsafsdfsdf" };
    var request = new GraphQLRequest
    {
        Query = @"mutation customerCreate($input: CustomerCreateInput!) {
          customerCreate(input: $input) {
            userErrors {
              field
              message
            }
            customer {
              id
            }
          }
        }",
        Variables = new
        {
            input = v
        }

    };
like image 66
ShaneKm Avatar answered Apr 02 '26 20:04

ShaneKm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!