Steve Willard
Steve Willard

Reputation: 16737

Dynamic GraphQL schema generation with relay

Is it possible to not use the babel-relay-plugin with Relay? I want to dynamically generate GraphQL schemas, whereas Relay seems to want to know the entire type system at compile time. Is it possible to defer this until runtime?

Anyone know what exactly babel-relay-plugin is doing? I really want to use it to transform the queries and combine fragments... but I'm willing to live without type checking and validation if it allows me to dynamically construct the queries.

On that note, has anyone used this? https://github.com/gyzerok/adrenaline

Maybe I'll just use this to craft queries.

Upvotes: 1

Views: 1374

Answers (1)

Greg Hurrell
Greg Hurrell

Reputation: 5437

In the typical scenario, Relay needs the schema at compile time as you've noted. Many aspects of the design are predicated on being able to do things (like generate queries) before rendering at runtime.

However, it is possible to hack things and get some runtime dynamism; for example, consider the "Playground" on the Relay website which allows you to define a schema in the browser and run code against it.

It does this with some nasty hacks, but basically boils down to calling Babel and evaling the result:

var {code} = babel.transform(source, {code: true, ast: false});
return eval(code);

Also instructive to look at: how it updates the schema and it's use of the relay-local-schema package.

Upvotes: 2

Related Questions