Skip to content

Instantly share code, notes, and snippets.

@hgwood
Last active November 30, 2016 08:40
Show Gist options
  • Save hgwood/9edeec6448506fb6e7155eac1e401b59 to your computer and use it in GitHub Desktop.
Save hgwood/9edeec6448506fb6e7155eac1e401b59 to your computer and use it in GitHub Desktop.
Example of transforming a GraphQL query into a Falcor query
const assert = require("assert")
const fs = require("fs")
const parser = require("graphql/language/parser")
const _ = require("lodash")
const write = (path, json) => fs.writeFileSync(path, JSON.stringify(json, null, 2))
const inputGraphQl = `
query {
families(name: "Stark") {
name
members {
firstName
}
}
}`
const expectedFalcor = [
["families", "Stark", "name"],
["families", "Stark", "members", "firstName"]
]
const parsedGraphQl = parser.parse(inputGraphQl)
write("parsed.json", parsedGraphQl)
let actualFalcor = []
_.cloneDeepWith(parsedGraphQl, value => {
if (_.get(value, "kind") === "SelectionSet") {
const currentPath = actualFalcor.shift() || []
actualFalcor = actualFalcor.concat(_.map(value.selections, selection => {
const newSegment = [selection.name.value]
if (selection.arguments) _.each(selection.arguments, arg => newSegment.push(arg.value.value))
return currentPath.concat(newSegment)
}).reverse())
}
})
console.log(actualFalcor)
assert.deepStrictEqual(actualFalcor, expectedFalcor)
{
"name": "falgql",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"graphql": "^0.6.0",
"lodash": "^4.13.1"
}
}
@hgwood
Copy link
Author

hgwood commented Nov 30, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment