simpod/graphql-request-factory

Factory to create PSR-7 compatible request to GraphQL server

Fund package maintenance!
simPod

0.1.5 2023-10-07 12:01 UTC

This package is auto-updated.

Last update: 2024-04-23 21:51:12 UTC


README

GitHub Actions 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);