Giter Site home page Giter Site logo

apollo-federation-php's Introduction

Apollo Federation PHP

This package provides classes and utilities for webonyx/graphql-php for creating federated GraphQL subgraphs in PHP to be consumed by the Apollo Gateway.

โš ๏ธ IMPORTANT: This package is still in active development and it might introduce breaking changes.

Usage

Via composer:

composer require skillshare/apollo-federation-php

Entities

An entity is an object type that you define canonically in one subgraph and can then reference and extend in other subgraphs. It can be defined via the EntityObjectType which takes the same configuration as the default ObjectType plus a keyFields and __resolveReference properties.

use Apollo\Federation\Types\EntityObjectType;

$userType = new EntityObjectType([
    'name' => 'User',
    'keyFields' => ['id', 'email'],
    'fields' => [
        'id' => ['type' => Type::int()],
        'email' => ['type' => Type::string()],
        'firstName' => ['type' => Type::string()],
        'lastName' => ['type' => Type::string()]
    ],
    '__resolveReference' => static function ($ref) {
        // .. fetch from a data source.
    }
]);
  • keyFields โ€” defines the entity's primary key, which consists of one or more of the type's. An entity's key cannot include fields that return a union or interface.

  • __resolveReference โ€” resolves the representation of the entity from the provided reference. Subgraphs use representations to reference entities from other subgraphs. A representation requires only an explicit __typename definition and values for the entity's primary key fields.

For more detail on entities, see the official docs.

Entity references

A subgraph can reference entities from another subgraph by defining a stub including just enough information to know how to interact with the referenced entity. Entity references are created via the EntityRefObjectType which takes the same configuration as the base EntityObjectType.

use Apollo\Federation\Types\EntityRefObjectType;

$userType = new EntityRefObjectType([
    'name' => 'User',
    'keyFields' => ['id', 'email'],
    'fields' => [
        'id' => ['type' => Type::int()],
        'email' => ['type' => Type::string()]
    ]
]);

For more detail on entity references, see the official docs.

Extending

A subgraph can add fields to an entity that's defined in another subgraph. This is called extending the entity. When a subgraph extends an entity, the entity's originating subgraph is not aware of the added fields. Only the extending subgraph (along with the gateway) knows about these fields.

use Apollo\Federation\Types\EntityRefObjectType;

$userType = new EntityRefObjectType([
    'name' => 'User',
    'keyFields' => ['id', 'email'],
    'fields' => [
        'id' => [
            'type' => Type::int(),
            'isExternal' => true
        ],
        'email' => [
            'type' => Type::string(),
            'isExternal' => true
        ]
    ]
]);

The subgraph can extend using the following configuration properties:

  • isExternal โ€” marks a field as owned by another service. This allows service A to use fields from service B while also knowing at runtime the types of that field.

  • provides โ€” used to annotate the expected returned fieldset from a field on a base type that is guaranteed to be selectable by the gateway.

  • requires โ€” used to annotate the required input fieldset from a base type for a resolver. It is used to develop a query plan where the required fields may not be needed by the client, but the service may need additional information from other services.

Federated schema

The FederatedSchema class extends from the base GraphQL\Schema class and augments a schema configuration using entity types and federated field annotations with Apollo Federation metadata. See the docs for more info.

use GraphQL\GraphQL;
use Apollo\Federation\FederatedSchema;


$schema = new FederatedSchema($config);
$query = 'query GetServiceSDL { _service { sdl } }';

$result = GraphQL::executeQuery($schema, $query);

Disclaimer

Documentation in this project include content quoted directly from the Apollo official documentation to reduce redundancy.

apollo-federation-php's People

Contributors

ldiego08 avatar emfrouin avatar jeffreybaird avatar jmauerhan avatar maccath avatar tarekul avatar xiian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.