GraphQL usage with REST Endpoints in .NET Core 6.0

152 views Asked by At

Since I am new to GraphQL, I don't really know how to call a REST endpoint and query the fields accordingly. In particular, I want to fetch certain fields from the entire response of a GET API.

What I have learnt is GraphQL sends a POST request. How do I hit the GET endpoints ?

PS: we cannot use Apollo client in the project.

1

There are 1 answers

1
Panagiotis Kanavos On

While trying to find the docs that say GraphQL works with POST, I found the docs that show it can work with GET too, if you pass the entire query in the query query string parameter. The Serving over HTTP - GET Request section shows that this query :

{
  me {
    name
  }
}

Can be sent using GET with :

http://myapi/graphql?query={me{name}}

Variables and operation names can be passed as additional parameters:

Query variables can be sent as a JSON-encoded string in an additional query parameter called variables. If the query contains several named operations, an operationName query parameter can be used to control which one should be executed.

To test this, I created a new minimal GraphQL services using HotChocolate's Getting Started example and tried this in a new browser tab:

http://localhost:5242/graphql?query={book{title}}

I got back a valid GraphQL response:

{
    "data": {
        "book": {
            "title": "C# in depth."
        }
    }
}