graphqlgraphql explorergraphql queryapigraphql vs rest

GraphQL Explained: How to Query and Explore a GraphQL API

GraphQL is an alternative to REST that lets clients request exactly the data they need. Learn the basics of GraphQL queries, mutations, and how to explore any GraphQL API.

10 min read

Related Tool

GraphQL Explorer

Open tool

GraphQL is a query language for APIs and a runtime for executing those queries. It was developed by Facebook in 2012 and open-sourced in 2015. GraphQL gives clients precise control over what data they receive, eliminates the over-fetching and under-fetching problems common with REST APIs, and provides a strongly typed schema that serves as both documentation and validation.

How GraphQL Differs from REST

In a REST API, the server defines the endpoints and the data structure each endpoint returns. The client requests a URL and gets whatever the server sends. If the client needs only two fields from a 20-field response, it still downloads all 20 fields.

In GraphQL, there is a single endpoint. The client sends a query that specifies exactly which fields it needs. The server returns exactly those fields, nothing more. This eliminates over-fetching.

REST APIs often require multiple round trips to assemble a complete view (a request for a user, then a request for their orders, then requests for the order items). GraphQL resolves nested data in a single request. This eliminates under-fetching.

The Three Operation Types

Queries retrieve data. A query specifies the type of data to retrieve and the fields to include. Queries can be nested to retrieve related data in a single round trip.

Mutations create, update, or delete data. They are the GraphQL equivalent of POST, PUT, PATCH, and DELETE in REST. Like queries, mutations return a specified set of fields.

Subscriptions are long-lived real-time connections that push updates from the server to the client when data changes. Subscriptions are implemented over WebSockets.

The Schema

Every GraphQL API has a schema that defines the available types, queries, and mutations. The schema is strongly typed: every field has a declared type. This schema serves as the contract between the client and server and as the documentation for the API.

GraphQL APIs support introspection: a client can query the schema itself to discover all available types and fields. This is what powers GraphQL tooling and explorers.

Using the DevHexLab GraphQL Explorer

Open the tool at /tools/network/graphql-explorer. Enter the URL of a GraphQL endpoint and any required authorization headers. Write your query in the editor. Press Ctrl + Space for autocomplete suggestions from the schema. Click Run to execute the query and see the response.

Use the documentation panel (populated automatically from schema introspection) to explore available types, queries, and mutations.

Frequently Asked Questions

Does GraphQL replace REST?

Not necessarily. GraphQL excels for complex, interconnected data and client-driven queries. REST is simpler for straightforward CRUD operations and is more universally understood. Many large companies use both.

Is GraphQL slower than REST?

GraphQL adds some overhead for query parsing and field resolution. However, the elimination of over-fetching and multiple round trips often makes the net result faster from the client's perspective.

What is N+1 problem in GraphQL?

When a query resolves a list of objects and each object requires a separate database lookup for a nested field, the resolver makes N+1 database queries (one for the list and one per item). The DataLoader pattern batches these into a single query.

Query exactly what you need and GraphQL delivers exactly that.