gpt4 book ai didi

php graphQl "Field\"人\"argument\"id\"of type\"ID!\"is required but not provided.",

转载 作者:行者123 更新时间:2023-12-04 15:53:20 25 4
gpt4 key购买 nike

我试图通过那里的例子来获取 person 对象:https://github.com/webonyx/graphql-php/tree/master/examples/01-blog

我应该如何调试这个?

索引 Controller .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;
}

人物类型.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
}
}

为什么我收到那个错误?

最佳答案

在编写问题并尝试最小化代码时,我经常发现自己为什么会收到错误:

这是因为在 QueryType.php __construct() 函数中有几行:

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

所以代码要求传递 id ,但我们没有传递它。我们的查询应该是这样的:
{
person(id: "1") {
id

}
}

关于php graphQl "Field\"人\"argument\"id\"of type\"ID!\"is required but not provided.",,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41777501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com