Skip to content

Navigation Menu

Sign in
Sign up

GraphQL Query Returning Null Despite Correct Configuration #1200

Unanswered
ersincetin asked this question in Q&A
Discussion options

I am facing an issue where a simple GraphQL query is returning null instead of the expected data. Here's a summary of my setup:

Resolver Class:

use Overblog\GraphQLBundle\Definition\Annotation as GQL;

#[GQL\Provider]
class HelloResolver {
 #[GQL\Query(name: 'hello', type: 'Hello')]
 public function hello(): array {
 return ['message' => 'Hello'];
 }
}

GraphQL Schema (hello.graphql):

graphql
type Hello {
 message: String!
}
type Query {
 hello: Hello
}

services.yaml:
yaml
services:
App\Resolver\HelloResolver:
tags:
- { name: 'overblog_graphql.resolver', method: 'hello' }
public: true

overblog_graphql.yaml:

yaml
overblog_graphql:
 definitions:
 schema:
 query: Query
 mappings:
 types:
 - type: graphql
 dir: "%kernel.project_dir%/config/graphql/types"
 suffix: null

GraphQL Query:
query {
hello {
message
}
}

Despite this setup, the query result is:

{
 "data": {
 "hello": null
 }
}

What I Have Tried:

Verified the resolver method is correctly defined and returning the expected array manually.

Checked that the resolver is correctly tagged in services.yaml.

Ensured the schema is correctly defined in the .graphql file.

Cleared Symfony cache multiple times.

Verified that the resolver service is loaded correctly using php bin/console debug:container | grep HelloResolver.

No errors or warnings in the logs.

Environment:

PHP version: 8.x

Symfony version: 7.2.x

Overblog/GraphQLBundle version: 1.7.0

Any assistance or guidance to resolve this issue would be greatly appreciated!

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Hi! You are mixing things. The attributes you are using #[GQL\Provider] & #[GQL\Query] require a mapping of type attributes:

 mappings:
 auto_discover: false
 types:
 - type: attribute
 dir: "%kernel.project_dir%/src/{GraphQL}"
 suffix: ~

The directory you indicate in the yaml in the mapping is the folder where to look for the services marked with the attributes (it will autodetect the providers, the queries & mutations).

The way you configured your service with - { name: 'overblog_graphql.resolver', method: 'hello' } is the other way of doing thing where you declare resolvers services but you have to map them on your entities with YAML configuration.

The mapping graphql you are using is experimental and lack a lot of features, you shouldn't not use it.

Hope it helps!

You must be logged in to vote
1 reply
Comment options

Problem didn't solve.

overblog_graphql:
 definitions:
 schema:
 query: Query
 mappings:
 auto_discover: false
 types:
 - type: attribute
 dir: "%kernel.project_dir%/src/GraphQL/Resolver"
 suffix: ~
 - type: graphql
 dir: "%kernel.project_dir%/src/GraphQL/Schema"
 suffix: ~
 
 services:
 _defaults:
 autowire: true # Automatically injects dependencies in your services.
 autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
 App\GraphQL\Resolver\:
 resource: '../src/GraphQL/Resolver/*'
 tags: [ 'overblog_graphql.resolver' ]
 public: true
 
 App\Service\:
 resource: '../src/Service'
 public: true

use App\Service\Interface\IUserService;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Annotation as GQL;

#[GQL\Provider]
final readonly class UserResolver
{
 public function __construct(private IUserService $userService)
 {
 }
 #[GQL\Query(name: "user", type: "User")]
 public function user(#[GQL\Arg(name: "id", type: "ID!")] int $id, ResolveInfo $resolveInfo)
 {
 $requestSelectedFields = $this->getSelectedFields($resolveInfo);
 return $this->userService->getUserById($id, $requestSelectedFields);
 }
 #[GQL\Query(name: "users", type: "[User]")]
 public function users(ResolveInfo $resolveInfo): array
 {
 $requestSelectedFields = $this->getSelectedFields($resolveInfo);
 return $this->userService->getAllUsers($requestSelectedFields);
 }
 private function getSelectedFields(ResolveInfo $resolveInfo): array
 {
 $fields = [];
 foreach ($resolveInfo->fieldNodes as $fieldNode) {
 if (isset($fieldNode->selectionSet)) {
 foreach ($fieldNode->selectionSet->selections as $selection) {
 $fields[] = $selection->name->value;
 }
 }
 }
 return $fields;
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /