进行 GraphQL 查询/变更时变量应该是什么数据类型?

Sha*_*eKm 3 c# graphql

如何使用变量执行 graphQL 查询/变异?我正在使用这个客户端: https: //github.com/graphql-dotnet/graphql-client

我正在尝试执行以下操作:

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
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

变量:

{
  "input": {
    "email": "user@example.com",
    "password": "HiZqFuDvDdQ7"
  }
}
Run Code Online (Sandbox Code Playgroud)

我的代码:

    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 = "user@example.com",
        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);
Run Code Online (Sandbox Code Playgroud)

Sha*_*eKm 10

找到了解决方案。它应该是匿名类型。就像这样:

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

    };
Run Code Online (Sandbox Code Playgroud)