Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Installation

To install php-graphql, you basically have two options. You can either use composer or git submodules.

Composer:

composer require joonlabs/php-graphql

Git-Submodule

git submodule add https://github.com/joonlabs/php-graphql.git

Notice: When using this package via Git-Submodules, a custom autoloader is required. php-graphql comes with an own autoloader as replacement for the PSR-4 autoloader of composer, which can be used like this:

require 'php-graphql/src/autoloader.php';

Additional Tools

Although it is completely possible to communicate with the GraphQL API via HTTP requests, it is much more convenient to use graphical tools like Altair or GraphiQL, while debugging and developing. These tools offer syntax highlighting, autocompletion, documentation insights and much more.

Hello world!

use GraphQL\Servers\Server;
use GraphQL\Schemas\Schema;
use GraphQL\Types\GraphQLString;
use GraphQL\Types\GraphQLObjectType;
use GraphQL\Fields\GraphQLTypeField;

// build the query type
$QueryType = new GraphQLObjectType("Query", "Root Query", function (){
    return [
        new GraphQLTypeField(
            "hello",
            new GraphQLString(),
            "Your first hello world GraphQL-Application",
            function (){ return 'Hello world!'; }
        )
    ];
});

// build the schema
$schema = new Schema($QueryType);

// start a server
$server = new Server($schema);
$server->listen();

That’s it! Now the GraphQL server is ready to accept requests at the URL of the PHP script. An example request can now look like this:

query {
    hello
}

… and will return:

{
  "data": {
    "hello" : "Hello world!"
  }
}

What’s next?

Since this example is extremely simple and does not really address what is possible with GraphQL, it is recommended to take a closer look at the Star Wars and books examples.