# Getting Started with GraphQL

✋ CAUTION

This third-party integration guide might not be up-to-date with Strapi v4. Contributions (opens new window) are most welcome.

This integration guide is following the Quick Start Guide. We assume that you have fully completed its "Hands-on" path, and therefore can consume the API by browsing this url (opens new window).

If you haven't gone through the Quick Start Guide, the way you request a Strapi API with GraphQL (opens new window) remains the same except that you will not fetch the same content.

# Install the GraphQL plugin

Install the GraphQL plugin in your Strapi project.

npm run strapi install graphql
yarn strapi install graphql
strapi install graphql

# Fetch your Restaurant collection type

Play with the GraphQL Playground (opens new window) to fetch your content.

Example query

query Restaurants {
  restaurants {
    id
    name
    description
    categories {
      name
    }
  }
}
Copied to clipboard!

Example response

{
  "data": {
    "restaurants": [
      {
        "id": "1",
        "name": "Biscotte Restaurant",
        "description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.",
        "categories": [
          {
            "name": "French Food"
          }
        ]
      }
    ]
  }
}
Copied to clipboard!

# Examples

These examples do not guide you to configure your client with Apollo for your GraphQL endpoint (opens new window). Please follow the associated documentation for each client: (React (opens new window) and Vue.js (opens new window) here)

Using React and Apollo (opens new window)

import { gql, useQuery } from '@apollo/client';

function Restaurants() {
  const { loading, error, data } = useQuery(gql`
    query Restaurants {
      restaurants {
        id
        name
        description
        categories {
          name
        }
      }
    }
  `);

  if (loading) return 'Loading...';
  if (error) return `Error! ${error.message}`;

  return (
    <ul>
      {data.restaurants.map(restaurant => (
        <li key={restaurant.id}>{restaurant.name}</li>
      ))}
    </ul>
  );
}
Copied to clipboard!

# Fetch your Category collection type

Example request

query Category {
  category(id: 1) {
    id
    name
    restaurants {
      id
      name
      description
    }
  }
}
Copied to clipboard!

Example response

{
  "data": {
    "category": {
      "id": "1",
      "name": "French Food",
      "restaurants": [
        {
          "id": "1",
          "name": "Biscotte Restaurant",
          "description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers."
        }
      ]
    }
  }
}
Copied to clipboard!

# Examples

Using React and Apollo (opens new window)

import { gql, useQuery } from '@apollo/client';

function Category({ id }) {
  const { loading, error, data } = useQuery(
    gql`
      query Category($id: ID!) {
        category(id: $id) {
          id
          name
          restaurants {
            id
            name
            description
          }
        }
      }
    `,
    { variables: { id } }
  );

  if (loading) return 'Loading...';
  if (error) return `Error! ${error.message}`;

  return (
    <div>
      <h1>{data.category.name}</h1>
      <ul>
        {data.category.restaurants.map(restaurant => (
          <li key={restaurant.id}>{restaurant.name}</li>
        ))}
      </ul>
    </div>
  );
}
Copied to clipboard!

# Conclusion

This is how you request your collection types in Strapi using GraphQL.

Feel free to explore more about GraphQL.