Skip to content

Empty graphql response #1265

Description

@Kagayakashi

I have 2 entities. Object and Field.
Object has many fields.
When i trying to get simple object data without fields it works:

query {
    select_objects(process_id: 7) {
        id,
        process_id,
        name,
        num,
    }
}

When i trying to get all object data, it return empty response with no error message:

query {
    select_objects(process_id: 7) {
        id,
        process_id,
        name,
        num,
        fields {
         id
        }
    }
}

I think the problem is like in syntax or fatal error. But I cant see it in response and find it by myself.

Object Graphql Type

class ObjectGraphqlType extends ObjectType
{
    /**
     * Returns Graphql Type as full field structure for adding it into Query.
     *
     * @return array
     */
    public static function getAsQueryField(): array
    {
        $field = [
            "select_objects" => [
                "type" => Type::listOf(new ObjectGraphqlType),
                "args" => [
                    'process_id' => [
                        'type' => Type::int(),
                    ],
                ],
                "resolve" => static function (array $root, array $args): array {
                    if(isset($args["process_id"])) return ObjectEntity::getObjectsByProcessId($args["process_id"]);

                    return [];
                }
            ]
        ];
        return $field;
    }

    public function __construct()
    {
        parent::__construct([
            'fields' => static fn (): array => [
                'id' => Type::int(),
                'process_id' => Type::int(),
                'name' => Type::string(),
                'num' => Type::string(),
                'fields' => Type::listOf(new FieldGraphqlType),
            ],
            'resolveField' => function ($root, $args, $context, ResolveInfo $info) {
                $field_name = $info->fieldName;
                $method = 'resolve' . ucfirst($field_name);

                if (method_exists($this, $method)) {
                    return $this->{$method}($root);
                }

                return $root->{$field_name};
            },
        ]);
    }

    /**
     * Get fields by process id for object
     * @param ObjectGraphqlType $args
     */
    public function resolveFields(ObjectGraphqlType $root): array
    {
        return FieldEntity::getFieldsByProcessId($root->process_id);
    }
}

Object Model

class ObjectEntity
{
    public function __construct(
        public readonly int $id,
        public readonly int $process_id,
        public readonly string $name,
        public readonly string $num,
        public readonly array $fields = [],
    ) {
        
    }

    public static function getObjectsByProcessId(int $process_id): array
    {
        $elems = [
            [
                "id" => 1,
                "process_id" => $process_id,
                "name" => "Object 1",
                "num" => "AB22040314", 
            ],
            [
                "id" => 2,
                "process_id" => $process_id,
                "name" => "Test object 2",
                "num" => "AB22040319", 
            ],
        ];

        switch ($process_id) {
            case 7:
                # return dummy data
                $data = $elems;
                break;
            
            default:
                $data = [];
                break;
        }

        $element_objects = [];

        foreach ($data as $element) {
            $element_objects[] = new static(
                id: $element["id"],
                process_id: $element["process_id"],
                num: $element["num"],
                name: $element["name"],
            );
        }

        return $element_objects;
    }
}

Field Graphql Type

class FieldGraphqlType extends ObjectType
{
    public function __construct()
    {        
        parent::__construct([
            'fields' => static fn (): array => [
                'id' => Type::int(),
                'name' => Type::string(),
                'process_id' => Type::int(),
                'variable' => Type::string(),
            ],
            'resolveField' => function ($root, $args, $context, ResolveInfo $info) {
                $field_name = $info->fieldName;
                return $root->{$field_name};
            },
        ]);
    }
}

Field Model

class FieldEntity
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
        public readonly int $process_id,
        public readonly string $variable,
    ) {
        
    }

    public static function getFieldsByProcessId(int $process_id): array
    {
        $elems = [
            [
                "id" => 1,
                "process_id" => $process_id,
                "name" => "field 1",
                "variable" => "o_field_first",  
            ],
            [
                "id" => 2,
                "process_id" => $process_id,
                "name" => "field field 2",
                "variable" => "o_field_second",  
            ],
            [
                "id" => 3,
                "process_id" => $process_id,
                "name" => "Field 3rd",
                "variable" => "o_field_third",  
            ],
            [
                "id" => 4,
                "process_id" => $process_id,
                "name" => "fourth field",
                "variable" => "o_field_fourth",  
            ],
        ];

        switch ($process_id) {
            case 7:
                # Keep test dummy data
                $data = $elems;
                break;
            
            default:
                $data = [];
                break;
        }

        $element_objects = [];

        foreach ($data as $element) {
            $element_objects[] = new static(
                id: $element["id"],
                name: $element["name"],
                process_id: $element["process_id"],
                variable: $element["variable"],
            );
        }

        return $element_objects;
    }
}

In StandardServer i have enabled debug flags:
DebugFlag::RETHROW_UNSAFE_EXCEPTIONS | DebugFlag::RETHROW_UNSAFE_EXCEPTIONS

Without debug at all there is internal error response, but i dont understand what the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions