GraphQL client query for object list and variables

0

I can't handle receiving data from one query and adding parameters to it.

My class:

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

An example that works for me:

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;

I can cast it on my class and everything seems fine.

An example that doesn't work for me:

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

I don't quite understand what is being returned. I got an error:

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'.

I thought an array of objects would be returned, but JSON says otherwise.

I was looking for client-side edges and node documentation, but I can't find it.

This is the result obtained on Insomnia: enter image description here

#

The second problem is that I can't add "" ean "' in query, which is visible in the screenshot from insomnia. I've tried:

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

But got error:

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

I checked:

https://github.com/StephenCleary/AsyncEx

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

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

EDIT

Okey, I've fixed it.

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

The new class, in line with the customer documentation that I was able to find!

However, I am still trying to pass parameters to the query

c#
graphql
asked on Stack Overflow Dec 17, 2019 by Walczak Jakub • edited Dec 17, 2019 by Walczak Jakub

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0