alex996 / graphql-chat

MERN + GraphQL real-time chat app
MIT License
161 stars 49 forks source link

Validation using custom scalar types #2

Open audiBookning opened 6 years ago

audiBookning commented 6 years ago

From your video MERN Stack & GraphQL - #7 Server-Side Validation, it seems that you don't approach the subject of using custom scalar types for validation.

There are already several packages that use that idea:

What are your thought about it?

alex996 commented 5 years ago

@audiBookning Sounds interesting, I didn't think about it. I like graphql-custom-types but I'm not sure if it plays nicely with Apollo. I'd wait to see if this pattern gets a wider adoption.

audiBookning commented 5 years ago

Thanks for the reply.

but I'm not sure if it plays nicely with Apollo.

I looked for your feedback, because i don't really see Custom Scalar types being used or mentioned very much. Which is strange given that the specs of graphql already have this feature since at least 2015: facebook.github.io Scalars, more info also at graphql.org Scalar types

and also from what i can see, they are already implemented on Apollo Server 2 since the beginning: apollographql.com Custom scalars

The problem that could arise would be with Apollo Client. There is a rich issue about the matter: Support for custom scalars and its follow up from the apollo-feature-requests repo TL;DR from Apollo site: '...Note that Apollo Client does not currently have a way to automatically interpret custom scalars, so there’s no way to automatically reverse the serialization on the client...' I haven't tried yet, but it does makes one wonder nonetheless...

I like graphql-custom-types

The packages are nice, but one doesn't really need to be dependent of them. One could transpose your code used for validation with the Joi package to one's own GraphQLScalarType instance easily: apollographql.com Custom GraphQLScalarType instance. In particular see the Date as a scalar example.

I'd wait to see if this pattern gets a wider adoption.

Your doubts are likely similar to mine. But from what i gleaned, i does look like this should be the best practice to do validation (and much more) with Graphql on the server. There must be others considerations that i don't see at the moment...