Skip to content

Commit a1c2aa8

Browse files
committed
add star wars example
1 parent 65c9d7f commit a1c2aa8

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

examples/starwars/__data.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$humans = [
4+
"1000" => ["type"=>"human", "id"=>"1000", "name"=>"Luke Skywalker", "friends"=>["1002", "1003", "2000", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Tatooine"],
5+
"1001" => ["type"=>"human", "id"=>"1001", "name"=>"Darth Vader", "friends"=>["1004"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Tatooine"],
6+
"1002" => ["type"=>"human", "id"=>"1002", "name"=>"Han Solo", "friends"=>["1000", "1003", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"]],
7+
"1003" => ["type"=>"human", "id"=>"1003", "name"=>"Leia Organa", "friends"=>["1000", "1002", "2000", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Alderaan"],
8+
"1004" => ["type"=>"human", "id"=>"1004", "name"=>"Wilhuff Tarkin", "friends"=>["1001"], "appearsIn"=>["NEW_HOPE"]],
9+
];
10+
11+
$droids = [
12+
"2000" => ["type"=>"droid", "id"=>"2000", "name"=>"C-3PO", "friends"=>["1000", "1002", "1003", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Protocol"],
13+
"2001" => ["type"=>"droid", "id"=>"2000", "name"=>"R2-D2", "friends"=>["1000", "1002", "1003"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Astromech"],
14+
];

examples/starwars/__schema.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
});

examples/starwars/index.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
// set header to json-format
3+
header("Content-Type: application/json");
4+
5+
// include the php-graphql-autoloader
6+
require '../../src/autoloader.php';
7+
8+
use GraphQL\Servers\Server;
9+
use GraphQL\Schemas\Schema;
10+
11+
// pre-fill queryType and mutationType
12+
$Query = null;
13+
14+
// load types and create schema from them
15+
include __DIR__ . "/__schema.php";
16+
$schema = new Schema($Query);
17+
18+
// build server
19+
$server = new Server($schema);
20+
$server->setInternalServerErrorPrint(true);
21+
$server->listen();
22+
?>

0 commit comments

Comments
 (0)