Skip to content

Commit ea53db2

Browse files
committed
Add create author mutation
Resolves #2
1 parent 9782f84 commit ea53db2

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ query {
8181
}
8282
```
8383

84+
#### Add new author
85+
86+
```graphql
87+
mutation {
88+
createAuthor(input:{name:"Fredrick", last_name:"Brooks"}) {
89+
id
90+
name
91+
last_name
92+
}
93+
}
94+
```
95+
8496
## License
8597

8698
[MIT license](./LICENSE)

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"di\\": "di",
1919
"models\\": "models",
2020
"queries\\": "queries",
21-
"types\\": "types"
21+
"types\\": "types",
22+
"inputTypes\\": "inputTypes",
23+
"mutations\\": "mutations"
2224
}
2325
}
2426
}

inputTypes/Author.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace inputTypes;
4+
5+
use GraphQL\Type\Definition\Type;
6+
use GraphQL\Type\Definition\InputObjectType;
7+
8+
class Author extends InputObjectType {
9+
static $type = null;
10+
11+
public static function get() {
12+
if (self::$type === null) {
13+
self::$type = self::create();
14+
}
15+
return self::$type;
16+
}
17+
18+
private static function create() {
19+
return new InputObjectType([
20+
'name' => 'AuthorInput',
21+
'fields' => [
22+
'name' => [
23+
'type' => Type::nonNull(Type::string()),
24+
'description' => 'Name of the author',
25+
],
26+
'last_name' => [
27+
'type' => Type::nonNull(Type::string()),
28+
'description' => 'Last name of the author',
29+
]
30+
]
31+
]);
32+
}
33+
}

mutations/CreateAuthor.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace mutations;
4+
5+
use GraphQL\Type\Definition\ObjectType;
6+
use GraphQL\Type\Definition\Type;
7+
8+
use types\Author as AuthorType;
9+
use inputTypes\Author as AuthorInputType;
10+
use models\Author As AuthorModel;
11+
12+
class CreateAuthor {
13+
public static function get() {
14+
return [
15+
'type' => AuthorType::get(),
16+
'args' => [
17+
'input' => AuthorInputType::get(),
18+
],
19+
'resolve' => function ($root, $args, $context) {
20+
$input = $args['input'];
21+
22+
$author = new AuthorModel();
23+
$author->name = $input['name'];
24+
$author->last_name = $input['last_name'];
25+
26+
$author->save();
27+
28+
return $author;
29+
}
30+
];
31+
}
32+
}

schema/schema.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use queries\Quote as QuoteQuery;
99
use queries\Quotes as QuotesQuery;
1010

11+
use mutations\CreateAuthor as CreateAuthorMutation;
12+
1113
return new Schema([
1214
'query' => new ObjectType([
1315
'name' => 'Query',
@@ -17,5 +19,11 @@
1719
'quote' => QuoteQuery::get(),
1820
'quotes' => QuotesQuery::get(),
1921
]
20-
])
22+
]),
23+
'mutation' => new ObjectType([
24+
'name' => 'Mutation',
25+
'fields' => [
26+
'createAuthor' => CreateAuthorMutation::get(),
27+
]
28+
])
2129
]);

0 commit comments

Comments
 (0)