Last active
November 30, 2016 08:40
-
-
Save hgwood/9edeec6448506fb6e7155eac1e401b59 to your computer and use it in GitHub Desktop.
Example of transforming a GraphQL query into a Falcor query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "falgql", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"dependencies": { | |
"graphql": "^0.6.0", | |
"lodash": "^4.13.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cleaner version now here : https://github.com/antoinecellier/raccord/blob/master/client/src/translate/graphql-to-falcor.js