simpod/graphql-request-factory

This package is abandoned and no longer maintained. The author suggests using the https://github.com/spawnia/sailor package instead.

Factory to create PSR-7 compatible request to GraphQL server

Fund package maintenance!
simPod

v0.1.2 2021-01-18 15:38 UTC

This package is auto-updated.

Last update: 2021-08-03 13:37:43 UTC


README

⚠️ Abandoned in favor of spawnia/sailor (since this PR introduces PSR-18 support in v0.9.0).

PHP GraphQL Request Factory

GitHub Actions Shepherd Type Code Coverage Downloads Packagist Infection MSI

This factory creates PSR-7 GraphQL Request through PSR-17 message factories that you can be passed to PSR-18 client.

Installation

Add as Composer dependency:

composer require simpod/graphql-request-factory

Usage

<?php

declare(strict_types=1);

namespace YourNs;

use SimPod\GraphQLRequestFactory\GraphQLRequestFactory;

$requestFactory = new GraphQLRequestFactory(new RequestFactoryInterfaceImpl(), new StreamFactoryInterfaceImpl());

$request = $requestFactory->createRequest(
    'https://localhost/graphql',
    <<<'GRAPHQL'
query GetHuman($id: ID!) {
  human(id: $id) {
    name
    appearsIn
    starships {
      name
    }
  }
}
GRAPHQL,
    ['id' => 1]
);

You can pass $query either as a string or PSR-7 StreamInterface.

Mkay, but what should I do with the request then?

You can pass it to any HTTP client supporting PSR-7. It's up to you what client you decide to use.

Guzzle

http://docs.guzzlephp.org/en/stable/quickstart.html#sending-requests

$response = $client->send($request);

PSR-18 Client (eg. HTTPPlug)

https://www.php-fig.org/psr/psr-18/#clientinterface

$response = $client->sendRequest($request);