1. Go to this page and download the library: Download hansott/graphql-language 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/ */
hansott / graphql-language example snippets
use HansOtt\GraphQL\Query\Node;
use HansOtt\GraphQL\Query\Traverser;
use HansOtt\GraphQL\Query\VisitorBase;
use HansOtt\GraphQL\Query\OperationQuery;
final class VisitorQueryFinder extends VisitorBase // Or implement HansOtt\GraphQL\Query\Visitor
{
/**
* @var OperationQuery[]
*/
private $queries = [];
public function enterNode(Node $node)
{
if ($node instanceof OperationQuery) {
$this->queries[] = $node;
}
}
public function getQueries()
{
return $this->queries;
}
}
$document = $parser->parse($query);
$finder = new VisitorQueryFinder;
$traverser = new Traverser($finder);
$traverser->traverse($document);
var_dump($finder->getQueries());
// Or if you need multiple visitors
// use HansOtt\GraphQL\Query\VisitorMany
$visitors = new VisitorMany([$finder, ...]);
$traverser = new Traverser($visitors);
$traverser->traverse($document);
var_dump($finder->getQueries());
use HansOtt\GraphQL\Schema\ParseError;
use HansOtt\GraphQL\Schema\SyntaxError;
use HansOtt\GraphQL\Schema\ParserFactory;
$factory = new ParserFactory;
$parser = $factory->create();
$schema = <<<'SCHEMA'
enum DogCommand { SIT, DOWN, HEEL }
type Dog implements Pet {
name: String!
nickname: String
barkVolume: Int
doesKnowCommand(dogCommand: DogCommand!): Boolean!
isHousetrained(atOtherHomes: Boolean): Boolean!
owner: Human
}
interface Sentient {
name: String!
}
interface Pet {
name: String!
}
type Alien implements Sentient {
name: String!
homePlanet: String
}
type Human implements Sentient {
name: String!
}
enum CatCommand { JUMP }
type Cat implements Pet {
name: String!
nickname: String
doesKnowCommand(catCommand: CatCommand!): Boolean!
meowVolume: Int
}
union CatOrDog = Cat | Dog
union DogOrHuman = Dog | Human
union HumanOrAlien = Human | Alien
type QueryRoot {
dog: Dog
}
SCHEMA;
try {
$schema = $parser->parse($schema);
var_dump($schema); // Instance of HansOtt\GraphQL\Schema\Schema
} catch (SyntaxError $e) {
echo "Syntax error in query: {$e->getMessage()}" . PHP_EOL;
} catch (ParseError $e) {
echo "Failed to parse query: {$e->getMessage()}" . PHP_EOL;
}
php
use HansOtt\GraphQL\Query\ParseError;
use HansOtt\GraphQL\Query\SyntaxError;
use HansOtt\GraphQL\Query\ParserFactory;
$factory = new ParserFactory;
$parser = $factory->create();
$query = <<<'QUERY'
{
author(id: 1) {
name
}
}
QUERY;
try {
$document = $parser->parse($query);
var_dump($document); // Instance of HansOtt\GraphQL\Query\Document
} catch (SyntaxError $e) {
echo "Syntax error in query: {$e->getMessage()}" . PHP_EOL;
} catch (ParseError $e) {
echo "Failed to parse query: {$e->getMessage()}" . PHP_EOL;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.