This factory creates PSR-7 GraphQL Request through PSR-17 message factories that you can be passed to PSR-18 client.
Add as Composer dependency:
composer require simpod/graphql-request-factory
<?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
.
You can pass it to any HTTP client supporting PSR-7. It's up to you what client you decide to use.
http://docs.guzzlephp.org/en/stable/quickstart.html#sending-requests
$response = $client->send($request);
https://www.php-fig.org/psr/psr-18/#clientinterface
$response = $client->sendRequest($request);