Giter Club home page Giter Club logo

qollabi-phx-graphql-client's Introduction

GraphQL Client for PHP

A client made for testing and consuming GraphQL endpoints

Targeting Symfony and Laravel GraphQL implementations

Installation

composer require goltzchristian/graphql-client-php

Setup

Laravel

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'
    );
  }
  
}

Symfony

class RegistrationTest extends TestCase
{
  /** @var Client */
  protected $graphql;
  
  public function setUp()
  {
    parent::setUp();
    $this->graphql = new \GraphQLClient\SymfonyWebTestGraphQLClient(
      new Symfony\Bundle\FrameworkBundle\Client,
      '/graphql'  // change it to your endpoint's URI
    );
  }
}

Examples

Mutations

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);
}

Queries


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));
  }
  

You can also checkout this link for detailed instructions

qollabi-phx-graphql-client's People

Contributors

christiangoltz avatar dimuziop avatar manonworld avatar yalpa 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.