GraphQL tools for getting started with Contentful
![New to using GraphQL with Contentful? Here's a handy cheatsheet to get you started, highlighting the most commonly used GraphQL tools and features.](https://images.ctfassets.net/fo9twyrwpveg/40ZE7qxEOlqByuiAoZcaNd/07420ea12803908a19e62b332dd9539e/2022-12_GraphQL_tools.png?fm=jpg&q=90&w=1536)
If you’re new to using GraphQL with Contentful, we have put together a handy cheat sheet to get you started. In this guide, we highlight the most commonly used GraphQL tools and features.Â
First things first, GraphQL is a query language that provides developers with the flexibility to retrieve data from APIs. In particular, the GraphQL Content API supports our Content Delivery API (CDA) and our Content Preview API (CPA).Â
A GraphQL schema comes with every Contentful space, which is based on the query language’s content types. You can find out more information on endpoint filtering capabilities, schema generation process, and error codes in the reference guide.
There are some fantastic educational resources available, which you can check out below:
With all this in mind, let’s dive into the need-to-knows.
GraphQL tools and access
There are many tools that allow you to use GraphQL with Contentful. Here's a selection to get you started.
1. Basic Contentful GraphQL API
Typically when you are using the GraphQL API in your code, you will use the following URL to request content:
https://graphql.contentful.com/content/v1/spaces/[SPACE_ID].Â
The content you receive from this URL is the same as the content you would receive when using the Contentful Delivery API.Â
2. GraphiQL tool
You also may use GraphiQL, an IDE tool by going to this URL:
 https://graphql.contentful.com/content/v1/spaces/[SPACE_ID]/explore?access_token=[CDA_API_TOKEN].Â
This tool allows you to build GraphQL queries easily in the browser.Â
![GraphQL](https://images.ctfassets.net/fo9twyrwpveg/MKgsSbmV2gohIMvEaPHW4/7c88cbd013d2a7dab340edbabd6b428c/unnamed__18_.png)
You can access your space ID and Content Delivery API token in your Contentful space settings.
3. GraphQL Playground App
Within Contentful, we also offer the GraphQL Playground App. It is similar to GraphiQL in that it allows you to build GraphQL queries, but the main benefit is that it allows you to do so within the Contentful web app and also automatically will connect to your Contentful space so there is no need to insert your API token or Contentful space ID.
![Playground App](https://images.ctfassets.net/fo9twyrwpveg/3ZG12V1qymHQIflzqIWCO5/2efc9478ed0d9ebb5441acead19767b0/unnamed__19_.png)
Contentful GraphQL queries
Keep in mind that in order to access draft content, you'll need to use your Contentful Preview API. Here are some of the most used Contentful GraphQL queries you’ll want to know.
Single entry query
Here’s how to retrieve a single entry.
Which returns the following:
Collection query
This is how you can retrieve multiple entries.
Which returns the following:
Preview query
Finally, use the following code, should you wish to include draft entries.
Reference queries
For single reference queries; query a reference field that only accepts a specific entry type.
Which returns the following:
When a reference field is created in a content type, there is the option to limit what types of entries can be referenced. The screenshot below is an example of what the validation would look like for a field where this query should be used.Â
![GraphQL](https://images.ctfassets.net/fo9twyrwpveg/24UhHbE0M9ZgUhIPYNLa8s/28e1d7ad3a93294066256256db3608a6/unnamed__24_.png)
Various reference types
Query a reference field that accepts multiple entry types.Â
In the reference field validation, if multiple entry types are selected as shown below, in your query, you must specify which entry type you would like to return.Â
![GraphQL](https://images.ctfassets.net/fo9twyrwpveg/7FImlMXlDAeR4fsD6UuXTg/c400d65ee06b78137717133c7bd714f5/unnamed__25_.png)
For queries that have many references, it’s possible that you might hit query complexity limits. To avoid this, you can add limits to your queries to lower the complexity cost.
GraphQL basics
Reference queries
You can reuse queries by passing down variables. In this example, this query might be used on a blog to show all of the posts for a certain author. Instead of writing a new query for every author, the same query can be used as the author’s name changes.Â
Step 1: Define variable
Step 2: Use variable in query
Named queries
You can provide unique query names to organize multiple queries.
Aliases
You can provide alternative names, or aliases, for query results to avoid conflicts when fetching from the same field or collection.
Fragments
These are reusable field groups that can be shared between multiple fields and queries.
Which returns the following:
Directives
You can include or skip a field in a query based on a variable value.Â
Step 1: Create variable
Step 2: Use variable with directives
Request
Here’s an example of how to request data using GraphQL with JavaScript.
Variables
You can create dynamic queries and add type safety.
Step 1: Define variables
Step 2: Use variable in a query
Step 3: Use variable in an API request
You can check out our blog for a detailed walkthrough of how to utilize variables in GraphQL in requests.
Wrapping up
Visit our developer portal for even more guides, tips and tricks. The portal also houses developer blog posts, videos, and tutorials.
Check out our Developer Showcase to see what developers have built with Contentful and to submit your own projects.
As always, feel free to contact us on Slack or Twitter to let us know how we can better support your awesome builds.