PHP code example of goltzchristian / graphql-client-php

1. Go to this page and download the library: Download goltzchristian/graphql-client-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

goltzchristian / graphql-client-php example snippets

 composer 
 
use GraphQLClient\Client;

abstract class TestCase extends \Laravel\Lumen\Testing\TestCase
{
  /** @var Client */
  protected $graphql;
  
  public function setUp()
  {
    parent::setUp();
    $this->graphql = new LaravelTestGraphQLClient(
      $this->app,
      '/graphql'
    );
  }
  
}

public function testRegistrationSuccess()
{
  $params = [
    'secret' => 'mySecret',
    'password' => $this->getFaker()->password()
    'email' => $this->getFaker()->email()
  ];  // build a query
  $query = new Query('registerUser', $params, [
    new Field('id'),
    new Field('email'),
    new Field('id'),
  ]);  // execute the query
  $fields = $this->graphql->mutate($query)->getData();  // check if the server returned values for all requested fields
  $this->graphql->assertGraphQlFields($fields, $query);  // check if the user was created in the db
  $user = User::query()->where([
    'email' => $params['email'],
  ])->first();
  $this->assertNotNull($user);
}



public function testReadProfile()
{
  // build the query
  $query = new Query('viewer', [ 'token' => 'myToken', [
    new Field('profile', [
      new Field('id'),
      new Field('email'),
      new Query('posts', [ 'first' => 10 ], [
        new Field('edges', [
          new Field('node', [
            new Field('id'),
            new Field('content')
          ])
        ])
      ])
    ])
  ]);  // execute the query
  $fields = $this->graphql->query($query)->getData();  // check if the server returned all requested fields
  $this->assertGraphQlFields($fields, $query);  // check some simple field
  $this->assertEquals(
    $this->getUser()->getId(),
    $result['profile']['id']
  );  // retrieve post ids from response
  $postIds = array_map(function(array $item) {
    return $item['node']['id'];
  }, $fields['profile']['posts']['edges']);  // check if all user posts were contained in the response
  foreach ($this->getUser()->getPosts() as $post) {
    $this->assertNotFalse(array_search($post->getId(), $postIds));
  }