Download the PHP package gmostafa/php-graphql-oqm without Composer

On this page you can find all versions of the php package gmostafa/php-graphql-oqm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-graphql-oqm

PHP GraphQL OQM

Build Status Build Status Codacy Badge Total
Downloads Latest Stable
Version License

This package utilizes the introspection feature of GraphQL APIs to generate a set of classes that map to the structure of the API schema. The generated classes can then be used in a very simple and intuitive way to query the API server.

Interacting with GraphQL API's using PHP has never been easier!

Installation

Run the following command to install the package using composer:

Generating The Schema Objects

After installing the package, the first step is to generate the schema objects. This can be easily achieved by executing the following command:

This script will retrieve the API schema types using the introspection feature in GraphQL, then generate the schema objects from the types, and save them in the schema_object directory in the root directory of the package. You can override the default write directory by providing the "Custom classes writing dir" value when running the command.

You can also specify all options via command line options:

or if you prefer long arguments

Usage

In all the examples below I'm going to use the super cool public Pokemon GraphQL API as an illustration.

Check out the API at: https://graphql-pokemon.now.sh/

And Github Repo: https://github.com/lucasbento/graphql-pokemon

After generating the schema objects for the public Pokemon API, we can easily query the API by using the RootQueryObject. Here's an example:

What this query does is that it selects the first 5 pokemons returning their names, ids, flee rates, fast attacks with their names. Easy right!?

All what remains is that we actually run the query to obtain results:

For more on how to use the client class refer to:

Notes

A couple of notes about schema objects to make your life easier when using the generating classes:

Dealing With Object Selectors

Whilst scalar field setters return an instance of the current query object, object field selectors return objects of the nested query object. This means that setting the $rootObject reference to the result returned by an object selector means that the root query object reference is gone.

Don't:

This way you end up with reference to the PokemonAttackQueryObject, and the reference to the RootQueryObject is gone.

Do:

This way you can keep track of the RootQueryObject reference and develop your query safely.

Dealing With Multiple Object Selectors

Suppose we want to get the pokemon "Charmander", retrieve his evolutions, evolution requirements, and evolution requirements of his evolutions, how can we do that?

We can't do this:

This is because the reference is now pointing to the evolution requirements of the evolutions of charmander and not charmander himself.

The best way to do this is by structuring the query like this:

This way we have kept the reference to charmander safe and constructed our query in an intuitive way.

Generally, whenever there's a branch off (just like in the case of getting evolutions and evolution requirements of the same object) the best way to do it is to structure the query like a tree, where the root of the tree becomes the reference to the object being branch off from. In this case, charmander is the root and evolutions and evolution requirements are 2 sub-trees branched off it.

Improving Query Objects Readability

A couple of hints on how to keep your query objects more readable:

  1. Store nodes that will be used as roots in branch offs in meaningful variables, just like the case with charmander.
  2. Write each selector on a separate line.
  3. Every time you use an object selector, add an extra indentation to the next selectors.
  4. Move construction of a new object in the middle of a query (such as an ArgumentsObject construction) to a new line.

Schema Objects Generation

After running the generation script, the SchemaInspector will run queries on the GraphQL server to retrieve the API schema. After that, the SchemaClassGenerator will traverse the schema from the root queryType recursively, creating a class for every object in the schema spec.

The SchemaClassGenerator will generate a different schema object depending on the type of object being scanned using the following mapping from GraphQL types to SchemaObject types:

Additionally, an ArgumentsObject will be generated for the arguments on each field in every object. The arguments object naming convention is:

{CURRENT_OBJECT}{FIELD_NAME}ArgumentsObject

The QueryObject

The object generator will start traversing the schema from the root queryType, creating a class for each query object it encounters according to the following rules:

The InputObject

For every input object the object generator encounters while traversing the schema, it will create a corresponding class according to the following rules:

The EnumObject

For every enum the object generator encounters while traversing the schema, it will create a corresponding ENUM class according to the following rules:

Live API Example

Looking at the schema of the Pokemon GraphQL API from the root queryType, that' how it looks like:

What we basically have is a root query object with 2 fields:

  1. pokemons: Retrieves a list of Pokemon objects. It has one argument: first.
  2. pokemon: Retrieves one Pokemon object. It has two arguments:: id and name.

Translating this small part of the schema leads to 3 objects:

  1. RootQueryObject: Represents the entry to point to traversing the API graph
  2. RootPokemonsArgumentsObject: Represents the arguments list on the "pokemons" field in the RootQueryObject
  3. RootPokemonArgumentsObject: Represents the arguments list on the "pokemon" field in the RootQueryObject

Here are the 3 classes generated:

The RootQueryObject contains 2 selector methods, one for each field, and an optional argument containing the ArgumentsObjects required.

The RootPokemonsArgumentsObject contains the only argument in the list for the "pokemons" field as a property with a setter for altering its value.

The RootPokemonArgumentsObject contains the 2 arguments in the list for the "pokemon" field as properties with setters to alter their values.

Extra

Additionally, PokemonQueryObject will be created while traversing the schema recursively. It is not needed to complete this demo, but I will add it below to make things clearer in case someone wants to see more of the generation in action:


All versions of php-graphql-oqm with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1 || ^8.0
gmostafa/php-graphql-client Version ^1.12
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package gmostafa/php-graphql-oqm contains the following files

Loading the files please wait ....