asp.net-coregraphqlgraphqlclient

Can I send GraphQL queries with an httpclient in .NET core?


Is it possible to send graphQL queries with the standard httpclient in .NET core? When I try to send my query with a client.post I get "Expected { or [ as first syntax token."

How can I send GraphQL queries with a httpclient. Without having to use a library (like GraphQLHttpClient etc..)


Solution

  • Got it: Just add "query" as a json object. Like this:

    {"query" : "query { __schema { queryType { name } mutationType { name } types { name } directives { name } } }"}
    

    In .NET you can use this in an HTTP post (don't forget to string escape the double quotes

    private static string myquery = "{ \"query\" : \"query { __schema { queryType { name } mutationType { name } types { name } directives { name } } }\" }";