Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

GraphQL with PHP and the Symfony Framework

GraphQL is a data query language developed by Facebook since 2012. In September 2015 Facebook released it to the public domain. GraphQL is essentially an alternative to REST and despite the name, it's not a Graph Database Query language like Cypher from the Neo4j project.

The origins of GraphQL stem from the needs that Facebook's mobile applications had (and continue to have). They needed a data-fetching API that was flexible enough to describe all the different kinds of data that the social network had available.

Back in September 2015 GraphQL was already powering Billions of API calls a day at Facebook. Even then it was already being used by other web properties, such as the Financial Times: GraphQL at The Financial Times

The core idea of GraphQL is to send a simple string to the server. This string is then interpreted by the server and it sends back a JSON payload that responds to follows the structure of the query itself. This is best illustrated by displaying an example query and response side-by-side:Thumbnail

This protocol can be used by a number of backends ranging from custom built GraphQL backends to content management systems that expose their own content structure via a GraphQL interpretation layer. GraphQL enables both reads (known as Queries) as well as writes (known as Mutations) to the data source.

Together with opening the GraphQL specification, Facebook also provided a reference implementation written in JavaScript. Since that there has been quite a bit of work done around GraphQL, including a number of projects written in PHP:

Symfony Framework GraphQL Integration Bundles

As you can see from the list above there are a number of options for working with GraphQL an Symfony. You can either start working with these libraries or start working with a Symfony framework compatible bundle that will give you integration and handle some of the basics.

Before writing your own GraphQL implementation or a Symfony integration bundle to an existing one, it's worth taking a look if there is something you could use and contribute to. Currently there are at least three solid alternatives available for Symfony developers:

  • Subirit GraphQLBundle
    A port of the Laravel implementation. Limited documentation and, little recent development activity.
  • Overblog GraphQLBundle
    Tools to build a complete GraphQL server in your Symfony App. Built by Overblog a French blogging platform. Comprehensive documentation and decent development activity.
  • Youshido GraphQLBundle
    An integration bundle to the pure PHP GraphQL Server implementation from Youshido, a web development shop. Superb documentation and active development both on the protocol implementation as well as this integration Bundle.

GraphQL is still quite a young technology, but it's straightforward for developers to use. It already runs in production on a large scale and looks like it's got staying power. In addition it can unlock developers to work across different systems better than with the current crop of proprietary REST API implementations:

While it's early days for GraphQL, it could hold prospect for being a universal query language for Graph Databases, such as SQL (Structured Query Language) has been for Relational Databases. Having a common lingua franca for querying Content Graphs in CMSes, for example, would unlock new possibilities in the long term.
- GraphQL is SQL for Knowledge, not Data

Learn more about GraphQL:


Written by Jani Tarvainen on Saturday May 14, 2016
Permalink - Tags: symfony, graphql, php, facebook

« Consider using a JSON Schema powered JavaScript CRUD in your applications - Spotlight: PHP 7.1 and Symfony 3.1 »