1

I have custom dynamically created Enum type MyCustomEnum witch I need to use in my ServiceProvider. For example I call Type string now Type::string():

<?php

namespace App\Providers;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Schema\TypeRegistry;
use GraphQL\Type\Definition\EnumType;


class GraphQLServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @param TypeRegistry $typeRegistry
     *
     * @return void
     */
    public function boot(TypeRegistry $typeRegistry): void
    {
        $typeRegistry->register(
            new ObjectType([
                'name' => 'MyOtherCustomType',
                'fields' => function () use ($typeRegistry): array{
                    return [
                        'my_field' => Type::string()
                    ];
                },
            ])
        );
    }
}

How I can call this dynamically created type MyCustomEnum on line 'my_field' => ... ?

Vyacheslav
  • 59
  • 5
  • I'm not sure you can, Enums in simple terms is just a pretty way of handling unsigned int (yeah there is namespace support but at its basic level it's just an unsigned int comparator) in PHP, the compiler will change them to binary numbers for comparison, – Barkermn01 Sep 02 '22 at 14:43
  • Maybe I didn't put the question correctly. The question is exactly how can I call the previously created dynamically custom type? For example, Type::customType('MyCustomEnum') – Vyacheslav Sep 02 '22 at 14:50
  • you can't an enum is not a type, it's an array, I have given a detailed explanation here, https://pastebin.com/DHc8EzVZ but it does not answer your question because it depends on what your using the enum for, i hope that helps you work it out, for us to need to see you `MyCustomEnum` definition – Barkermn01 Sep 02 '22 at 14:56
  • I will clarify, the type is created dynamically and is described in GraphQL scheme. Example: enum MyCustomEnum { CREATED @enum(value: "created") PENDING @enum(value: "pending") } – Vyacheslav Sep 02 '22 at 15:04
  • It has exactly the same thing you must define the Enum to have a type and not just a default, https://webonyx.github.io/graphql-php/type-definitions/enums/#field-resolution All enums in every programming lang that supports them, they are arrays, if you want a specific type, you must define the values in the enums of a specific type and then the type of the value of the options of an enum is the type the enum values are. – Barkermn01 Sep 02 '22 at 15:07

2 Answers2

0

Enums are not a Type, they do not get implemented like class or struct in any way they are true arrays, when your pass an enum, you don't actually pass the enum you pass one of the values the enum has registered to it, now this can be hidden if you don't supply values. to not be hidden when you define the enum you must supply the value for each option and then the values, therefore, have a type.

E.G

$episodeEnum = new EnumType([
    'name' => 'Episode',
    'description' => 'One of the films in the Star Wars Trilogy',
    'values' => [
        'NEWHOPE' => [
            'value' => 4,
            'description' => 'Released in 1977.'
        ],
        'EMPIRE' => [
            'value' => 5,
            'description' => 'Released in 1980.'
        ],
        'JEDI' => [
            'value' => 6,
            'description' => 'Released in 1983.'
        ],
    ]
]);

Now the enum Episode always has a type of int because the 3 options of the enum all have values that are ints, so the type of that enum value is an int. Therefore anything that uses that Episode Enum, they have to supply what value of the enum they want to save(E.G Episode.NEWHOPE) and that is enum value's actual value is actually what is saved (so the last E.G would actually save 4 and is there for an int), and that defines the type of what is saved/transferred, it is the type of the value.

Barkermn01
  • 6,781
  • 33
  • 83
0

I have a php enum class named CountryEnum which has a static method called graphEnumType(). This method returns an array with this shape. I register it in AppServiceProvider like this so it can be used with graphql:

$typeRegistry->register(CountryEnum::graphEnumType());

Inside the php i treat it like a php enum and call it like this:

CountryEnum::AT->value;
mostafa
  • 196
  • 4
  • Do you know how I can use this created type? For example like `GraphQL::type('CountryEnum')`, but this doesn't work – Vyacheslav Sep 02 '22 at 15:37
  • [Please check here](https://webonyx.github.io/graphql-php/type-definitions/enums/#construction-from-php-enum), I didn't test it but maybe something like this works, `$countryEnumType = new PhpEnumType(CountryEnum::class); ` then `'type' => $countryEnumType` – mostafa Sep 05 '22 at 07:37