graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

GraphQLHttpClient only taking POST request to fetch data

Open JoseGeorges8 opened this issue 3 years ago • 1 comments
trafficstars

I've setup a client and made a request like this:

vargraphQLClient = new GraphQLHttpClient($"{ConfigurationManager.ConnectionStrings["mybaseurl"].ConnectionString}/api/graphql", new NewtonsoftJsonSerializer());

var myrequest = new GraphQLRequest {
                Query = @"
                {
                    ...
                }"
            };

var graphQLResponse = await graphQLClient.SendQueryAsync<GraphqlResponseData>(myrequest);

This code fails with a GraphQLHttpRequestException of 404 NotFound when the endpoint is a GET request. However, I switched it to a POST request and it works fine! I don't want to make my endpoint a POST request because I'm clearly fetching data. What can I do to make this client accept a GET request?

JoseGeorges8 avatar Jun 03 '22 14:06 JoseGeorges8

This library sends each GraphQL request as a POST request (see here).

You can create your own request class (which inherits from GraphQLHttpRequest) and override the ToHttpRequestMessage to change that behavior.

Although I would recommend to make your server accept POST requests, too (see GraphQL Best Practices). Since GraphQL normaly only uses one single endpoint for queries and mutations (where data gets modified in some way or the other), the POST method is the natural choice.

GraphQL has its own built-in way to differentiate between "read" and "write" operations, the HTTP protocol is just a transport layer to it.

rose-a avatar Jun 05 '22 19:06 rose-a