Skip to content

Add JSON schema / Type support to props_template #16

@jho406

Description

@jho406

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions