N
N
nepster-web2017-03-09 21:23:06
PHP
nepster-web, 2017-03-09 21:23:06

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
    }
}

Scheme
...
        $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' => 'Секрет',
                        ];
                    },
                ],
            ],
        ]);
...

Everything works, everything is fine. However, as I understand in 'resolve' I have to make a request to get data, this can be seen in the tests: https://github.com/webonyx/graphql-php/blob/master...
Everything would be fine, but if the client requests, for example, only id and alias, then I do not need to get all the data, including relations, etc. I understand that they will not be shown during the output, but the request for them will still go. It would be nice to get a list of fields (id, alias) that I'm requesting in 'resolve' in order to build an optimal query.
Please tell me how can this be done?
P.S. Interested in something like this:
$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

1 answer(s)
N
nepster-web, 2017-03-10
@nepster-web

https://github.com/webonyx/graphql-php/issues/95

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question