File tree Expand file tree Collapse file tree 5 files changed +89
-2
lines changed Expand file tree Collapse file tree 5 files changed +89
-2
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,18 @@ query {
81
81
}
82
82
```
83
83
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
+
84
96
## License
85
97
86
98
[ MIT license] ( ./LICENSE )
Original file line number Diff line number Diff line change 18
18
"di\\ " : " di" ,
19
19
"models\\ " : " models" ,
20
20
"queries\\ " : " queries" ,
21
- "types\\ " : " types"
21
+ "types\\ " : " types" ,
22
+ "inputTypes\\ " : " inputTypes" ,
23
+ "mutations\\ " : " mutations"
22
24
}
23
25
}
24
26
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 8
8
use queries \Quote as QuoteQuery ;
9
9
use queries \Quotes as QuotesQuery ;
10
10
11
+ use mutations \CreateAuthor as CreateAuthorMutation ;
12
+
11
13
return new Schema ([
12
14
'query ' => new ObjectType ([
13
15
'name ' => 'Query ' ,
17
19
'quote ' => QuoteQuery::get (),
18
20
'quotes ' => QuotesQuery::get (),
19
21
]
20
- ])
22
+ ]),
23
+ 'mutation ' => new ObjectType ([
24
+ 'name ' => 'Mutation ' ,
25
+ 'fields ' => [
26
+ 'createAuthor ' => CreateAuthorMutation::get (),
27
+ ]
28
+ ])
21
29
]);
You can’t perform that action at this time.
0 commit comments