drupal-graphql / drupal-graphql/graphql

[4.x] Provide better support for custom scalars

Open
#1,160 0 comments 0 reactions 0 assignees View on GitHub
4.x enhancement feature
Dominant language
PHP
Stars
287
Forks
198
PR merge metrics
No merged PRs in 30d

Description

Custom scalars are a great way to give standard types more meaning and to aid in input validation (e.g. for emails, URLs or timestamps).

A good place to inject these custom scalar types would be in `SdlSchemaPluginBase::getSchema` which has the following snippet.

```php
$schema = BuildSchema::build($document, function ($config, TypeDefinitionNode $type) use ($resolver) {
if ($type instanceof InterfaceTypeDefinitionNode || $type instanceof UnionTypeDefinitionNode) {
$config['resolveType'] = $resolver;
}

return $config;
});
```

The provided definition extender will also be called for custom scalar types. This allows to adjust the `$config` array passed to the `CustomScalarConfig` class in `ASTDefinitionBuilder::makeSchemaDefFromConfig`. However, this requires overwriting the `getSchema` function in the `SdlSchemaPluginBase` class and will require you to specify separate functions in an array.

A more ergonomic way would be to introduce/expose the type registry for the GraphQL module that allows registering custom classes for custom scalars.

I'm not sure if the `ResolverRegistry` would be the best place to accommodate this? It's not really a resolver but more a serializer/deserializer per scalar type.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.