Home > Software engineering >  Custom response type in GraphQL .NET
Custom response type in GraphQL .NET

Time:01-29

I have a service which will now consume a GraphQL API just to return an image URL for a front-end application.

This is my first time using GraphQL at all, and this is the query structure I must use:

var request = new GraphQLRequest
        {
            Query = @"
            query($status: Status = PUBLISHED, $path: String){
                brand (status: $status, where: { path: $path }, first: 1){ 
                    card { 
                        contentItems { 
                            ... on Cartao { 
                                imagemFrente { 
                                    urls
                                } 
                            } 
                        } 
                    } 
                } 
            }",
            Variables = new
            {
                path = "[EXAMPLE-PATH]"
            }
        };

The problem I'm struggling with is how can my class ReturnType must be so I can access the urls property from the request down below?

var graphQLResponse = Task.Run(async () => await graphQLClient.SendQueryAsync<ReturnType>(request));

I'm using the GraphQLClient package only.

CodePudding user response:

Run your query somewhere where you can grab the json result, copy it into the clipboard, then in VS, Edit/Paste Special/Paste JSON as classes.

Or use on of the many JSON to C# converters online, such as https://json2csharp.com/

That will give you the C# classes ready to use in your SendQueryAsync<>.

  •  Tags:  
  • Related