Defining schemas and queries dynamically


I’ve been reading through some forum threads and github issues about compile-time schema definitions topic.

I’m trying to achieve the following flow: a user can define some object with some fields (for example a post object with title field) via UI in the browser, then submit this form, right after it should be possible to send a mutation which would create a new post with the given title and finally, it should be possible to query this post. As I understand, for this to work, one would need to dynamically define a new object with a new field in it, a new mutation and a query with a resolver.

I’m wondering if a) it’s feasible at all (using metaprogramming or better purely using Absinthe functions) and b) anybody managed to achieve somewhat similar functionality.


We’ve done something like this but only in support of a local development tool. We generate code and recompile the schema every time we detect a change that needs to be accounted for (in fact there are a chain of modules we have to recompile in our case). I don’t know if it’d work in a release.

But, I’ll say this, it was a fraught and difficult path and we still hit bugs and edge cases, some of which we’ve yet to fully account for. The instability is ok for us because this is a local testing tool that is extremely valuable to us even with bugs, but I would not want to do it in production :grimacing: I consider this mad science.

If someone has found a better way to do this dynamic stuff at runtime I’ll be curious on a purely academic level, but it’s also worth considering other approaches to the problem.

So I guess tl;dr: yes we pulled it off, and also I would never ship it :joy:

1 Like

Poking around in the Absinthe internals, it seems like all the Schema machinery is ultimately about producing Pipeline structs:

I don’t think it would be easy to write code to produce those structs dynamically (instead of using the Notation macros) but there’s no reason it would be impossible either. That approach would avoid some of the headaches of trying to generate complex macros and compile them on-the-fly.

1 Like