question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom scalars vs custom schema directives

See original GitHub issue

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:open
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
FluorescentHallucinogencommented, Apr 10, 2019

@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?

0reactions
robross0606commented, Mar 4, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found