Giter Site home page Giter Site logo

Comments (6)

smyrick avatar smyrick commented on May 23, 2024

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

from graphql-kotlin.

dariuszkuc avatar dariuszkuc commented on May 23, 2024

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.

from graphql-kotlin.

pzavtoni avatar pzavtoni commented on May 23, 2024

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

from graphql-kotlin.

dariuszkuc avatar dariuszkuc commented on May 23, 2024

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.

from graphql-kotlin.

pzavtoni avatar pzavtoni commented on May 23, 2024

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

from graphql-kotlin.

dariuszkuc avatar dariuszkuc commented on May 23, 2024

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

from graphql-kotlin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.