Skip to content

Instantly share code, notes, and snippets.

@dispalt
Last active January 10, 2017 06:04
Show Gist options
  • Save dispalt/fe980b222a5531deb7791def15f9ceb2 to your computer and use it in GitHub Desktop.
Save dispalt/fe980b222a5531deb7791def15f9ceb2 to your computer and use it in GitHub Desktop.
/*
This is a macro that takes a GQL statement and produces js. I achieve this
by forking out to node and executing the babelRelayPlugin. This solution is sub-par for
3 big reasons:
1) Naming - Since it executes outside the "scope" (aka independently). I have to do some error prone things.
* use the js.constructorOf[FooClass] and assign it to a global variable like global.FooClass = js.constructorOf...
2) Static stuff, which is due for sjs inclusion, so not huge.
3) Eval - It turns into strings that get evaluated... Is there a better way to integrate?
Potential better solutions:
*) Generate a node module, then programatically require that? using the @JSImport stuff
*) Do some post processing or use string substitution to use rql" .. ${fragment[Class]("name")} ..."
fragment = some marker for which to execute the js.constructorOf[C], maybe like contextual?
???
*/
// Notice no string interpolation
val j: js.Object = RelayQL("""
|fragment on Quote {
| id
| step1 {
| zip
| }
| ${FooClass.getFragment('quote')}
| ${BarClass.getFragment('quote')}
| ${BazClass.getFragment('quote')}
|}
""".stripMargin)
// Generated code
scala.scalajs.js.eval("(function (RQL_0, RQL_1, RQL_2) {
return {
children: [].concat.apply([], [{
fieldName: 'id',
kind: 'Field',
metadata: {
isRequisite: true
},
type: 'ID'
}, {
children: [{
fieldName: 'zip',
kind: 'Field',
metadata: {},
type: 'String'
}],
fieldName: 'step1',
kind: 'Field',
metadata: {
canHaveSubselections: true
},
type: 'QuoteStart'
}, Relay.QL.__frag(RQL_0), Relay.QL.__frag(RQL_1), Relay.QL.__frag(RQL_2)]),
id: Relay.QL.__id(),
kind: 'Fragment',
metadata: {},
name: 'UnknownRelayQL',
type: 'Quote'
};
})(FooClass.getFragment('quote'), BarClass.getFragment('quote'), BazClass.getFragment('quote'));")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment