Flow: Feature request: Type providers

Created on 16 Aug 2015  路  9Comments  路  Source: facebook/flow

It would be really cool to be able to create types from something other than inference/annotations at compile time. In particular, I can think of two use cases:

  1. JSON Schema. It would be really interesting if we could write a type-level function schema(schema: string) -> type, which would take a JSON schema document and output a type. This capability would allow us to use the same schema document on the server/other contexts, while keeping the type checking in sync automatically. We might want to make the argument a path, or have a separate builtin type-level function to read files.
  2. Tagged templates. Currently, tagged templates are pretty smart already, but there's opportunity here too. In particular, I'm thinking of the Relay.QL tagged template.

For nuts and bolts, I was thinking that we could use something like OCaml's Dynlink, so the providers would be OCaml bytecode, which would register their capabilities with some builtin API.

feature request wishlist

Most helpful comment

I've explored RPC-based plugin approach. Seems doable, but hard to predict performance implications.

All 9 comments

Great idea ! F# users love this feature !
Using dynlink is suitable, even if it's pretty low level (You could take a look to ocaml_plugin if you want to see something easier to use).

It鈥檇 be nice if flow understood JSON Schema. Less need to double up on schema/interface declarations.

This would be a huge help. The opposite already exists (https://github.com/seanhess/runtime-types) for converting Flow types to simple JSON. I may start work on a very simple POC that reverses the process - we download schemata from a database on boot and I'd really like to work it into our build process so we can validate everything flowing through the system.

@STRML Any progress on converting JSON schema to Flow types?

Yeah, I have a project working with https://github.com/STRML/json-to-flow. You can create your own templates & hooks if you need custom functionality.

Just wanted to check in on whether anyone had made progress or explored this. I'm very interested in a standard way to do type generation from other schemas like API contracts, GraphQL queries, etc.

I've explored RPC-based plugin approach. Seems doable, but hard to predict performance implications.

Any progress on this one? Is it on some kind of roadmap? If no one else is working on such a thing, I'd consider taking it on as a weekend project (once I've gotten some experience with OCaml).

TIA

I'm looking for contracts (or guards) based on Flow type definitions e.g. I declare a type signature in Flow, and I would be able to do runtime check if JSON response from the server is the same as my expected signature. If not it will return Nothing (from Maybe) or throw an error. This is similar to what they do in Elm. Anybody aware of something like this?

Was this page helpful?
0 / 5 - 0 ratings