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 {
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 )
Original file line number Diff line number Diff line change 1818 "di\\ " : " di" ,
1919 "models\\ " : " models" ,
2020 "queries\\ " : " queries" ,
21- "types\\ " : " types"
21+ "types\\ " : " types" ,
22+ "inputTypes\\ " : " inputTypes" ,
23+ "mutations\\ " : " mutations"
2224 }
2325 }
2426}
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 88use queries \Quote as QuoteQuery ;
99use queries \Quotes as QuotesQuery ;
1010
11+ use mutations \CreateAuthor as CreateAuthorMutation ;
12+
1113return new Schema ([
1214 'query ' => new ObjectType ([
1315 'name ' => 'Query ' ,
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]);
You can’t perform that action at this time.
0 commit comments