forked from apollographql/apollo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy over boilerplate from relay runtime query
- Loading branch information
Sashko Stubailo
committed
Mar 17, 2016
1 parent
8775cd9
commit ef4a25c
Showing
11 changed files
with
2,920 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
|
||
# don't commit compiled files | ||
lib |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Ben Newman | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "relay-runtime-query", | ||
"version": "0.7.1", | ||
"description": "Compile your Relay GraphQL queries at runtime, no need for Babel. Only for use in development.", | ||
"main": "./lib/index.js", | ||
"jsnext:main": "./src/index.js", | ||
"scripts": { | ||
"test": "mocha --reporter spec --full-trace test/index.js", | ||
"compile": "babel --presets es2015,stage-0 -d lib/ src/", | ||
"prepublish": "npm run compile" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "meteor/relay-runtime-query" | ||
}, | ||
"keywords": [ | ||
"ecmascript", | ||
"es2015", | ||
"jsnext", | ||
"javascript", | ||
"relay", | ||
"npm", | ||
"react" | ||
], | ||
"author": "Sashko Stubailo <sashko@stubailo.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"babel-polyfill": "^6.5.0", | ||
"babel-relay-plugin": "0.7.1", | ||
"graphql": "^0.4.17", | ||
"inherits": "^2.0.1", | ||
"react": "^0.14.7", | ||
"react-dom": "^0.14.7", | ||
"react-relay": "0.7.1", | ||
"util": "^0.10.3" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "6.3.21", | ||
"babel-loader": "6.2.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.5.0", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"graphql": "^0.4.17", | ||
"graphql-relay": "^0.3.6", | ||
"lodash": "^4.5.1", | ||
"mocha": "^2.3.3" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import RelayQLTransformer from 'babel-relay-plugin/lib/RelayQLTransformer'; | ||
const {utilities_buildClientSchema: {buildClientSchema}} = require('babel-relay-plugin/lib/GraphQL'); | ||
import invariant from 'babel-relay-plugin/lib/invariant'; | ||
import RelayQLPrinter from 'babel-relay-plugin/lib/RelayQLPrinter'; | ||
import { introspectionQuery } from 'graphql/utilities/introspectionQuery'; | ||
import Relay from 'react-relay'; | ||
import generateHash from 'babel-relay-plugin/lib/generateHash'; | ||
|
||
function getSchema(schemaProvider: GraphQLSchemaProvider): GraphQLSchema { | ||
const introspection = typeof schemaProvider === 'function' ? | ||
schemaProvider() : | ||
schemaProvider; | ||
invariant( | ||
typeof introspection === 'object' && introspection && | ||
typeof introspection.__schema === 'object' && introspection.__schema, | ||
'Invalid introspection data supplied to `getBabelRelayPlugin()`. The ' + | ||
'resulting schema is not an object with a `__schema` property.' | ||
); | ||
return buildClientSchema(introspection); | ||
} | ||
|
||
let fragmentIndex = 0; | ||
const fragmentCache = {}; | ||
|
||
function encodeFragmentIndex(index) { | ||
return '$$$' + index + '$$$'; | ||
} | ||
|
||
const t = { | ||
arrayExpression(array) { | ||
return array; | ||
}, | ||
nullLiteral() { | ||
return null; | ||
}, | ||
valueToNode(value) { | ||
return value; | ||
}, | ||
objectExpression(propertyArray) { | ||
const obj = {}; | ||
|
||
propertyArray.forEach((property) => { | ||
if (property.value.__identifier) { | ||
throw new Error("Don't support identifiers yet"); | ||
} | ||
|
||
obj[property.key] = property.value; | ||
}); | ||
|
||
return obj; | ||
}, | ||
identifier(identifierName) { | ||
return { | ||
__identifier: identifierName | ||
}; | ||
}, | ||
objectProperty(nameIdentifier, value) { | ||
return { | ||
key: nameIdentifier.__identifier, | ||
value: value | ||
}; | ||
}, | ||
|
||
// Only used once, to return a definition object in `print` | ||
returnStatement(expressionToReturn) { | ||
return { | ||
__fakeReturnStatement: expressionToReturn | ||
}; | ||
}, | ||
|
||
// Used twice - for runtime errors, and to return a definition object in `print` | ||
blockStatement(arrayOfStatements) { | ||
return { | ||
__fakeBlockStatement: arrayOfStatements | ||
}; | ||
}, | ||
|
||
functionExpression(name, substitutionIdentifiers, printedDocumentReturnBlockStatement) { | ||
const query = printedDocumentReturnBlockStatement.__fakeBlockStatement[0].__fakeReturnStatement; | ||
|
||
const querySubstitutionFunction = function () { | ||
return query; | ||
} | ||
|
||
return querySubstitutionFunction; | ||
}, | ||
|
||
callExpression(func, args) { | ||
// Try to hackily identify shallowFlatten | ||
if (args && args.length === 2) { | ||
return [].concat.apply([], args[1]); | ||
} | ||
|
||
if (args && args.length > 0) { throw new Error("Args not implemented lol") } | ||
|
||
return func(); | ||
}, | ||
|
||
memberExpression(members) { | ||
return { | ||
__fakeMemberExpression: members | ||
}; | ||
} | ||
}; | ||
|
||
export function initTemplateStringTransformer(schemaJson) { | ||
const schema = getSchema(schemaJson); | ||
const transformer = new RelayQLTransformer(schema, {}); | ||
|
||
function templateStringTag(quasis, ...expressions) { | ||
const processedTemplateLiteral = processTemplateLiteral(quasis, expressions, 'queryName'); | ||
|
||
const processedTemplateText = transformer.processTemplateText(processedTemplateLiteral.templateText, { | ||
documentName: 'queryName', | ||
propName: 'propName' | ||
}); | ||
|
||
const definition = transformer.processDocumentText(processedTemplateText, { | ||
documentName: 'queryName', | ||
propName: 'propName', | ||
fragmentLocationID: generateHash(JSON.stringify(processedTemplateText)).substring(0, 12) | ||
}); | ||
|
||
const options = {}; | ||
const Printer = RelayQLPrinter(t, options); | ||
|
||
modifyPrinterClass(Printer); | ||
|
||
const printed = new Printer('wtf??', {}) | ||
.print(definition, []); | ||
|
||
return printed; | ||
} | ||
|
||
return templateStringTag; | ||
} | ||
|
||
// Attempted lift from https://github.com/facebook/relay/blob/0be965c3c92c48499b452e953d823837838df962/scripts/babel-relay-plugin/src/RelayQLTransformer.js#L114-L148 | ||
// Returns { substitutions, templateText, variableNames } | ||
// Who knows why they are called quasis?? | ||
function processTemplateLiteral(quasis, expressions, documentName) { | ||
const chunks = []; | ||
const variableNames = {}; | ||
const substitutions = []; | ||
|
||
quasis.forEach((chunk, ii) => { | ||
chunks.push(chunk); | ||
|
||
if (ii !== quasis.length - 1) { | ||
const name = 'RQL_' + ii; | ||
const value = expressions[ii]; | ||
|
||
runtime.fragments[name] = value; | ||
|
||
substitutions.push({name, value}); | ||
|
||
if (/:\s*$/.test(chunk)) { | ||
invariant( | ||
false, // this.options.substituteVariables, | ||
'You supplied a GraphQL document named `%s` that uses template ' + | ||
'substitution for an argument value, but variable substitution ' + | ||
'has not been enabled.', | ||
documentName | ||
); | ||
chunks.push('$' + name); | ||
variableNames[name] = undefined; | ||
} else { | ||
chunks.push('...' + name); | ||
} | ||
} | ||
}); | ||
|
||
return {substitutions, templateText: chunks.join('').trim(), variableNames}; | ||
} | ||
|
||
// Override certain functions on the printer | ||
function modifyPrinterClass(printer) { | ||
printer.prototype.printFragmentReference = function (fragmentReference) { | ||
return [].concat.apply([], [Relay.QL.__frag(runtime.getFragment(fragmentReference.getName()))]); | ||
} | ||
} | ||
|
||
const runtime = { | ||
fragments: {}, | ||
getFragment(name) { | ||
const frag = this.fragments[name]; | ||
delete this.fragments[name]; | ||
return frag; | ||
} | ||
} | ||
|
||
// Eventually improve this to support old services like GraphiQL does. | ||
export function initTemplateStringTransformerFromUrl(url, callback) { | ||
graphQLFetcher(url, { query: introspectionQuery }).then(result => { | ||
const schemaJson = result.data; | ||
callback(initTemplateStringTransformer(schemaJson)); | ||
}); | ||
} | ||
|
||
function graphQLFetcher(url, graphQLParams) { | ||
return fetch(url, { | ||
method: 'post', | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(graphQLParams), | ||
credentials: 'include', | ||
}).then(function (response) { | ||
return response.text(); | ||
}).then(function (responseBody) { | ||
try { | ||
return JSON.parse(responseBody); | ||
} catch (error) { | ||
return responseBody; | ||
} | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var getbabelRelayPlugin = require('babel-relay-plugin'); | ||
var schema = require('./starwars.json'); | ||
|
||
module.exports = getbabelRelayPlugin(schema.data); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// This file cannot be written with ECMAScript 2015 because it has to load | ||
// the Babel require hook to enable ECMAScript 2015 features! | ||
require("babel-core/register"); | ||
require("babel-polyfill"); | ||
|
||
// The tests, however, can and should be written with ECMAScript 2015. | ||
require("./tests.js"); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { StarWarsSchema } from './starWarsSchema.js'; | ||
import { graphql } from 'graphql'; | ||
import { introspectionQuery } from 'graphql/utilities/introspectionQuery'; | ||
|
||
export async function introspectStarwars() { | ||
return await graphql(StarWarsSchema, introspectionQuery); | ||
} |
Oops, something went wrong.