【问题标题】:What data type should Variables be when making GraphQL queries/mutations?在进行 GraphQL 查询/突变时,变量应该是什么数据类型?
【发布时间】:2019-11-05 05:55:36
【问题描述】:

如何使用变量执行 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);

【问题讨论】:

    标签: c# graphql


    【解决方案1】:

    找到了解决办法。它应该是匿名类型。像这样:

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

    【讨论】:

    • 因为 Newton.Json 序列化了所有类型的对象。有没有办法使用普通实例而不是匿名对象?
    猜你喜欢
    • 2020-03-12
    • 2019-07-20
    • 2016-01-14
    • 2022-01-13
    • 2017-10-18
    • 1970-01-01
    • 2022-11-25
    • 2019-04-04
    • 2019-11-18
    相关资源
    最近更新 更多