-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
Provide an obvious way to register custom scalar type implementations #20
Comments
Hi @itkach, sorry for the late answer. The reason why it works in the GraphQL.js example is because it sets the type like this: Object.assign(schema._typeMap.MyScalar, MyScalar) Note that this is different from simply setting schema.type_map["MyScalar"].__dict__.update(MyScalar.__dict__) Or, you can do it like this: my_scalar = schema.type_map["MyScalar"]
my_scalar.serialize = ...
my_scalar.pare_literal = ...
my_scalar.pare_value = ... Or, create a function that copies these methods from one GraphQLScalarType to another. |
@Cito Thank you for the explanation, I see now how it works. It's unfortunate that GraphQL.js thinks that mutating guts of an object inside of a not-quite-public other object is an acceptable API. I understand that graphql-core-next is a port, but perhaps there's a room for a better API here? |
@itkach There is actually a better API for creating executable schemas from SDL, but it's in graphql-tools which is not part of GraphQL.js. It provides a function It would be good to have something like that in Python, either as a separate package like graphql-tools or as a subpackage of graphql-core-next. I will create a ticket to move that forward. |
But note that you can of course build executable schemas directly in graphql-core-next using the provided classes, as I have shown in the blog article above. This is considered the primary way of building them in graphql-core-next. |
I was following "create schema from SDL" approach in https://cito.github.io/blog/shakespeare-with-graphql/ and wanted to use custom scalar types. Looks like in GraphQL.js custom scalar type implementation can be set by simply assigning in type map (https://stackoverflow.com/questions/47824603/graphql-custom-scalar-definition-without-graphql-tools), so equivalent here would be something like this
This doesn't work though as schema.type_map is not consulted when serializing output types or parsing arguments. The following workaround using
extend_schema
seems to produce the desired effect of assigning custom scalar type implementation to a scalar name declared in schema and having it used when serializing and parsing:and then use it like this:
Surely this is a hack and there has to be a better way?
The text was updated successfully, but these errors were encountered: