Skip to content

Commit 27ac6ae

Browse files
committed
add docs to php-graphql
1 parent d742994 commit 27ac6ae

File tree

13 files changed

+556
-0
lines changed

13 files changed

+556
-0
lines changed

docs/getting-started.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Installation
2+
To install php-graphql, you basically have two options. You can either use composer or git submodules.
3+
4+
Composer (coming soon):
5+
````bash
6+
composer require joonlabs/php-graphql
7+
````
8+
9+
Git-Submodule
10+
````bash
11+
git submodule add https://github.com/joonlabs/php-graphql.git
12+
````
13+
# Additional Tools
14+
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](https://github.com/imolorhe/altair) or [GraphiQL](https://github.com/graphql/graphiql), while debugging and developing.
15+
These tools offer syntax highlighting, autocompletion, documentation insights and much more.
16+
17+
# Hello world!
18+
19+
```php
20+
use GraphQL\Servers\Server;
21+
use GraphQL\Schemas\Schema;
22+
use GraphQL\Types\GraphQLString;
23+
use GraphQL\Types\GraphQLObjectType;
24+
use GraphQL\Fields\GraphQLTypeField;
25+
26+
// build the query type
27+
$QueryType = new GraphQLObjectType("Query", "Root Query", function (){
28+
return [
29+
new GraphQLTypeField(
30+
"hello",
31+
new GraphQLString(),
32+
"Your first hello world GraphQL-Application",
33+
function (){ return 'Hello world!'; }
34+
)
35+
];
36+
});
37+
38+
// build the schema
39+
$schema = new Schema($QueryType);
40+
41+
// start a server
42+
$server = new Server($schema);
43+
$server->listen();
44+
```
45+
46+
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:
47+
````graphql
48+
query {
49+
hello
50+
}
51+
````
52+
... and will return:
53+
54+
````json
55+
{
56+
"data": {
57+
"hello" : "Hello world!"
58+
}
59+
}
60+
````
61+
62+
# What's next?
63+
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](https://github.com/joonlabs/php-graphql/tree/master/examples/starwars) and [books](https://github.com/joonlabs/php-graphql/tree/master/examples/books) examples.

docs/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Introduction
2+
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
3+
4+
At its core, GraphQL is a standard (see the [official specification](https://spec.graphql.org)) developed by Facebook Engineers. This means that GraphQL is implementation independent.
5+
A well-documented overview of the features and capabilities of GraphQL can be found on the official website, all of which were implemented in this project.
6+
7+
# php-graphql
8+
9+
**php-graphql** is a new and feature-complete implementation of the GraphQL specifications, based on the [refernce implenentation in JavaScript](https://github.com/graphql/graphql-js).
10+
The goal of this project is to create a lightweight and superfast alternative to other PHP GraphQL implementations, which also supports file upload from scratch.
11+
12+
To achieve this goal, the library features its own LL(1) parser, validator and executor. Also, performance critical duplicate validations by validator and executor are tried to be avoided to allow fast validation and subsequent execution.
13+
14+
The following features are included in the library:
15+
- GraphQL-Types for building a Type System
16+
- Introspection of your Type System (for tools like [Altair](https://github.com/imolorhe/altair) or [GraphiQL](https://github.com/graphql/graphiql))
17+
- Parsing, validating and executing GraphQL queries
18+
- Automated input handling and easy GraphQL-Server setup
19+
20+
# Status
21+
The first release of this project was published in May 2021.
22+
The current relaese supports all features described by the official specification.
23+
As this project was developed out of internal needs to overcome shortcomings of other implementations, it will be maintained for several years.
24+
25+
# Source Code
26+
The project's source code is hosted on [GitHub](https://github.com/joonlabs/php-graphql/)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Introduction
2+
This section describes how parsing, validation and execution in a high level and should only be interesting for building an own processing pipeline or understanding how the GraphQL-Server works.
3+
In all other cases the default `Server` should be fine to use.
4+
5+
# Parsing
6+
Before a query can be executed, it must first be parsed into a so-called document. The string of a query is converted into an associative array, which contains more detailed information about certain parts of the query, such as locations of different words, their meaning in the current context, and so on. This parsed document can then be passed to a validator, or directly to an executor.
7+
8+
Parsing can be done like this:
9+
```php
10+
use GraphQL\Parser\Parser;
11+
12+
// define the query
13+
$query = "
14+
query {
15+
helloWorld
16+
}";
17+
18+
// create parser and parse the query
19+
$parser = new Parser();
20+
$parser->parse($query);
21+
22+
// check if query was syntactically correct and obtain parsed document
23+
if($parser->queryIsValid()){
24+
$document = $parser->getParsedDocument();
25+
}
26+
```
27+
28+
# Validation
29+
A validator takes a parsed document, and a schema to decide wether the given operations in the document are valid against the schema or not.
30+
31+
Validation can be done like this:
32+
33+
```php
34+
use GraphQL\Validation\Validator;
35+
36+
// create validator and validate document against schema
37+
$validator = new Validator();
38+
$validator->validate($schema, $document);
39+
40+
// check if document was valid against the schema
41+
if($validator->documentIsValid()){
42+
// continue with execution
43+
}
44+
```
45+
46+
# Execution
47+
An exeutor takes a parsed document, and a schema to perform all operation definitions given by the document on the schema.
48+
This can be any kind of query or mutation, or multiple of those. It is highly recommended to validate the query first and execute it afterwards to prevent errors during execution.
49+
If you are using the default `Server` this work is already done for you.
50+
51+
```php
52+
use GraphQL\Execution\Executor;
53+
54+
// create executor
55+
$executor = new Executor();
56+
57+
// execute document
58+
$result = $executor->execute($schema, $document);
59+
```

docs/schema-definition.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Schema Definition
2+
The schema is a container for your type hierarchy that accepts object types as roots for the query type, and the mutation type.
3+
It is passed to the validator and the executor and used to model your underlying data structures.
4+
5+
````php
6+
use GraphQL\Servers\Server;
7+
8+
$schema = new Schema(
9+
$QueryType,
10+
$MutationType
11+
);
12+
````
13+
14+
# Query and Mutation
15+
As described above, the schema consits of two root types (which are both object types):
16+
- the **query** type for performing read operations on your data and
17+
- the **mutation** type for performing changes in and operations on your data (this is completely optional)
18+
19+
```php
20+
use GraphQL\Types\GraphQLInt;
21+
use GraphQL\Types\GraphQLString;
22+
use GraphQL\Types\GraphQLNonNull;
23+
use GraphQL\Types\GraphQLObjectType;
24+
use GraphQL\Fields\GraphQLTypeField;
25+
use GraphQL\Arguments\GraphQLFieldArgument;
26+
27+
$QueryType = new GraphQLObjectType("Query", "Root Query", function (){
28+
return [
29+
new GraphQLTypeField(
30+
"hello",
31+
new GraphQLString(),
32+
"Your first hello world GraphQL-Application's query",
33+
function (){ return 'Hello world!'; }
34+
)
35+
];
36+
});
37+
38+
$MutationType = new GraphQLObjectType("Mutation", "Root Mutation", function (){
39+
return [
40+
new GraphQLTypeField(
41+
"multiply",
42+
new GraphQLInt(),
43+
"Your first hello world GraphQL-Application's mutation",
44+
function ($parent, $args, $context, $info){
45+
return $args["a"] * $args["b"];
46+
},
47+
[
48+
new GraphQLFieldArgument("a", new GraphQLNonNull(new GraphQLInt())),
49+
new GraphQLFieldArgument("b", new GraphQLNonNull(new GraphQLInt())),
50+
]
51+
)
52+
];
53+
});
54+
```

docs/server.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Server
2+
The default `Server` is a simple wrapper around a parser, validator and executor to save you work.
3+
When constructing a server, simply pass the schema to the constructor and enable processing via the `listen()` function.
4+
5+
```php
6+
use GraphQL\Servers\Server;
7+
8+
// create server and listen for inputs
9+
$server = new Server($schema);
10+
$server->listen();
11+
```
12+
13+
For more details on parsing, validation and execution, take a look at the according section in the docs.

docs/types/directives.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Directives
2+
A directive describes how the data should be processed at a meta level.
3+
A directive can be attached to a field or fragment and can affect the execution of the query.
4+
5+
**php-graphql** implements the both directives given by the specification:
6+
- **@include(if:Boolean)** A field or fragment is only included if this directive's parameter `if` is **true**
7+
- **@skip(if:Boolean)** A field or fragment is skipped if this directive's parameter `if` is **true**
8+
9+
For example:
10+
```graphql
11+
query Hero($episode: Episode, $withFriends: Boolean!) {
12+
hero(episode: $episode) {
13+
name
14+
friends @include(if: $withFriends) {
15+
name
16+
}
17+
}
18+
}
19+
```
20+
21+
The selection `friends` will only be included in the returning data, if the variable **$withFriends** is set to **true**.

docs/types/enums.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Enums
2+
Enumeration types are a special kind of scalar that is restricted to a particular set of allowed values and in this implementation handled as string values.
3+
4+
**php-graphql** represents enums as instances of the class `GraphQLEnum`, which accepts a name, description and possible values
5+
(`GraphQlEnumValue`) as parameters in the constructor.
6+
7+
```php
8+
use GraphQL\Types\GraphQLEnum;
9+
use GraphQL\Types\GraphQLEnumValue;
10+
11+
$Episode = new GraphQLEnum("Episode", "One of the films in the Star Wars Trilogy.", [
12+
new GraphQLEnumValue("NEW_HOPE", "Released in 1977."),
13+
new GraphQLEnumValue("EMPIRE", "Released in 1980."),
14+
new GraphQLEnumValue("JEDI", "Released in 1983.")
15+
]);
16+
```

docs/types/index.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Types
2+
In **php-graphql**, types are represented as instances of a GraphQL-Type-Class. Supported built in types are:
3+
- ```GraphQLBoolean()```
4+
- ```GraphQLEnum()```
5+
- ```GraphQLFloat()```
6+
- ```GraphQLID()```
7+
- ```GraphQLInputObjectType()```
8+
- ```GraphQLInt()```
9+
- ```GraphQLInterface()```
10+
- ```GraphQLList()```
11+
- ```GraphQLNonNull()```
12+
- ```GraphQLObjectType()```
13+
- ```GraphQLString()```
14+
- ```GraphQLUnion()```
15+
16+
# Definition
17+
All types are created inline. This means that to give an object a type, you just need to create an instance of that type. For example:
18+
```php
19+
use GraphQL\Types\GraphQLObjectType;
20+
use GraphQL\Fields\GraphQLTypeField;
21+
use GraphQL\Types\GraphQLInt;
22+
use GraphQL\Types\GraphQLEnum;
23+
use GraphQL\Types\GraphQLEnumValue;
24+
use GraphQL\Types\GraphQLString;
25+
26+
// create enum type
27+
$MyEnumType = new GraphQLEnum(
28+
"MyEnumType",
29+
"This is the description of my enum type",
30+
[
31+
new GraphQLEnumValue("PossibleValue1"),
32+
new GraphQLEnumValue("PossibleValue2"),
33+
new GraphQLEnumValue("PossibleValue3")
34+
]
35+
);
36+
37+
// create object type
38+
$MyObjectType = new GraphQLObjectType(
39+
"MyObjectType",
40+
"This is the description of my object type",
41+
function() use(&$MyEnumType){
42+
return [
43+
new GraphQLTypeField("myIntField", new GraphQLInt()),
44+
new GraphQLTypeField("myStringField", new GraphQLString()),
45+
new GraphQLTypeField("myEnumField", $MyEnumType)
46+
];
47+
}
48+
);
49+
```
50+
51+
For more details about each type, just check the corresponding sections.

docs/types/interfaces.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Interfaces
2+
An Interface is an abstract type that includes a certain set of fields that a type must include to implement the interface.
3+
An Interface can never be returned directly but must be implemented by 'child types'.
4+
5+
**php-graphql** represents interfaces as instances of the class `GraphQLInterface`, which accepts several parameters
6+
in the constructor for configuring the interface.
7+
8+
```php
9+
use GraphQL\Types\GraphQLInterface;
10+
use GraphQL\Fields\GraphQLTypeField;
11+
use GraphQL\Types\GraphQLNonNull;
12+
use GraphQL\Types\GraphQLString;
13+
use GraphQL\Types\GraphQLList;
14+
15+
$Character = new GraphQLInterface("Character", "A character in the Star Wars Trilogy.", function () use (&$Character, &$Episode) {
16+
return [
17+
new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the character."),
18+
new GraphQLTypeField("name", new GraphQLString(), "The name of the character."),
19+
new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the character, or an empty list if they have none."),
20+
new GraphQLTypeField("appearsIn", new GraphQLList($Episode), "Which movies they appear in."),
21+
];
22+
}, function ($character) {
23+
if ($character["type"] === "human") {
24+
return "Human";
25+
}
26+
return "Droid";
27+
});
28+
```
29+
# GraphQLInterface Constructor
30+
Parameters used for the `GraphQLInterface` constructors are (in the following order):
31+
32+
| Parameter | Type | Description | Required |
33+
| --- | --- | --- | --- |
34+
| **type** | `string` | Name of the new interface. Used for introspection. | ☑️ |
35+
| **description** | `string` | Description of the new interface. Used for introspection. | ☑️ |
36+
| **fields** | `\Closure` | Function that returns an array of `GraphQLTypeField`. Each field must be implemented in the type that implements this interface. Is called as soon the fields are needed. Used during validation and execution. | ☑️ |
37+
| **resolveTypeFn** | `\Closure` | Function that decides to which implementing object type a data object belongs to. If not passed, the default resolver is used. Used during validation and execution. ||
38+
39+
40+
# Implementing Interfaces
41+
Interfaces can be implemented by a `GraphQLObjectType`, when passing the interface (reference) to the `GraphQLObjectType` constructor (see parameters of `GraphQLObjectType` constructor in the according section).

docs/types/list-and-non-nulls.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Lists and Non-Nulls
2+
Lists and Non-Nulls are wrapping types in GraphQL. This means, that they cannot be used solely, but must feature an inner type, which they hold.
3+
4+
5+
# Lists
6+
**php-graphql** represents lists of other types as instances of the class `GraphQLList`, which accepts an inner type
7+
as parameter in the constructor.
8+
9+
```php
10+
use GraphQL\Types\GraphQLList;
11+
use GraphQL\Types\GraphQLInt;
12+
13+
$ListOfInts = new GraphQLList(new GraphQLInt());
14+
```
15+
16+
# Non-Null
17+
**php-graphql** represents non-null types as instances of the class `GraphQLNonNull`, which accepts an inner type
18+
as parameter in the constructor.
19+
20+
```php
21+
use GraphQL\Types\GraphQLNonNull;
22+
use GraphQL\Types\GraphQLInt;
23+
24+
$NonNullInt = new GraphQLNonNull(new GraphQLInt());
25+
```

0 commit comments

Comments
 (0)