GraphQL in PHP: webonyx/graphql-php library for creating APIs

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

GraphQL PHP Library: webonyx/graphql-php



In modern web development, the need for flexible and efficient APIs is increasingly common. Traditional REST services often suffer from over-fetching or under-fetching of data. GraphQL is a query language and runtime environment created by Facebook that allows clients to request exactly the data they need. For PHP, one of the most popular implementations is the webonyx/graphql-php library.



This library provides a complete set of tools for building GraphQL servers in PHP: from defining a type schema to processing complex queries with nested relationships. It is ideal for creating modern APIs for web applications, mobile applications, and SPAs (Single Page Applications).



Key Features of webonyx/graphql-php



  • Full support for the GraphQL specification (June 2018) — all types, directives, variables, and fragments.
  • Type system based on PHP objects — defining types, interfaces, union types, and enums.
  • Built-in resolvers — the ability to define data retrieval logic for each field.
  • Query validation — automatic syntax and type checking.
  • Lazy loading support — data is loaded only on demand.
  • Compatibility with popular frameworks — Laravel, Symfony, Yii2 via wrapper packages.


Installation and Setup



Installation is done via Composer:



composer require webonyx/graphql-php


PHP 7.1+ with the mbstring extension is required. After installation, let's create a simple server.



Example: Creating a GraphQL Schema



Let's look at a basic example — fetching a list of books and authors. First, define the types:



<?phpuse GraphQL\\Type\\Definition\\ObjectType;use GraphQL\\Type\\Definition\\Type;use GraphQL\\Type\\Schema;

// Type "Author"$authorType = new ObjectType([ 'name' => 'Author', 'fields' => [ 'id' => Type::int(), 'name' => Type::string(), 'books' => [ 'type' => Type::listOf($bookType), // will be defined below 'resolve' => function ($author) { // Assume there is a function getBooksByAuthorId() return getBooksByAuthorId($author['id']); } ] ]]);

// Type "Book" (defined with a closure for circular references)$bookType = new ObjectType([ 'name' => 'Book', 'fields' => function() use ($authorType) { return [ 'id' => Type::int(), 'title' => Type::string(), 'author' => [ 'type' => $authorType, 'resolve' => function ($book) { return getAuthorById($book['author_id']); } ] ]; }]);

// Type "Query"$queryType = new ObjectType([ 'name' => 'Query', 'fields' => [ 'books' => [ 'type' => Type::listOf($bookType), 'resolve' => function() { return getAllBooks(); } ], 'author' => [ 'type' => $authorType, 'args' => [ 'id' => Type::nonNull(Type::int()) ], 'resolve' => function($root, $args) { return getAuthorById($args['id']); } ] ]]);

// Create the schema$schema = new Schema([ 'query' => $queryType]);


Handling a Query



Now let's write the code that accepts a GraphQL query and returns the result:



<?phpuse GraphQL\\GraphQL;use GraphQL\\Error\\DebugFlag;

// Get the request body from the in

Recommendations