Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to set custom scalars for specific class properties of the same type? #226

Closed
pzavtoni opened this issue May 23, 2019 · 6 comments

Comments

@pzavtoni
Copy link

Let's say we have this class as an example:
data class Salary( val employer: Employer? = null, val amount: Int? = null, val month: Int? = null, )

When defining .graphql files for this type it is also possible to use Scalars for month and amount. In my case there is a Scalar used for amount that validates that value is a positive one and for month there is a Scalar that allows only values from 1 to 12.

By setting scalars in this way we can see in the schema the exact type used for this fields and also the description for each scalars.

In CustomSchemaGeneratorHooks we can define scalars only by type and I can't see any solution by using it for my purpose.

Is there any other solution for this?
If there is not any solution at the moment would it be possible to implement such a feature?

@smyrick
Copy link
Contributor

smyrick commented May 23, 2019

Yes it is! We have an example of doing this same validation by using a URL type from Java which is converted to a GraphQL String. https://github.com/ExpediaDotCom/graphql-kotlin/wiki/Scalars#custom-scalars

So here you could create a custom data class Month(val value: Int) and have the validation in the constructor or in the coercing code

@dariuszkuc
Copy link
Collaborator

One thing to keep in mind though that if you do create custom scalars all your clients will also need to know how to properly serialize/deserialize those objects.

Another approach is to add some validations on the server that will return validation error to the user.

@pzavtoni
Copy link
Author

pzavtoni commented May 25, 2019

So here you could create a custom data class Month(val value: Int) and have the validation in the constructor or in the coercing code

@smyrick thanks for your reply, I took this possibility in account but this will mess up some logic that is already implemented...

I am just curious if this could be done without any additional classes for such custom scalars

@dariuszkuc
Copy link
Collaborator

Custom scalars need to be defined in their own classes/types - without it how would you distinguish between a regular Int and "restricted" Int value for your custom scalar?

You could always do some validations on the server manually and return GraphQLError if input is invalid. Another alternative is to use GraphQL Directives to customize the behavior.

@pzavtoni
Copy link
Author

@dkuc84 I am making use of validations already and it works, thanks.

I was thinking of an implementation like it's done with @GraphQLID annotation.

@dariuszkuc
Copy link
Collaborator

@GraphQLID converts the field to a GraphQL ID scalar type which is a built-in type. It has a special meaning (see the docs) but it doesn't perform any validation of the fields.

Even if you were to create a custom annotation that would convert your Int input to a custom scalar it would still be exposed in your GraphQL schema as a custom scalar type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants