Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
202 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
id: mutations | ||
title: Writing mutations | ||
sidebar_label: Mutations | ||
--- | ||
|
||
In GraphQL-Controllers, mutations are created [just like queries](my_first_query.md). | ||
|
||
To create a mutation, you annotate a method in a controller with the `@Mutation` annotation. | ||
|
||
Here is a sample of a "saveProduct" query: | ||
|
||
```php | ||
namespace App\Controllers; | ||
|
||
use TheCodingMachine\GraphQL\Controllers\Annotations\Mutation; | ||
|
||
class ProductController | ||
{ | ||
/** | ||
* @Mutation | ||
*/ | ||
public function saveProduct(int $id, string $name, ?float $price = null): Product | ||
{ | ||
// Some code that saves a product. | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
--- | ||
id: type_mapping | ||
title: Type mapping | ||
sidebar_label: Type mapping | ||
--- | ||
|
||
The job of GraphQL-Controllers is to create GraphQL types from PHP types. | ||
|
||
Internally, GraphQL-Controllers uses a "type mapper". | ||
|
||
## Mapping a PHP class to a GraphQL type | ||
|
||
The ["my first query"](my_first_query.md) documentation page | ||
already explains how to use the `@Type` annotation to map a PHP class to a GraphQL type. Please refer to this documentation | ||
for class mapping | ||
|
||
## Mapping of scalar types | ||
|
||
Scalar PHP types can be type-hinted to the corresponding GraphQL types: | ||
|
||
- string | ||
- int | ||
- bool | ||
- float | ||
|
||
## Mapping of ID type | ||
|
||
GraphQL comes with a native "ID" type. PHP has no such type. | ||
|
||
TODO: develop a ID PHP class type? (for input types?) | ||
|
||
|
||
## Mapping of dates | ||
|
||
Out of the box, GraphQL does not have a `DateTime` type, but we took the liberty to add one, with sensible defaults. | ||
|
||
When used as an output type (i.e. in a "return type"), `DateTimeImmutable` or `DateTimeInterface` PHP classes are | ||
automatically mapped to this `DateTime` GraphQL type. | ||
|
||
```php | ||
/** | ||
* @Field | ||
*/ | ||
public function getDate(): \DateTimeInterface | ||
{ | ||
|
||
} | ||
``` | ||
|
||
The "date" field will be of type "DateTime". In the returned JSON response to a query, the date is formatted as a string | ||
in the ISO8601 format (aka ATOM format). | ||
|
||
When used in an "input type" (i.e. in arguments of a method), the <code>DateTime</code> PHP class is not supported. | ||
Only the <code>DateTimeImmutable</code> PHP class is mapped. | ||
|
||
<div class="alert alert-success">This is ok:</div> | ||
|
||
```php | ||
/** | ||
* @Query | ||
* @return Product[] | ||
*/ | ||
public function getProducts(\DateTimeImmutable $fromDate): array | ||
{ | ||
|
||
} | ||
``` | ||
|
||
<div class="alert alert-error">But <code>DateTime</code> input type is not supported:</div> | ||
|
||
```php | ||
/** | ||
* @Query | ||
* @return Product[] | ||
*/ | ||
public function getProducts(\DateTime $fromDate): array // BAD | ||
{ | ||
|
||
} | ||
``` | ||
|
||
|
||
|
||
TODO: ID | ||
|
||
TODO: Union type | ||
|
||
TODO: External type | ||
TODO: Extend class | ||
TODO: Sourcefield (other doc) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace TheCodingMachine\GraphQL\Controllers\Types; | ||
|
||
|
||
use TheCodingMachine\GraphQL\Controllers\GraphQLException; | ||
|
||
/** | ||
* A class that maps to the GraphQL ID type. | ||
*/ | ||
class ID | ||
{ | ||
private $value; | ||
|
||
public function __construct($value) | ||
{ | ||
if (! is_scalar($value) && (! is_object($value) || ! method_exists($value, '__toString'))) { | ||
throw new GraphQLException('ID constructor cannot be passed a non scalar value.'); | ||
} | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @return bool|float|int|string | ||
*/ | ||
public function val() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return (string) $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"docs": { | ||
"Installation": ["getting-started", "symfony-bundle", "other-frameworks"], | ||
"Usage": ["my-first-query", "input-types", "file-uploads", "custom-output-types"] | ||
"Usage": ["my-first-query", "mutations", "type_mapping", "input-types", "file-uploads", "custom-output-types"] | ||
} | ||
} |