2017-01-21 3 views
0

내가 거기에 예에 의해 사람 개체를 가져 오기 위해 노력하고 있어요! https://github.com/webonyx/graphql-php/tree/master/examples/01-blogPHP graphQl "필드 "사람 아이디 "인수 " "형의 "ID "제공 필요하지만하지 않습니다.",

어떻게 디버깅해야합니까?

IndexController.php

public function indexAction() 
    { 
     if (!empty($_GET['debug'])) { 
      // Enable additional validation of type configs 
      // (disabled by default because it is costly) 
      Config::enableValidation(); 
      // Catch custom errors (to report them in query results if debugging is enabled) 
      $phpErrors = []; 
      set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) { 
       $phpErrors[] = new ErrorException($message, 0, $severity, $file, $line); 
      }); 
     } 
     try { 
      /* // Initialize our fake data source 
      DataSource::init();*/ 
      // Prepare context that will be available in all field resolvers (as 3rd argument): 
      $appContext = new AppContext(); 

      // here we can change to repository which returns user object 
      //$appContext->viewer = DataSource::findUser('1'); // simulated "currently logged-in user" 
      $appContext->rootUrl = 'http://localhost:8080'; 
      $appContext->request = $_REQUEST; 
      // Parse incoming query and variables 
      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}'; 
      } 
      // GraphQL schema to be passed to query executor: 
      $schema = new Schema([ 
       'query' => Types::query() 
      ]); 
      $result = GraphQL::execute(
       $schema, 
       $data['query'], 
       null, 
       $appContext, 
       (array) $data['variables'] 
      ); 
      // Add reported PHP errors to result (if any) 
      if (!empty($_GET['debug']) && !empty($phpErrors)) { 
       $result['extensions']['phpErrors'] = array_map(
        ['GraphQL\Error\FormattedError', 'createFromPHPError'], 
        $phpErrors 
       ); 
      } 
      $httpStatus = 200; 
     } catch (\Exception $error) { 
      $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); 
     die; 
    } 

PersonType.php

namespace Application\GraphQL\Type; 

use Application\GraphQL\Types; 
use GraphQL\Type\Definition\ObjectType; 
use GraphQL\Type\Definition\ResolveInfo; 

class PersonType extends ObjectType 
{ 
    public function __construct() 
    { 
     $config = [ 
      'name' => 'Person', 
      'description' => 'Persons', 
      'fields' => function() { 
       return [ 
        'id' => Types::id(), 
        'firstName' => [ 
         'type' => Types::string(), 
        ], 
       ]; 
      }, 
      // todo what is this 
      'interfaces' => [ 

       Types::node() 
      ], 
      'resolveField' => function($value, $args, $context, ResolveInfo $info) { 
       if (method_exists($this, $info->fieldName)) { 
        return $this->{$info->fieldName}($value, $args, $context, $info); 
       } else { 
        return $value->{$info->fieldName}; 
       } 
      } 
     ]; 
     parent::__construct($config); 
    } 

} 

쿼리 유형 :

namespace Application\GraphQL\Type; 


use Application\GraphQL\Types; 
use GraphQL\Type\Definition\ObjectType; 
use GraphQL\Type\Definition\ResolveInfo; 
use GraphQL\Type\Definition\Type; 

class QueryType extends ObjectType 
{ 
    public function __construct() 
    { 
     $config = [ 
      'name' => 'Query', 
      'fields' => [ 
       'person' => [ 
        'type' => Types::person(), 
        'description' => 'Returns person by id', 
        'args' => [ 
         'id' => Types::nonNull(Types::id()) 
        ] 
       ], 
       'hello' => Type::string() 
      ], 
      'resolveField' => function($val, $args, $context, ResolveInfo $info) { 
       return $this->{$info->fieldName}($val, $args, $context, $info); 
      } 
     ]; 
     parent::__construct($config); 
    } 

    public function person($rootValue, $args) 
    { 
     // todo ? 
    } 

    public function hello() 
    { 
     return 'Your graphql-php endpoint is ready! Use GraphiQL to browse API aaa'; 
    } 
} 

내가 보내고 쿼리

{ 
    person { 
    id 
    } 
} 

왜 그 오류가 발생합니까?

답변

1

종종 내가 왜 오류지고 있다고 자신을 발견, 질문을 작성하고 코드를 최소화하기 위해 노력하고 있지만 :

'args' => [ 
       'id' => Types::nonNull(Types::id()) 
      ] 
:

그것은 (QueryType.php의 __construct에 becuase됩니다)이 기능은 라인입니다

그래서 코드는 id가 전달 될 것을 요구하지만 전달하지는 않습니다. Google의 검색어는 다음과 같아야합니다.

{ 
    person(id: "1") { 
    id 

    } 
}