overblog / graphql-subscription

GraphQL native subscription.
MIT License
14 stars 1 forks source link

OverblogGraphQLSubscription

This library allow using GraphQL subscription over Mercure protocol with any implementation of GraphQL PHP. It Comes out-of-the-box with a Symfony Bridge so it can be easily combine with OverblogGraphQLBundle or API Platform or other Symfony implementation based on GraphQL PHP.

Installation

composer req overblog/graphql-subscription

Default builder executor

<?php
use GraphQL\Executor\ExecutionResult;
use GraphQL\GraphQL;

function (
    $schema,
    $source,
    $rootValue = null,
    $context = null,
    $variableValues = null,
    ?string $operationName = null
): ExecutionResult {
    return GraphQL::executeQuery(...func_get_args());
}

CORS preflight headers

This library does not handle natively CORS preflight headers.

Symfony

Installation without flex

Add the OverblogGraphQLSubscriptionBundle to your application's kernel:

    public function registerBundles()
    {
        $bundles = [
            // ...
            new Overblog\GraphQLSubscription\Bridge\Symfony\OverblogGraphQLSubscriptionBundle(),
            // ...
        ];
        // ...
    }

Configuration

Symfony Flex generates:

Handling CORS preflight headers

NelmioCorsBundle is recommended to manage CORS preflight, follow instructions to install it.

Here a configuration assuming that subscription endpoint is /subscriptions:

nelmio_cors:
    defaults:
        origin_regex: true
        allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
        allow_methods: ['GET', 'OPTIONS', 'POST']
        allow_headers: ['Content-Type']
        max_age: 3600
    paths:
        '^/subscriptions': ~