|
| 1 | +<?php |
| 2 | + |
| 3 | +use GraphQL\Types\GraphQLObjectType; |
| 4 | +use GraphQL\Types\GraphQLInt; |
| 5 | +use GraphQL\Types\GraphQLEnum; |
| 6 | +use GraphQL\Types\GraphQLEnumValue; |
| 7 | +use GraphQL\Types\GraphQLString; |
| 8 | +use GraphQL\Types\GraphQLInterface; |
| 9 | +use GraphQL\Types\GraphQLNonNull; |
| 10 | +use GraphQL\Types\GraphQLList; |
| 11 | +use GraphQL\Types\GraphQLUnion; |
| 12 | +use GraphQL\Fields\GraphQLTypeField; |
| 13 | +use GraphQL\Arguments\GraphQLFieldArgument; |
| 14 | + |
| 15 | +require __DIR__ . "/__data.php"; |
| 16 | + |
| 17 | +/** |
| 18 | + * EPISODE |
| 19 | + */ |
| 20 | +$Episode = new GraphQLEnum("Episode", "One of the films in the Star Wars Trilogy.", [ |
| 21 | + new GraphQLEnumValue("NEW_HOPE", "Released in 1977."), |
| 22 | + new GraphQLEnumValue("EMPIRE", "Released in 1980."), |
| 23 | + new GraphQLEnumValue("JEDI", "Released in 1983.") |
| 24 | +]); |
| 25 | + |
| 26 | +/** |
| 27 | + * CHARACTER |
| 28 | + */ |
| 29 | +$Character = new GraphQLInterface("Character", "A character in the Star Wars Trilogy.", function() use(&$Character, &$Episode){ |
| 30 | + return [ |
| 31 | + new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the character."), |
| 32 | + new GraphQLTypeField("name", new GraphQLString(), "The name of the character."), |
| 33 | + new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the character, or an empty list if they have none."), |
| 34 | + new GraphQLTypeField("appearsIn", new GraphQLList($Episode), "Which movies they appear in."), |
| 35 | + ]; |
| 36 | +}, function ($character){ |
| 37 | + if($character["type"]==="human"){ |
| 38 | + return "Human"; |
| 39 | + } |
| 40 | + return "Droid"; |
| 41 | +}); |
| 42 | + |
| 43 | +/** |
| 44 | + * HUMAN |
| 45 | + */ |
| 46 | +$Human = new GraphQLObjectType("Human", "A humanoid creature in the Star Wars universe.", function() use(&$Character, &$Episode, &$humans, &$droids){ |
| 47 | + return [ |
| 48 | + new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the human."), |
| 49 | + new GraphQLTypeField("name", new GraphQLString(), "The name of the human."), |
| 50 | + new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the human, or an empty list if they have none.", function ($character) use(&$humans, &$droids){ |
| 51 | + return array_map(function($friendId) use(&$humans, &$droids){ |
| 52 | + return $humans[$friendId] ?? $droids[$friendId]; |
| 53 | + }, $character["friends"]); |
| 54 | + }), |
| 55 | + new GraphQLTypeField("appearsIn", new GraphQLList($Episode), "Which movies they appear in."), |
| 56 | + new GraphQLTypeField("homePlanet", new GraphQLString(), "The home planet of the human, or null if unknown.") |
| 57 | + ]; |
| 58 | +}, [ |
| 59 | + $Character |
| 60 | +]); |
| 61 | + |
| 62 | +/** |
| 63 | + * DROID |
| 64 | + */ |
| 65 | +$Droid = new GraphQLObjectType("Droid", "A mechanical creature in the Star Wars universe.", function() use(&$Character, &$Episode, &$humans, &$droids){ |
| 66 | + return [ |
| 67 | + new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the droid."), |
| 68 | + new GraphQLTypeField("name", new GraphQLString(), "The name of the droid."), |
| 69 | + new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the droid, or an empty list if they have none.", function ($character) use(&$humans, &$droids){ |
| 70 | + return array_map(function($friendId) use(&$humans, &$droids){ |
| 71 | + return $humans[$friendId] ?? $droids[$friendId]; |
| 72 | + }, $character["friends"]); |
| 73 | + }), |
| 74 | + new GraphQLTypeField("appearsIn", new GraphQLList($Episode), "Which movies they appear in."), |
| 75 | + new GraphQLTypeField("primaryFunction", new GraphQLString(), "The primary function of the droid.") |
| 76 | + ]; |
| 77 | +}, [ |
| 78 | + $Character |
| 79 | +]); |
| 80 | + |
| 81 | + |
| 82 | +/** |
| 83 | + * QUERY |
| 84 | + */ |
| 85 | +$Query = new GraphQLObjectType("Query", "Root Query", function () use (&$Episode, &$Character, &$Human, &$Droid, &$humans, &$droids){ |
| 86 | + return [ |
| 87 | + new GraphQLTypeField("hero", $Character, "", function($_, $args) use(&$humans, &$droids){ |
| 88 | + if(($args["episode"] ?? null)==="JEDI"){ |
| 89 | + return $humans["1000"]; // Luke Skywalker |
| 90 | + } |
| 91 | + return $droids["2000"]; // R2-D2 |
| 92 | + }, [ |
| 93 | + new GraphQLFieldArgument("episode", $Episode, "If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode") |
| 94 | + ] |
| 95 | + ), |
| 96 | + new GraphQLTypeField("human", $Human, "", function($_, $args) use(&$humans){ |
| 97 | + return $humans[$args["id"]] ?? null; |
| 98 | + }, [ |
| 99 | + new GraphQLFieldArgument("id", new GraphQLNonNull(new GraphQLString()), "id of the human") |
| 100 | + ] |
| 101 | + ), |
| 102 | + new GraphQLTypeField("droid", $Droid, "", function($_, $args) use(&$droids){ |
| 103 | + return $droids[$args["id"]] ?? null; |
| 104 | + }, [ |
| 105 | + new GraphQLFieldArgument("id", new GraphQLNonNull(new GraphQLString()), "id of the droid") |
| 106 | + ] |
| 107 | + ) |
| 108 | + ]; |
| 109 | +}); |
0 commit comments