Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

GraphQL: Automatically Generated, an Overview

GraphQL: Automatically Generated, an Overview

~13 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026

ALONGSIDE REST, API Platform generates a COMPLETE GraphQL endpoint FROM THE SAME #[ApiResource] attributes – WITHOUT a SINGLE additional line of code.

Enabling GraphQL

docker compose exec php composer require webonyx/graphql-php

The api-platform distribution often comes with this package ALREADY installed – if NOT, the command installs it AFTERWARD. AFTERWARD, /api/graphql is IMMEDIATELY available, for EVERY existing resource.

Exploring GraphiQL

Opening /api/graphql in the browser shows GraphiQL – EXACTLY the GraphQL equivalent of Swagger UI (chapter 6), WITH an interactive query editor and automatic schema documentation.

A first query

query {
  projects(first: 5) {
    edges {
      node {
        id
        name
        tasks {
          totalCount
        }
      }
    }
  }
}

A SINGLE request returns projects TOGETHER WITH their task COUNT – with REST, this would have needed EITHER multiple requests OR a purpose-built DTO (chapter 59). THAT is the CLASSIC GraphQL advantage: the CLIENT decides WHICH fields it needs.

Filters and security still apply

SearchFilter/OrderFilter (block 4), security attributes (block 6), AND custom voters WORK in GraphQL JUST like in REST – the SAME metadata configuration controls BOTH protocols SIMULTANEOUSLY.

Mutations for write operations

mutation {
  createProject(input: { name: "New project via GraphQL" }) {
    project {
      id
      name
    }
  }
}

GraphQL mutations correspond to the POST/PUT/PATCH/DELETE operations from REST – the SAME validation (block 3) applies, errors appear as an errors array in the GraphQL response instead of an HTTP status code.

Achtung: THIS course DELIBERATELY focuses on REST, since the React frontend (blocks 9-10) consumes REST – GraphQL is presented here ONLY as an EXISTING possibility, NOT explored in depth. Anyone wanting to use GraphQL in the frontend would use @apollo/client instead of axios.

Tipp: GraphQL AND REST can stay active SIMULTANEOUSLY, for the SAME resources – some clients (e.g. a mobile app with limited bandwidth) benefit from GraphQL's precise field selection, others (e.g. simple integrations) prefer the SIMPLICITY of REST.