-
-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Give devs a way to make use of typescript in superglue. Currently building responses using props_template does not support types. To support types, a developer would have to manually create type definitions for their page responses and separately use props_template to generate responses. This two step process is better than nothing, but not as smooth as something like graphql-ruby where types are automatically generated.
How can we do better?
Examples in the wild:
https://github.com/bullet-train-co/jbuilder-schema
https://graphql-ruby.org/
Suggestion:
Remove or optionally turn off the magic of method_missing and allow PropsTemplate to accept a JSON Schema and dynamically create subclasses of Props::Template that explicitly define setter methods that match a schema's properties.
Assuming we have :
# app/schemas/person_show.json
{
"type": "object",
"properties": {
"firstName": {
"type": "string",
},
"age": {
"type": "number"
},
"address": { "$ref": "#/$defs/address" }
},
"$defs": {
"address": {
"type": "object"
"properties": {
"zipCode": {
"type": "string",
}
}
}
}
}
and we somehow pass this to form_props in an initializer, or set it at the controller level:
class PersonController < ApplicationController
success_schema_for :show, :person_show
def show
end
end
Then the following will happen in show.json.props
# Thoughts: `json` would be an instance of a class that explicitly defines
# firstName and age
json.firstName "john"
json.age 18
# The below will raise an error
json.lastName "smith"
json.address do
# Thoughts: `json` would be an instance of a class that explicitly defines
# zipCode
json.zipCode "11233"
#This will raise an error
json.firstName "jill"
end
We would also have to add a new generator to superglue_rails that adds respective schemas.
Typescript to JSONSchema.
I'm selecting JSON Schema here because it seems like we can allow user to define types in typescript using typebox , generate the schema, and build the ruby objects.
Its the opposite of graphql-ruby. Instead of starting with ruby to generate the types in typescript. I'm suggesting the opposite, write the types in typescript, and generate the setter interface for props_template.