GraphQLBundle is a set of tools designed to easily create API projects based on GraphQL & Relay Specification

Prerequisites

This documentation assumes your familiarity with GraphQL concepts. If it is not the case - first learn about GraphQL on the official website.

Installation

Use composer to add the bundle as a requirement:

composer require ynloultratech/graphql-bundle

Add the bundle in the kernel

$bundles = [
    ...
    new Ynlo\GraphQLBundle\YnloGraphQLBundle(),
    new App\AppBundle(),
 ];

In Symfony4 the bundle is automatically registered after composer installation.

At this point it's almost ready

Configuration

Endpoint Route

GraphQL use one only endpoint for all queries & mutations. To configure it, add the following route in your routing.yml or routes.yaml in symfony4.

api:
  resource: '@YnloGraphQLBundle/Resources/config/routing/root.yml'
  prefix:   /api
  trailing_slash_on_root: false

The trailing_slash_on_root option was introduced in Symfony 4.1.

Now your GraphQL server under the /api route is ready, but does not have any data to serve.

Configure your first object

GraphQL is based on objects, fields and types, is required al least register one object to has something to serve.

Add the following annotation to a doctrine entity

use Doctrine\ORM\Mapping as ORM;
use Ynlo\GraphQLBundle\Annotation as GraphQL;

/**
 * @ORM\Entity()
 * @ORM\Table()
 *
 * @GraphQL\ObjectType()
 */
class User
{
....

The entity must implements Ynlo\GraphQLBundle\Model\NodeInterface.

The node interface is a requirement in order to accomplish with the Relay Specification to create a GlobalID for all nodes. See this documentation to know when is required the use of NodeInterface

Your entity should look like this:


use Doctrine\ORM\Mapping as ORM;
use Ynlo\GraphQLBundle\Annotation as GraphQL;
use Ynlo\GraphQLBundle\Model\NodeInterface;

/**
 * @ORM\Entity()
 * @ORM\Table()
 *
 * @GraphQL\ObjectType()
 */
class User implements NodeInterface
{

Congrats, you have your first GraphQL object ready and you can start using the API now. Read more about creating object types.

Using GraphiQL

Can configure GraphiQL to interact with your API in dev or production environments.

CRUD Operations

Your API is ready but is useless, you need more operations for all objects like, add, update etc..

This bundle has basic CRUD operations integrated for nodes, see CRUD Operations documentation