I'm trying to migrate from using a Rails REST API to using GraphQL. One of our REST endpoints that we're trying to replace uses a serializer. In that serializer, we return either a string or a boolean, depending on if the string is "true" or "false". I'm having trouble creating a graphql object that can handle this. I've tried creating a custom type, and a custom scalar to no avail. I'm trying to use rspec to test out the graphql query, but when I try using a custom scalar or custom type, i keep getting a runtime error that says:
no implicit conversion of nil into String
The unhelpful part is I don't have a good way of debugging what is causing this error.
- is there an easy way to define a graphQL field that will return either a boolean or a string?
- does anyone have any suggestions on how to debug the error I'm getting via rspec? I've tried using
byebugin different places, but thebyebugstatement is never getting hit.
Here's an example of a serializer I'm trying to convert to graphql
class ModelToSerialize < ActiveModel::Serializer
attributes :id, :customization_key, :value
# value is stored as a string in the relational database
# customization_key is stored as an integer in the relational database
def value
if object.customization_key == 1
return true if object.value == true || object.value =~ /^(true|t|yes|y|1)$/i
return false
else
object.value
end
end
end
Here's the GraphQL type that I tried to create:
module Types
class ModelToSerializeType < Types::BaseObject
field :id, Int, null: false
field :value, String, null: false # this should return boolean OR string
field :customization_key, Int, null: false, camelize: false
end
end
The above doesn't work because I'm limiting the type to be String, but I want it to return either a boolean or a string. Even if I copy/paste the code from the serializer into the type definition, it will still only return a string