Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. use GraphQLTypeDefinitionObjectType;
  2. use GraphQLTypeDefinitionResolveInfo;
  3. class CertificateType extends ObjectType{
  4. public function __construct(){
  5. $config = [
  6. 'name' => 'Certificate',
  7. 'fields' => function() {
  8. return [
  9. 'id' => [
  10. 'type' => Types::nonNull(Types::string()),
  11. ],
  12.  
  13. 'number' => [
  14. 'type' => Types::int()
  15. ],
  16. 'first_issue_date' => [
  17. 'type' => Types::string()
  18. ],
  19.  
  20. 'products' => [
  21. 'type' => Types::product(),
  22.  
  23. 'resolve'=> function($value, $args, $context){
  24. $pdo = $context['pdo'];
  25. $cert_id = $value->id;
  26.  
  27. $result = $pdo->query("select * from products where cert_id = {$cert_id} ");
  28. return $result->fetchObject() ?: null;
  29. }
  30. ]
  31.  
  32.  
  33. ];
  34. }
  35.  
  36. ];
  37. parent::__construct($config);
  38. }
  39.  
  40. }
  41.  
  42.  
  43. use GraphQLTypeDefinitionType;
  44.  
  45. class Types extends Type{
  46.  
  47. protected static $typeInstances = [];
  48.  
  49. public static function certificate(){
  50. return static::getInstance(CertificateType::class);
  51. }
  52.  
  53. public static function product(){
  54. return static::getInstance(ProductType::class);
  55. }
  56.  
  57.  
  58. protected static function getInstance($class, $arg = null){
  59. if (!isset(static::$typeInstances[$class])) {
  60. $type = new $class($arg);
  61. static::$typeInstances[$class] = $type;
  62. }
  63. return static::$typeInstances[$class];
  64. }
  65. }
  66.  
  67. use GraphQLTypeDefinitionObjectType;
  68. use GraphQLTypeDefinitionResolveInfo;
  69.  
  70. class ProductType extends ObjectType
  71. {
  72. public function __construct()
  73. {
  74. $config = [
  75. 'name' => 'Product',
  76. 'fields' => function() {
  77. return [
  78. 'id' => [
  79. 'type' => Types::nonNull(Types::string()),
  80. ],
  81.  
  82. 'primary_activity' => [
  83. 'type' => Types::string()
  84. ],
  85.  
  86. 'trade_name' => [
  87. 'type' => Types::string()
  88. ],
  89.  
  90. ];
  91. },
  92. ];
  93. parent::__construct($config);
  94. }
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. require_once __DIR__ . '/../../../../autoload.php';
  103. use GraphQLGraphQL;
  104. use GraphQLTypeSchema;
  105. use GraphQLTypeDefinitionObjectType;
  106. use GraphQLTypeDefinitionType;
  107.  
  108. define('BASE_URL', 'http://127.0.0.1:8080');
  109.  
  110. ini_set('display_errors', 0);
  111.  
  112.  
  113. $debug = !empty($_GET['debug']);
  114. if ($debug) {
  115. $phpErrors = [];
  116. set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {
  117. $phpErrors[] = new ErrorException($message, 0, $severity, $file, $line);
  118. });
  119. }
  120.  
  121. try {
  122.  
  123. $dbHost = 'localhost';
  124. $dbName = '*******';
  125. $dbUsername = 'root';
  126. $dbPassword = '*********';
  127.  
  128.  
  129. $pdo = new PDO("mysql:host={$dbHost};dbname={$dbName}", $dbUsername, $dbPassword);
  130. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  131. $appContext = [
  132. 'pdo' => $pdo ];
  133.  
  134. if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
  135. $raw = file_get_contents('php://input') ?: '';
  136. $data = json_decode($raw, true);
  137. } else {
  138.  
  139. $data = $_REQUEST;
  140. }
  141.  
  142. $data += ['query' => null, 'variables' => null];
  143.  
  144. if (null === $data['query']) {
  145.  
  146. $data['query'] = '{hello}';
  147. }
  148. require __DIR__ . '/types/CertificateType.php';
  149. require __DIR__ . '/types/ProductType.php';
  150. require __DIR__ . '/types/OrganizationType.php';
  151.  
  152. require __DIR__ . '/Types.php';
  153.  
  154. $queryType = new ObjectType([
  155. 'name' => 'Query',
  156. 'fields' => [
  157. 'hello' => [
  158. 'description' => ' Hello world',
  159. 'type' => Types::string(),
  160. 'resolve' => function() {
  161. return 'Hello World';
  162. }
  163. ],
  164. 'certificate' => [
  165. 'type' => Types::listOf(Types::certificate()),
  166. 'description' => 'This is the certificate identification',
  167. 'args' => [
  168. 'id' => Types::string()],
  169. 'resolve' => function ($rootValue,$args,$context) {
  170. $pdo = $context['pdo'];
  171. $id = $args['id'];
  172.  
  173. return $pdo->query("SELECT * from certificates where id ={$id}");
  174. return $data->fetchObject() ?: null;
  175. }
  176. ],
  177. 'certificates' => [
  178. 'type' => Types::listOf(Types::certificate()),
  179. 'resolve' => function($rootValue, $args, $context) {
  180. $pdo = $context['pdo'];
  181. $result = $pdo->query("select * from certificates order by id limit 10");
  182. return $result->fetchAll(PDO::FETCH_OBJ);
  183. }
  184. ],
  185.  
  186. ]
  187. ]);
  188.  
  189. $schema = new Schema([
  190. 'query' => $queryType
  191. ]);
  192.  
  193. $result = GraphQL::execute(
  194. $schema,
  195. $data['query'],
  196. null,
  197. $appContext,
  198. (array) $data['variables']
  199. );
  200.  
  201. if ($debug && !empty($phpErrors)) {
  202. $result['extensions']['phpErrors'] = array_map(
  203. ['GraphQLErrorFormattedError', 'createFromPHPError'],
  204. $phpErrors
  205. );
  206. }
  207. $httpStatus = 200;
  208. } catch (Exception $error) {
  209. // Handling Exception
  210. // *************************************
  211. $httpStatus = 500;
  212. if (!empty($_GET['debug'])) {
  213. $result['extensions']['exception'] = FormattedError::createFromException($error);
  214. } else {
  215. $result['errors'] = [FormattedError::create('Unexpected Error')];
  216. }
  217. }
  218.  
  219. header('Content-Type: application/json', true, $httpStatus);
  220. echo json_encode($result);
  221.  
  222. {"errors":[{"message":"Internal server error","category":"internal","locations":[{"line":1,"column":2}],"path":["certificate"]}],"data":{"certificate":null}}
  223.  
  224. Secondly when I run a nested query
  225. "http://192.168.211.15:8080/index.php?query{certificates{id,products{id}}}"
  226.  
  227. I get the below listed response:
  228.  
  229. {"errors":[{"message":"Internal server error","category":"internal","locations":[{"line":1,"column":26}],"path":["certificates",0,"products"]}
  230. "data":{"certificates":[{"id":"a023gavcx","status":"Valid","products":null}]}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement