Skip to content

How to register ScalarTypes? #681

@brunobg

Description

@brunobg

How do I resolve extended ScalarTypes? https://webonyx.github.io/graphql-php/type-system/scalar-types/ explains how to create a class for them and the tests show how to ]build a Schema manually with them

'd' => ['type' => $ComplexScalarType],
, but I can't find how to load them with a parser.

Concrete example, trying to parse this GraphQL:

scalar TestScalarType @scalar(class: "ModelariumTests\\TestScalarType")

schema {
  query: TestType
}

input TestInputObject {
  t: TestScalarType
}

type TestType {
  fieldWithObjectInput(input: TestInputObject): String
  fieldWithScalarInput(input: TestScalarType): TestScalarType
}

The idea is to use the directive to resolve the class. I'm parsing it like this:

class TestScalarType extends ScalarType { ... }

class MyTest {
    public function testObjectParserQuery()
    {
        $document = Parser::parse(
            'query q($input: TestInputObject) {
            fieldWithObjectInput(input: $input)
        }'
        );

        $data = file_get_contents('test.graphql'); // loads the schema above
        $ast = \GraphQL\Language\Parser::parse($data);
        $schemaBuilder = new \GraphQL\Utils\BuildSchema(
            $ast,
            [__CLASS__, 'extendDatatypes']
        );
        $schema = $schemaBuilder->buildSchema();

        $result = Executor::execute($schema, $document, null, null, ['input' => [ 't' => 'valid']]);

        $this->assertEquals([], $result->errors);
        $expected = [
            'data' => ['fieldWithObjectInput' => '{"t":"xvalid"}'],
        ];
        $this->assertEquals($expected, $result->toArray());
    }

extendDatatypes is called for TestType, but I don't see how to load the scalar classes dynamically. Any hints please?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    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