Custom scalars vs custom schema directives
See original GitHub issueIssue Description
At the moment, I’m working on a GraphQL API architecture for my app. And I’m interested in pros and cons in using custom scalars vs. custom schema directives for validation (in the limitations and potential flexibility of each of these approaches).
See, e.g. @okgrow/graphql-scalars for a library of custom GraphQL scalar types for creating precise type-safe GraphQL schemas.
@confuser Did I understand correctly that @constraint
directive from graphql-constraint-directive
package can only be applied to INPUT_FIELD_DEFINITION
, while there are many places where directives (in general) can be applied (see the full list here: https://github.com/graphql/graphql-js/blob/master/src/language/directiveLocation.js)?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Custom scalars - Apollo GraphQL Docs
Defining a custom scalar You can now use MyCustomScalar in your schema anywhere you can use a default scalar (e.g., as the type...
Read more >Custom Scalars and Enums – GraphQL Tools
To enable this, GraphQL allows you to define custom scalar types. Enumerations are similar to custom scalars, but their values can only be...
Read more >GraphQL directives are underrated - LogRocket Blog
Schema-type directives are private. They're used by the GraphQL schema admins/designers/owners to configure the schema and are not exposed to ...
Read more >Schemas and Types - GraphQL
The most basic components of a GraphQL schema are object types, which just represent a kind of ... there is also a way...
Read more >Apollo GraphQL Custom Directives - E.Y. - Medium
@deprecated is a schema type directive used to mark a field as deprecated in the SDL, and can only be added to FIELD_DEFINITION...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@confuser
As for the my custom scalars vs. custom schema directives for validation question, I’ve deep dived into how this package works and realized that it uses custom scalar types under the hood (wraps the field to which the directive is applied into custom scalars). So these are essentially equivalent things. Right?
@FluorescentHallucinogen, this is my understanding as well. From what I can tell, the directives are run through a GraphQL transform using
graphql-toosl/utils
mapSchema()
and end up being replaced by generated scalars.