Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use GraphQLTypeDefinitionObjectType;
- use GraphQLTypeDefinitionResolveInfo;
- class CertificateType extends ObjectType{
- public function __construct(){
- $config = [
- 'name' => 'Certificate',
- 'fields' => function() {
- return [
- 'id' => [
- 'type' => Types::nonNull(Types::string()),
- ],
- 'number' => [
- 'type' => Types::int()
- ],
- 'first_issue_date' => [
- 'type' => Types::string()
- ],
- 'products' => [
- 'type' => Types::product(),
- 'resolve'=> function($value, $args, $context){
- $pdo = $context['pdo'];
- $cert_id = $value->id;
- $result = $pdo->query("select * from products where cert_id = {$cert_id} ");
- return $result->fetchObject() ?: null;
- }
- ]
- ];
- }
- ];
- parent::__construct($config);
- }
- }
- use GraphQLTypeDefinitionType;
- class Types extends Type{
- protected static $typeInstances = [];
- public static function certificate(){
- return static::getInstance(CertificateType::class);
- }
- public static function product(){
- return static::getInstance(ProductType::class);
- }
- protected static function getInstance($class, $arg = null){
- if (!isset(static::$typeInstances[$class])) {
- $type = new $class($arg);
- static::$typeInstances[$class] = $type;
- }
- return static::$typeInstances[$class];
- }
- }
- use GraphQLTypeDefinitionObjectType;
- use GraphQLTypeDefinitionResolveInfo;
- class ProductType extends ObjectType
- {
- public function __construct()
- {
- $config = [
- 'name' => 'Product',
- 'fields' => function() {
- return [
- 'id' => [
- 'type' => Types::nonNull(Types::string()),
- ],
- 'primary_activity' => [
- 'type' => Types::string()
- ],
- 'trade_name' => [
- 'type' => Types::string()
- ],
- ];
- },
- ];
- parent::__construct($config);
- }
- }
- require_once __DIR__ . '/../../../../autoload.php';
- use GraphQLGraphQL;
- use GraphQLTypeSchema;
- use GraphQLTypeDefinitionObjectType;
- use GraphQLTypeDefinitionType;
- define('BASE_URL', 'http://127.0.0.1:8080');
- ini_set('display_errors', 0);
- $debug = !empty($_GET['debug']);
- if ($debug) {
- $phpErrors = [];
- set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {
- $phpErrors[] = new ErrorException($message, 0, $severity, $file, $line);
- });
- }
- try {
- $dbHost = 'localhost';
- $dbName = '*******';
- $dbUsername = 'root';
- $dbPassword = '*********';
- $pdo = new PDO("mysql:host={$dbHost};dbname={$dbName}", $dbUsername, $dbPassword);
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $appContext = [
- 'pdo' => $pdo ];
- if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
- $raw = file_get_contents('php://input') ?: '';
- $data = json_decode($raw, true);
- } else {
- $data = $_REQUEST;
- }
- $data += ['query' => null, 'variables' => null];
- if (null === $data['query']) {
- $data['query'] = '{hello}';
- }
- require __DIR__ . '/types/CertificateType.php';
- require __DIR__ . '/types/ProductType.php';
- require __DIR__ . '/types/OrganizationType.php';
- require __DIR__ . '/Types.php';
- $queryType = new ObjectType([
- 'name' => 'Query',
- 'fields' => [
- 'hello' => [
- 'description' => ' Hello world',
- 'type' => Types::string(),
- 'resolve' => function() {
- return 'Hello World';
- }
- ],
- 'certificate' => [
- 'type' => Types::listOf(Types::certificate()),
- 'description' => 'This is the certificate identification',
- 'args' => [
- 'id' => Types::string()],
- 'resolve' => function ($rootValue,$args,$context) {
- $pdo = $context['pdo'];
- $id = $args['id'];
- return $pdo->query("SELECT * from certificates where id ={$id}");
- return $data->fetchObject() ?: null;
- }
- ],
- 'certificates' => [
- 'type' => Types::listOf(Types::certificate()),
- 'resolve' => function($rootValue, $args, $context) {
- $pdo = $context['pdo'];
- $result = $pdo->query("select * from certificates order by id limit 10");
- return $result->fetchAll(PDO::FETCH_OBJ);
- }
- ],
- ]
- ]);
- $schema = new Schema([
- 'query' => $queryType
- ]);
- $result = GraphQL::execute(
- $schema,
- $data['query'],
- null,
- $appContext,
- (array) $data['variables']
- );
- if ($debug && !empty($phpErrors)) {
- $result['extensions']['phpErrors'] = array_map(
- ['GraphQLErrorFormattedError', 'createFromPHPError'],
- $phpErrors
- );
- }
- $httpStatus = 200;
- } catch (Exception $error) {
- // Handling Exception
- // *************************************
- $httpStatus = 500;
- if (!empty($_GET['debug'])) {
- $result['extensions']['exception'] = FormattedError::createFromException($error);
- } else {
- $result['errors'] = [FormattedError::create('Unexpected Error')];
- }
- }
- header('Content-Type: application/json', true, $httpStatus);
- echo json_encode($result);
- {"errors":[{"message":"Internal server error","category":"internal","locations":[{"line":1,"column":2}],"path":["certificate"]}],"data":{"certificate":null}}
- Secondly when I run a nested query
- "http://192.168.211.15:8080/index.php?query{certificates{id,products{id}}}"
- I get the below listed response:
- {"errors":[{"message":"Internal server error","category":"internal","locations":[{"line":1,"column":26}],"path":["certificates",0,"products"]}
- "data":{"certificates":[{"id":"a023gavcx","status":"Valid","products":null}]}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement