带有.net客户端的GraphQL

时间:2019-02-19 20:36:26

标签: c# .net graphql github-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
        }
      }
    }

变量:

{
  "input": {
    "email": "user@example.com",
    "password": "HiZqFuDvDdQ7"
  }
}

我的代码:

    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);

1 个答案:

答案 0 :(得分:0)

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

    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
        }

    };