Skip to content
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

Add JSON schema / Type support to props_template #16

Open
jho406 opened this issue Apr 15, 2024 · 0 comments
Open

Add JSON schema / Type support to props_template #16

jho406 opened this issue Apr 15, 2024 · 0 comments

Comments

@jho406
Copy link
Contributor

jho406 commented Apr 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant