Skip to content

Commit 76791c9

Browse files
committed
Add create author mutation
1 parent e5d0452 commit 76791c9

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ query {
163163
}
164164
```
165165

166+
### Mutations
167+
168+
#### Create new author
169+
170+
```graphql
171+
mutation {
172+
createAuthor(input:{firstName:"Fredrick", lastName:"Brooks"}) {
173+
id
174+
_id
175+
firstName
176+
lastName
177+
}
178+
}
179+
```
180+
166181
## License
167182

168183
[MIT license](./LICENSE)

src/mutations/CreateAuthor.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 types\inputs\Author As AuthorInputType;
10+
11+
class CreateAuthor {
12+
public static function get() {
13+
return [
14+
'type' => AuthorType::get(),
15+
'args' => [
16+
'input' => AuthorInputType::get(),
17+
],
18+
'resolve' => function ($root, $args, $context) {
19+
$context->service->logger->debug('CreateAuthorMutation', $args);
20+
21+
$input = $args['input'];
22+
23+
$firstName = isset($input['firstName']) && !empty($input['firstName']) ? $input['firstName'] : null;
24+
$lastName = isset($input['lastName']) && !empty($input['lastName']) ? $input['lastName'] : null;
25+
26+
return $context->repository->author->create($firstName, $lastName);
27+
}
28+
];
29+
}
30+
}

src/repositories/AuthorRepository.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@ public function update(int $id, ?string $firstName, ?string $lastName): ?AuthorM
7777

7878
}
7979

80-
public function create(string $fistName, string $lastName): AuthorModel {
80+
public function create(string $firstName, string $lastName): AuthorModel {
8181
$this->logger->debug('AuthorRepository->create', ['firstName' => $firstName, 'lastName' => $lastName]);
8282

83+
$id = $this->db->getConnection()
84+
->table('author')
85+
->insertGetId([
86+
'name' => $firstName,
87+
'last_name' => $lastName,
88+
]);
89+
90+
return new AuthorModel(['id' => $id, 'firstName' => $firstName, 'lastName' => $lastName]);
91+
8392
}
8493

8594
public function delete(int $id): ?AuthorModel {

src/schema/schema.php

Lines changed: 8 additions & 0 deletions
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',
@@ -18,4 +20,10 @@
1820
'quotes' => QuotesQuery::get(),
1921
]
2022
]),
23+
'mutation' => new ObjectType([
24+
'name' => 'Mutation',
25+
'fields' => [
26+
'createAuthor' => CreateAuthorMutation::get(),
27+
]
28+
])
2129
]);

src/types/inputs/Author.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace inputTypes;
3+
namespace types\inputs;
44

55
use GraphQL\Type\Definition\Type;
66
use GraphQL\Type\Definition\InputObjectType;
@@ -19,11 +19,11 @@ private static function create() {
1919
return new InputObjectType([
2020
'name' => 'AuthorInput',
2121
'fields' => [
22-
'name' => [
22+
'firstName' => [
2323
'type' => Type::nonNull(Type::string()),
24-
'description' => 'Name of the author',
24+
'description' => 'First name of the author',
2525
],
26-
'last_name' => [
26+
'lastName' => [
2727
'type' => Type::nonNull(Type::string()),
2828
'description' => 'Last name of the author',
2929
]

0 commit comments

Comments
 (0)