A general purpose GraphQL Schema Parser library in PHP.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Pho/Lib/GraphQL/Parser
tests
.gitignore
LICENSE
README.md
VERSION
composer.json
phpunit.xml.dist

README.md

pho-lib-graphql-parser

A general purpose GraphQL schema parser library in PHP. This library does not parse GraphQL queries or responses.

Dependencies

The following dependencies must be met before installing pho-lib-graphql-parser. Please note, the patches shown must be applied since schema support is still experimental.

  1. The LibGraphQLParser library by Facebook. Make sure the experimental schema support is enabled with this patch.
  2. The graphql-parser-php PHP extension. Make sure the experimental schema support is enabled with this patch.

Installation

Once you make sure that all the dependencies are properly satisfied, the recommended way to install pho-lib-graphql-parser is through composer. It's as simple as:

composer require phonetworks/pho-lib-graphql-parser

Usage

Suppose you have the following GraphQL schema file as BasicSchema.graphql:

type Basic implements Something {
  id: ID!,
  context: String!
  user: User @the_directive(the_argument: "TheValue")
}

Then, you can parse it via:

$parsed = new Pho\Lib\GraphQL\Parser\Parse("BasicSchema.graphql");
foreach($parsed as $entity) {
  $entity_name = $entity->name(); // Basic
  $implementations = $entity->implementations(); // an array with a single element
  $implementation_name = $entity->implementations(0)->name(); // Something
  $directives = $entity->directives(); // no root level directive. But we would have one if it was type Basic @is_a { ...
  $fields = $entity->fields(); // we have three; id, context and user.
  $field_1["name"] = $entity->field(0)->name(); // id
  $field_1["type"] = $entity->field(0)->type(); // ID
  $field_1["nullable"] = $entity->field(0)->nullable(); // false (due to !)
  $field_3["directives"][0] = $entity->field(0)->directive(0)->name(); // the_directive
  $field_3["directives"][0]["arguments"][0]["name"] = $entity->field(0)->directive(0)->name(); // the_argument
  $field_3["directives"][0]["arguments"][0]["value"] = $entity->field(0)->directive(0)->value(); // TheValue 
}

Please note, for argument values, we currently support String only.

License

Released under the terms of the permissive MIT license.