Exploring the API

This section introduces testing GraphQL endpoint queries using tools like GraphiQL and Postman, and details the POST request structure with a “GetCustomers” query example.

For testing, you can run requests to the GraphQL endpoint with your preferred tools. We recommend:

  • GraphiQL - a web-based GraphQL IDE
  • Postman - the widely used collaboration platform for API development, with built-int GraphQL support
  • Insomnia - similar to Postman, also with built-in support for GraphQL
  • Curl - a widely used command line tool for transfering data using a variety of network protocols, including HTTP/HTTPs

The general form of a GraphQL query is as follows:

  • the request is a POST
  • the content type is application/json
  • the body is a JSON with the form shown below, where the value of query is a serialized JSON (properly escaped) and variables is a JSON object. The variables property can be skipped or could be set to null if there are no GraphQL variables in the query.
{
  "query" : "...",
  "variables" : {}
}

The following snippet shows a query example:

{
   "query":"query GetCustomers($companyNo: Int, $pageSize: Int, $after :String)\n{\n  useCompany(no: $companyNo)\n  {\n    associate(first: $pageSize, after: $after)\n    {\n      totalCount\n      pageInfo\n      {\n        hasNextPage\n        hasPreviousPage\n        startCursor\n        endCursor\n      }\n      items\n      {\n        associateNo\n        customerNo\n        name\n      }\n    }\n  }\n}",
   "variables":{"companyNo":9112233,"pageSize":5},
   "operationName":"GetCustomers"
}

In this example, you can also see the property operationName that contains the name of the operation (that follows the query or mutation keyword).

Note

The topic of GraphQL variables is addressed later in the tutorial in section Named queries and parameters.


GraphiQL

Explore and test GraphQL queries using GraphiQL, a web IDE with features like schema auto-fetch, query auto-complete, and documentation exploration.

Postman

Postman supports GraphQL requests with built-in tools or raw POST requests, using application/json content type for queries and variables.

Insomnia

Insomnia offers superior GraphQL support with schema fetching, autocomplete, and an interactive schema explorer for efficient API management.

Making requests from code

Learn how to make programmatic requests in C#/.NET 5 to execute GraphQL queries and deserialize JSON responses.

Last modified September 24, 2024