babel-relay-plugin
Advanced tools
Comparing version 0.2.3 to 0.2.4
{ | ||
"name": "babel-relay-plugin", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Babel Relay Plugin for transpiling GraphQL queries for use with Relay.", | ||
@@ -5,0 +5,0 @@ "license": "BSD-3-Clause", |
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
@@ -9,0 +9,0 @@ |
@@ -56,14 +56,4 @@ /** | ||
var Plugin = babel.Plugin; | ||
var parse = babel.parse; | ||
var t = babel.types; | ||
var traverse = babel.traverse; | ||
/** | ||
* Same as `babel.util.parseTemplate`. | ||
*/ | ||
function parseTemplate(loc, code) { | ||
var ast = parse(code, {filename: loc, looseModules: true}).program; | ||
return traverse.removeProperties(ast); | ||
} | ||
return new Plugin('relay-query', { | ||
@@ -185,6 +175,13 @@ visitor: { | ||
); | ||
code = ( | ||
'function() { throw new Error(\'' + | ||
message.replace(/\'/g, '\\\'') + | ||
'\'); }' | ||
code = t.functionExpression( | ||
null, | ||
[], | ||
t.blockStatement([ | ||
t.throwStatement( | ||
t.newExpression( | ||
t.identifier('Error'), | ||
[t.literal(message)] | ||
) | ||
) | ||
]) | ||
); | ||
@@ -203,15 +200,6 @@ | ||
} | ||
code = '(' + code + ')'; | ||
var funcExpr = parseTemplate('Relay.QL', code).body[0].expression; | ||
// Immediately invoke the function with substitutions as arguments. | ||
var substitutions = node.quasi.expressions; | ||
var funcCall = t.callExpression( | ||
t.functionExpression( | ||
null, | ||
funcExpr.params, | ||
funcExpr.body | ||
), | ||
substitutions | ||
); | ||
var funcCall = t.callExpression(code, substitutions); | ||
this.replaceWith(funcCall); | ||
@@ -218,0 +206,0 @@ } |
@@ -1,4 +0,12 @@ | ||
/*jslint node:true*/ | ||
"use strict"; | ||
/** | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -5,0 +13,0 @@ var formatError = require('graphql/error').formatError; |
@@ -1,6 +0,15 @@ | ||
/*jslint node:true*/ | ||
"use strict"; | ||
/** | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
var kinds = require('graphql/language/kinds'); | ||
var printer = require('graphql/language/printer'); | ||
var t = require('babel-core').types; | ||
var types = require('graphql/type'); | ||
@@ -15,2 +24,4 @@ var typeIntrospection = require('graphql/type/introspection'); | ||
var NULL = t.literal(null); | ||
/** | ||
@@ -32,2 +43,3 @@ * This is part of the Babel transform to convert embedded GraphQL RFC to | ||
var printedDocument; | ||
switch (ast.kind) { | ||
@@ -37,30 +49,41 @@ case kinds.OPERATION_DEFINITION: | ||
case 'query': | ||
return printQuery(ast, options); | ||
printedDocument = printQuery(ast, options); | ||
break; | ||
case 'mutation': | ||
return printOperation(ast, options); | ||
printedDocument = printOperation(ast, options); | ||
break; | ||
} | ||
break; | ||
case kinds.FRAGMENT_DEFINITION: | ||
return printQueryFragment(ast, options); | ||
printedDocument = printQueryFragment(ast, options); | ||
break; | ||
} | ||
throw new Error('unexpected type: ' + ast.kind); | ||
if (!printedDocument) { | ||
throw new Error('unexpected type: ' + ast.kind); | ||
} | ||
return t.functionExpression( | ||
null, | ||
options.substitutions.map(function(sub) { | ||
return t.identifier(sub); | ||
}), | ||
t.blockStatement([ | ||
t.variableDeclaration( | ||
'var', | ||
[ | ||
t.variableDeclarator( | ||
t.identifier('GraphQL'), | ||
t.memberExpression( | ||
identify(options.rqlFunctionName), | ||
t.identifier('__GraphQL') | ||
) | ||
) | ||
] | ||
), | ||
t.returnStatement(printedDocument), | ||
]) | ||
); | ||
}; | ||
function printQueryFragment(fragment, options) { | ||
var argsCode = getFragmentCode(fragment, options); | ||
var substitutionNames = options.substitutions.join(', '); | ||
return [ | ||
'function(' + substitutionNames + ') {', | ||
'var GraphQL = ' + options.rqlFunctionName + '.__GraphQL;', | ||
'return new GraphQL.QueryFragment(' + argsCode + ');', | ||
'}' | ||
].join(''); | ||
} | ||
function printInlineFragment(fragment, options) { | ||
var argsCode = getFragmentCode(fragment, options); | ||
return 'new GraphQL.QueryFragment(' + argsCode + ')'; | ||
} | ||
function getFragmentCode(fragment, options) { | ||
var typeName = getTypeName(fragment); | ||
@@ -84,16 +107,21 @@ var type = options.schema.getType(typeName); | ||
); | ||
var fields = fieldsAndFragments.fields; | ||
var fragments = fieldsAndFragments.fragments; | ||
var directives = printDirectives(fragment.directives); | ||
var metadata = getRelayDirectiveMetadata(fragment); | ||
var metadataCode = stringifyObject(metadata); | ||
return getFunctionArgCode([ | ||
JSON.stringify(getName(fragment)), | ||
JSON.stringify(getTypeName(fragment)), | ||
fields, | ||
fragments, | ||
metadataCode | ||
]); | ||
return t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier('QueryFragment') | ||
), | ||
trimArguments([ | ||
t.literal(getName(fragment)), | ||
t.literal(getTypeName(fragment)), | ||
fields, | ||
fragments, | ||
objectify(metadata), | ||
directives | ||
]) | ||
); | ||
} | ||
@@ -128,3 +156,3 @@ | ||
var callArgsCode = printArguments(rootField.arguments[0], options); | ||
var printedArgs = printArguments(rootField.arguments[0], options); | ||
@@ -140,3 +168,3 @@ var fieldsAndFragments = printFieldsAndFragments( | ||
var fragments = fieldsAndFragments.fragments; | ||
var directives = printDirectives(rootField.directives); | ||
var metadata = {}; | ||
@@ -159,20 +187,16 @@ | ||
var metadataCode = stringifyObject(metadata); | ||
var argsCode = getFunctionArgCode([ | ||
JSON.stringify(getName(rootField)), | ||
callArgsCode, | ||
fields, | ||
fragments, | ||
metadataCode, | ||
JSON.stringify(getName(query)) | ||
]); | ||
var substitutionNames = options.substitutions.join(', '); | ||
return ( | ||
'function(' + substitutionNames + ') {' + | ||
'var GraphQL = ' + options.rqlFunctionName + '.__GraphQL;' + | ||
'return new GraphQL.Query(' + argsCode + ');' + | ||
'}' | ||
return t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier('Query') | ||
), | ||
trimArguments([ | ||
t.literal(getName(rootField)), | ||
printedArgs, | ||
fields, | ||
fragments, | ||
objectify(metadata), | ||
t.literal(getName(query)), | ||
directives | ||
]) | ||
); | ||
@@ -206,9 +230,12 @@ } | ||
var callCode = | ||
'new GraphQL.Callv(' + | ||
getFunctionArgCode([ | ||
JSON.stringify(getName(rootField)), | ||
var printedCall = t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier('Callv') | ||
), | ||
trimArguments([ | ||
t.literal(getName(rootField)), | ||
printCallVariable('input') | ||
]) + | ||
')'; | ||
]) | ||
); | ||
@@ -234,18 +261,15 @@ if (field.args.length !== 1) { | ||
var argsCode = getFunctionArgCode([ | ||
JSON.stringify(getName(operation)), | ||
JSON.stringify(type.name), | ||
callCode, | ||
fields, | ||
fragments, | ||
stringifyObject(metadata) | ||
]); | ||
var substitutionNames = options.substitutions.join(', '); | ||
return ( | ||
'function(' + substitutionNames + ') {' + | ||
'var GraphQL = ' + options.rqlFunctionName + '.__GraphQL;' + | ||
'return new GraphQL.' + className + '(' + argsCode + ');' + | ||
'}' | ||
return t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier(className) | ||
), | ||
trimArguments([ | ||
t.literal(getName(operation)), | ||
t.literal(type.name), | ||
printedCall, | ||
fields, | ||
fragments, | ||
objectify(metadata) | ||
]) | ||
); | ||
@@ -267,5 +291,11 @@ } | ||
// We assume that all spreads were added by us | ||
if (selection.directives && selection.directives.length) { | ||
throw new Error( | ||
'Directives are not yet supported for `${fragment}`-style ' + | ||
'fragment references.' | ||
); | ||
} | ||
fragments.push(printFragmentReference(getName(selection), options)); | ||
} else if (selection.kind === kinds.INLINE_FRAGMENT) { | ||
fragments.push(printInlineFragment(selection, options)); | ||
fragments.push(printQueryFragment(selection, options)); | ||
} else if (selection.kind === kinds.FIELD) { | ||
@@ -281,37 +311,41 @@ fields.push(selection); | ||
} | ||
var fragmentsCode = null; | ||
if (fragments.length) { | ||
fragmentsCode = '[' + fragments.join(',') + ']'; | ||
} | ||
return { | ||
fields: printFields(fields, type, options, requisiteFields, parentType), | ||
fragments: fragmentsCode, | ||
fragments: fragments.length ? | ||
t.arrayExpression(fragments) : | ||
NULL, | ||
}; | ||
} | ||
function printArguments(args, options) { | ||
function printArguments(args) { | ||
if (!args) { | ||
return null; | ||
return NULL; | ||
} | ||
var value = args.value; | ||
if (value.kind === kinds.LIST) { | ||
return '[' + value.values.map(function(arg) { | ||
return printArgument(arg, options) | ||
}).join(', ') + ']'; | ||
return t.arrayExpression( | ||
value.values.map(function(arg) { | ||
return printArgument(arg); | ||
}) | ||
); | ||
} else { | ||
return printArgument(value, options); | ||
return printArgument(value); | ||
} | ||
} | ||
function printArgument(arg, options) { | ||
function printArgument(arg) { | ||
var value; | ||
switch (arg.kind) { | ||
case kinds.INT: | ||
return JSON.stringify(parseInt(arg.value, 10)); | ||
value = parseInt(arg.value, 10); | ||
break; | ||
case kinds.FLOAT: | ||
return JSON.stringify(parseFloat(arg.value)); | ||
value = parseFloat(arg.value); | ||
break; | ||
case kinds.STRING: | ||
case kinds.ENUM: | ||
case kinds.BOOLEAN: | ||
return JSON.stringify(arg.value); | ||
value = arg.value; | ||
break; | ||
case kinds.VARIABLE: | ||
@@ -325,8 +359,25 @@ if (!arg.name || arg.name.kind !== kinds.NAME) { | ||
} | ||
return printCallValue(value); | ||
} | ||
function printCallVariable(name) { | ||
return 'new GraphQL.CallVariable(' + JSON.stringify(name) + ')'; | ||
return t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier('CallVariable') | ||
), | ||
[t.literal(name)] | ||
); | ||
} | ||
function printCallValue(value) { | ||
return t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier('CallValue') | ||
), | ||
[t.literal(value)] | ||
); | ||
} | ||
function printFields(fields, type, options, requisiteFields, parentType) { | ||
@@ -338,3 +389,3 @@ var generateFields = {}; | ||
var fieldStrings = fields.map(function(field) { | ||
var printedFields = fields.map(function(field) { | ||
var fieldName = getName(field); | ||
@@ -351,14 +402,20 @@ delete generateFields[fieldName]; | ||
}; | ||
fieldStrings.push( | ||
printedFields.push( | ||
printField(generatedAST, type, options, requisiteFields, true, parentType) | ||
); | ||
}); | ||
if (fieldStrings.length === 0) { | ||
return null; | ||
if (printedFields.length === 0) { | ||
return NULL; | ||
} | ||
return '[' + fieldStrings.join(', ') + ']'; | ||
return t.arrayExpression(printedFields); | ||
} | ||
function printFragmentReference(substitutionName, options) { | ||
return options.rqlFunctionName + '.__frag(' + substitutionName + ')'; | ||
return t.callExpression( | ||
t.memberExpression( | ||
identify(options.rqlFunctionName), | ||
t.identifier('__frag') | ||
), | ||
[t.identifier(substitutionName)] | ||
); | ||
} | ||
@@ -418,4 +475,5 @@ | ||
'Connections arguments `%s(before: <cursor>, first: <count>)` are ' + | ||
'not supported. Use `(first: <count>)` or ' + | ||
'`(after: <cursor>, first: <count>)`. ', | ||
'not supported. Use `(first: <count>)`, ' + | ||
'`(after: <cursor>, first: <count>)`, or ' + | ||
'`(before: <cursor>, last: <count>)`.', | ||
fieldName | ||
@@ -427,4 +485,5 @@ )); | ||
'Connections arguments `%s(after: <cursor>, last: <count>)` are ' + | ||
'not supported. Use `(last: <count>)` or ' + | ||
'`(before: <cursor>, last: <count>)`. ', | ||
'not supported. Use `(last: <count>)`, ' + | ||
'`(before: <cursor>, last: <count>)`, or ' + | ||
'`(after: <cursor>, first: <count>)`.', | ||
fieldName | ||
@@ -487,2 +546,3 @@ )); | ||
var fragments = fieldsAndFragments.fragments; | ||
var directives = printDirectives(field.directives); | ||
@@ -496,20 +556,50 @@ if (isGenerated) { | ||
var callsCode = printCalls(field, fieldDecl, options); | ||
var calls = printCalls(field, fieldDecl); | ||
var fieldAlias = field.alias ? field.alias.value : null; | ||
var fieldAliasCode = field.alias ? | ||
JSON.stringify(field.alias.value) : | ||
null; | ||
var metadataCode = stringifyObject(metadata); | ||
return t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier('Field') | ||
), | ||
trimArguments([ | ||
t.literal(fieldName), | ||
fields, | ||
fragments, | ||
calls, | ||
t.literal(fieldAlias), | ||
NULL, | ||
objectify(metadata), | ||
directives | ||
]) | ||
); | ||
} | ||
var argsCode = getFunctionArgCode([ | ||
JSON.stringify(fieldName), | ||
fields, | ||
fragments, | ||
callsCode, | ||
fieldAliasCode, | ||
null, | ||
metadataCode | ||
]); | ||
return 'new GraphQL.Field(' + argsCode + ')'; | ||
function printDirectives(directives) { | ||
if (!directives || !directives.length) { | ||
return NULL; | ||
} | ||
var printedDirectives; | ||
directives.forEach(function(directive) { | ||
var name = getName(directive); | ||
if (name === 'relay') { | ||
return; | ||
} | ||
printedDirectives = printedDirectives || []; | ||
printedDirectives.push(t.objectExpression([ | ||
property('name', t.literal(getName(directive))), | ||
property('arguments', t.arrayExpression( | ||
directive.arguments.map(function(argument) { | ||
return t.objectExpression([ | ||
property('name', t.literal(getName(argument))), | ||
property('value', printArgument(argument.value)), | ||
]); | ||
}) | ||
)), | ||
])); | ||
}); | ||
if (!printedDirectives) { | ||
return NULL; | ||
} | ||
return t.arrayExpression(printedDirectives); | ||
} | ||
@@ -519,3 +609,3 @@ | ||
if (field.arguments.length === 0) { | ||
return null; | ||
return NULL; | ||
} | ||
@@ -541,13 +631,15 @@ | ||
} | ||
return ( | ||
'new GraphQL.Callv(' + | ||
getFunctionArgCode([ | ||
JSON.stringify(callName), | ||
printArguments(arg, options), | ||
stringifyObject(metadata), | ||
]) + | ||
')' | ||
return t.newExpression( | ||
t.memberExpression( | ||
t.identifier('GraphQL'), | ||
t.identifier('Callv') | ||
), | ||
trimArguments([ | ||
t.literal(callName), | ||
printArguments(arg), | ||
objectify(metadata) | ||
]) | ||
); | ||
}); | ||
return '[' + callStrings.join(', ') + ']'; | ||
return t.arrayExpression(callStrings); | ||
} | ||
@@ -788,31 +880,46 @@ | ||
function trimArray(arr) { | ||
var lastIndex = -1; | ||
for (var ii = arr.length - 1; ii >= 0; ii--) { | ||
if (arr[ii] !== null) { | ||
lastIndex = ii; | ||
break; | ||
} | ||
function objectify(obj) { | ||
if (obj == null) { | ||
return NULL; | ||
} | ||
arr.length = lastIndex + 1; | ||
return arr; | ||
var keys = Object.keys(obj); | ||
if (!keys.length) { | ||
return NULL; | ||
} | ||
return t.objectExpression( | ||
keys.map(function(key) { | ||
return property(key, t.literal(obj[key])); | ||
}) | ||
); | ||
} | ||
function stringifyObject(obj) { | ||
for (var ii in obj) { | ||
if (obj.hasOwnProperty(ii)) { | ||
return JSON.stringify(obj); | ||
function identify(str) { | ||
return str.split('.').reduce(function(acc, name) { | ||
if (!acc) { | ||
return t.identifier(name); | ||
} | ||
} | ||
return null; | ||
return t.memberExpression(acc, t.identifier(name)); | ||
}, null); | ||
} | ||
function getFunctionArgCode(arr) { | ||
return trimArray(arr) | ||
.map(function(arg) { | ||
return arg === null ? 'null' : arg; | ||
}) | ||
.join(', '); | ||
function property(name, value) { | ||
return t.property('init', t.identifier(name), value); | ||
} | ||
function trimArguments(args) { | ||
var lastIndex = -1; | ||
for (var ii = args.length - 1; ii >= 0; ii--) { | ||
if (args[ii] == null) { | ||
throw new Error( | ||
'Use `NULL` to indicate that output should be the literal value `null`.' | ||
); | ||
} | ||
if (args[ii] !== NULL) { | ||
lastIndex = ii; | ||
break; | ||
} | ||
} | ||
return args.slice(0, lastIndex + 1); | ||
} | ||
function getSelections(node) { | ||
@@ -819,0 +926,0 @@ if (node.selectionSet && node.selectionSet.selections) { |
@@ -0,1 +1,10 @@ | ||
/** | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
@@ -2,0 +11,0 @@ |
@@ -0,1 +1,10 @@ | ||
/** | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
@@ -2,0 +11,0 @@ |
@@ -0,1 +1,10 @@ | ||
/** | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
@@ -2,0 +11,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
163930
64
3972
0