Answer the question
In order to leave comments, you need to log in
How to get a list of requested fields in GraphQL?
I am working with this library: https://github.com/webonyx/graphql-php, or rather, I am learning to work with it.
Organized a small example:
Request
{
Record(id: "7", alias: "test") {
id,
alias
}
}
...
$contentType = new ObjectType([
'name' => 'Content',
'description' => 'Информационная сущность.',
'fields' => [
'id' => [
'type' => new NonNull(Type::string()),
'description' => 'Уникальный идентификатор',
],
'alias' => [
'type' => Type::string(),
'description' => 'Альтернативное уникальное именование',
],
'visible' => [
'type' => Type::boolean(),
'description' => 'Статус отображения',
],
...
],
]);
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'Record' => [
'type' => $contentType,
'args' => [
'id' => Type::string(),
'alias' => Type::string(),
],
'resolve' => function ($root, $args) {
return [
'id' => '1',
'alias' => 'alias',
'visible' => true,
'title' => 'Заголовок',
'short_text' => 'Короткое описание',
'full_text' => 'Подробное описание',
'secret' => 'Секрет',
];
},
],
],
]);
...
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'Record' => [
'type' => $contentType,
'args' => [
'id' => Type::string(),
'alias' => Type::string(),
],
'resolve' => function ($root, $args) {
// тут получаем список запрашиваемых полей для Record. В нашем случае id и alias
// формируем оптимальный запрос
},
],
],
]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question