Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Question, handling different response Types #44

Open
SenhorCastor opened this issue Aug 3, 2022 · 1 comment
Open

Question, handling different response Types #44

SenhorCastor opened this issue Aug 3, 2022 · 1 comment

Comments

@SenhorCastor
Copy link

            var client = new GraphQLClient("xxx/api/graphql", null, headers, null);
            var request = new Request
            {
                Query = "query FindAccount($userId: Long!) { findAccount(userId: $userId) { " 
                        +"... on Account { id, type, userId, email, expirationDate, lastSubscriptionReminderDate, isDeveloper }"
                        +"... on NotFound { notFound }" 
                        +"... on BadRequestError { type, message }" 
                        +"... on AppError { type, message} } }",
                    Variables = new
                {
                    userId= this.userId().Value()
                }
            };
            var responseType = new { findAccount = new { type ="" , message =""} } ;
            var response = await client.Send( () => responseType, request);

How do I properly work on query that can return multiple response Types?

Thanks in advance
Fer

@NavidK0
Copy link
Owner

NavidK0 commented Jan 1, 2023

Apologies for the super late reply.

There are several ways to handle this, but in your more complex use case, I recommend you receive the payload as a string using the non-generic method and then check your fields in the JSON response to figure out what to parse it to (similar to TypeScript).

The generic methods are there for convenience, but to support multiple types from the API, the easiest way is to parse the response string yourself.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants