3

Is it possible to use flurl to send a post request to a GraphQL API?

I've tried the following, but I'm only getting errors back.

var stringContent = @"query {
                webhooks {
                    data {
                        id
                        name
                        topic
                        source
                        address
                        enabled
                        hashKey
                    }
                }
            }";

HttpContent content = new StringContent(stringContent, Encoding.UTF8, "application/graphql");

try
        {
            var test = await GRAPHQL_API_URL
                .WithOAuthBearerToken(ACCESS_TOKEN)
                .WithHeader("Content-Type", "application/graphql")
                .PostAsync(content).ReceiveJson();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

2 Answers2

0

The API provider did something, and now the requests are accepted

0

GraphQL queries are now a lot easier with Flurl using the extensions that I've published in the FlurlGraphQL library which allows the original question to be implemented easily as:

Assuming the GraphQL server is spec compliant for Http Request payload.

var json = await GRAPHQL_API_URL
    .WithOAuthBearerToken(ACCESS_TOKEN)
    .WithGraphQLQuery(@"
        query {
            webhooks {
                data {
                    id
                    name
                    topic
                    source
                    address
                    enabled
                    hashKey
                }
            }
        }
    ")
    .PostGraphQLQueryAsync()
    .ReceiveGraphQLRawJsonResponse();

But you don't have to retrieve raw Json, you can de-serialize into your model and the library significantly simplifies handling even complex elements such as Edges, Nodes, etc.

CajunCoding
  • 720
  • 5
  • 9