GraphQL客户端查询对象列表和变量

时间:2019-12-17 10:54:52

标签: c# graphql

我无法处理从一个查询接收数据并向其添加参数的情况。

我的课:

 public class DistributorProduct
    {
        public string id { get; set; }
        public string ean { get; set; }
        public int salesMultiple { get; set; }
        public int moq { get; set; }
        public float price { get; set; }
        public int stock { get; set; }
    }

一个对我有用的例子:

var request2 = new GraphQLRequest
            {
                Query = @"
              query {
                    getDistributorProduct( id: 110822 ) {
                    id
                    price
                      ean
                      price
                    }
                }"
            };

            var graphQLClient = new GraphQLClient("####");
            var graphQLResponse = await graphQLClient.PostAsync(request2);
            var responseType = graphQLResponse.GetDataFieldAs<DistributorProduct>("getDistributorProduct");
            var ean = responseType.ean;

我可以将其投放到班上,一切似乎都很好。

一个不适合我的示例:

 var request1 = new GraphQLRequest
            {
                Query = @"
                     query {
                        getDistributorProductListing {
                        totalCount
                            edges {
                                node {
                                    id
                                    ean
                                    }
                                  }
                        }
                    }"
            };

            var graphQLClient = new GraphQLClient("###");
            var graphQLResponse = await graphQLClient.PostAsync(request1);
            var responseType = graphQLResponse.GetDataFieldAs<DistributorProduct[]>("getDistributorProductListing"); 

            Console.WriteLine(responseType.Length.ToString());

我不太了解所返回的内容。 我遇到错误:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type '###.Data.DistributorProduct[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'getDistributorProductListing.totalCount'.)
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at ###.Program.Main(String[] args) in C:\###\Program.cs:line 20

Inner Exception 1:
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type '###.Data.DistributorProduct[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'getDistributorProductListing.totalCount'.

我认为将返回对象数组,但JSON则相反。

我一直在寻找客户端边缘和节点文档,但找不到。

这是失眠症的结果: enter image description here

第二个问题是我无法在查询中添加“” ean“”,这在失眠的屏幕快照中可见。 我尝试过:

 Query = @"
              query MojeQuery($ean: ean) {
getDistributorProductListing( sortBy: ean  ) {
totalCount
edges {
                    node {
                    id
ean
                }
            }
}
    }",
                Variables = {
                    ean = "\"ean\""
                }

但是出现错误:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (Cannot perform runtime binding on a null reference)
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at ###.Program.Main(String[] args) in C:\###\Program.cs:line 20

Inner Exception 1:
RuntimeBinderException: Cannot perform runtime binding on a null reference

我检查了:

https://github.com/StephenCleary/AsyncEx

https://code-maze.com/consume-graphql-api-with-asp-net-core/

https://github.com/graphql-dotnet/graphql-client

编辑

Okey,我已修复它。

  var responseType = graphQLResponse.GetDataFieldAs<DistributorProductConnection>("getDistributorProductListing");

新类,与我能够找到的客户文档一致!

但是,我仍在尝试将参数传递给查询

0 个答案:

没有答案