Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-relay-plugin

Package Overview
Dependencies
Maintainers
8
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-relay-plugin - npm Package Compare versions

Comparing version 0.9.0 to 0.9.1

lib/computeLocation.js

60

lib/getBabelRelayPlugin.js

@@ -18,2 +18,4 @@ // @generated

var computeLocation = require('./computeLocation');
var _require = require('./GraphQL');

@@ -24,2 +26,3 @@

var RelayQLTransformer = require('./RelayQLTransformer');
var RelayTransformError = require('./RelayTransformError');
var babelAdapter = require('./babelAdapter');

@@ -124,31 +127,42 @@ var invariant = require('./invariant');

} catch (error) {
// Print a console warning and replace the code with a function
// that will immediately throw an error in the browser.
var sourceText = error.sourceText;
var validationErrors = error.validationErrors;
var basename = state.file.opts.basename || 'UnknownFile';
var filename = state.file.opts.filename || 'UnknownFile';
var errorMessages = [];
var isValidationError = !!(validationErrors && sourceText);
if (isValidationError) {
var sourceLines = sourceText.split('\n');
validationErrors.forEach(function (_ref3) {
var message = _ref3.message;
var locations = _ref3.locations;
errorMessages.push(message);
warning('\n-- GraphQL Validation Error -- %s --\n', basename);
warning(['File: ' + filename, 'Error: ' + message, 'Source:'].join('\n'));
locations.forEach(function (location) {
var preview = sourceLines[location.line - 1];
if (preview) {
warning(['> ', '> ' + preview, '> ' + ' '.repeat(location.column - 1) + '^^^'].join('\n'));
}
});
});
} else {
if (error instanceof RelayTransformError) {
errorMessages.push(error.message);
warning('\n-- Relay Transform Error -- %s --\n', basename);
warning(['File: ' + filename, 'Error: ' + error.stack].join('\n'));
var sourceLine = node.quasi.loc.start.line;
var relative_loc = computeLocation(error.loc);
if (relative_loc) {
warning(['Within RelayQLDocument ' + filename + ':' + sourceLine, '> ', '> line ' + relative_loc.line + ' (approximate)', '> ' + relative_loc.source, '> ' + ' '.repeat(relative_loc.column - 1) + '^^^', 'Error: ' + error.message, 'Stack: ' + error.stack].join('\n'));
}
} else {
// Print a console warning and replace the code with a function
// that will immediately throw an error in the browser.
var sourceText = error.sourceText;
var validationErrors = error.validationErrors;
var isValidationError = !!(validationErrors && sourceText);
if (isValidationError) {
var sourceLines = sourceText.split('\n');
validationErrors.forEach(function (_ref3) {
var message = _ref3.message;
var locations = _ref3.locations;
errorMessages.push(message);
warning('\n-- GraphQL Validation Error -- %s --\n', basename);
warning(['File: ' + filename, 'Error: ' + message, 'Source:'].join('\n'));
locations.forEach(function (location) {
var preview = sourceLines[location.line - 1];
if (preview) {
warning(['> ', '> ' + preview, '> ' + ' '.repeat(location.column - 1) + '^^^'].join('\n'));
}
});
});
} else {
errorMessages.push(error.message);
warning('\n-- Relay Transform Error -- %s --\n', basename);
warning(['File: ' + filename, 'Error: ' + error.stack].join('\n'));
}
}

@@ -155,0 +169,0 @@ var runtimeMessage = util.format('%s error ``%s`` in file `%s`. Try updating your GraphQL ' + 'schema if an argument/field/type was recently added.', isValidationError ? 'GraphQL validation' : 'Relay transform', errorMessages.join(' '), filename);

@@ -39,2 +39,4 @@ // @generated

var invariant = require('./invariant');
var util = require('util');
var RelayTransformError = require('./RelayTransformError');

@@ -55,2 +57,7 @@ var GraphQLRelayDirectiveInstance = new GraphQLDirectiveClass(GraphQLRelayDirective);

_createClass(RelayQLNode, [{
key: 'getLocation',
value: function getLocation() {
return this.ast.loc;
}
}, {
key: 'getType',

@@ -94,3 +101,3 @@ value: function getType() {

} else {
invariant(false, 'Unexpected selection kind: %s', selection.kind);
throw new RelayTransformError(util.format('Unexpected selection kind: %s', selection.kind), _this.getLocation());
}

@@ -180,3 +187,5 @@ });

var _name = this.getName();
invariant(_name, 'Static fragments require a name. Use `fragment NAME on %s { ... }`.', this.getType().getName({ modifiers: true }));
if (!_name) {
throw new RelayTransformError(util.format('Static fragments require a name. Use `fragment NAME on %s { ... }`.', this.getType().getName({ modifiers: true })), this.getLocation());
}
this.staticFragmentID = _name + ':' + suffix;

@@ -198,6 +207,8 @@ }

// Inline fragments without type conditions fall back to parent type.
invariant(this.parentType, 'Cannot get type of typeless inline fragment without parent type.');
if (!this.parentType) {
throw new RelayTransformError('Cannot get type of typeless inline fragment without parent type.', this.getLocation());
}
return this.parentType;
} else {
invariant(false, 'Unexpected fragment kind: %s', this.ast.kind);
throw new RelayTransformError(util.format('Unexpected fragment kind: %s', this.ast.kind), this.getLocation());
}

@@ -277,3 +288,5 @@ }

var fieldDef = parentType.getFieldDefinition(fieldName, ast);
invariant(fieldDef, 'You supplied a field named `%s` on type `%s`, but no such field ' + 'exists on that type.', fieldName, parentType.getName({ modifiers: false }));
if (!fieldDef) {
throw new RelayTransformError(util.format('You supplied a field named `%s` on type `%s`, but no such field ' + 'exists on that type.', fieldName, parentType.getName({ modifiers: false })), _this8.getLocation());
}
_this8.fieldDef = fieldDef;

@@ -321,3 +334,5 @@ return _this8;

var argType = argTypes[argName];
invariant(argType, 'You supplied an argument named `%s` on field `%s`, but no such ' + 'argument exists on that field.', argName, _this9.getName());
if (!argType) {
throw new RelayTransformError(util.format('You supplied an argument named `%s` on field `%s`, but no such ' + 'argument exists on that field.', argName, _this9.getName()), _this9.getLocation());
}
return new RelayQLArgument(_this9.context, arg, argType);

@@ -363,3 +378,3 @@ });

value: function getSelections() {
invariant(false, 'Cannot get selection of a fragment spread.');
throw new RelayTransformError('Cannot get selection of a fragment spread.', this.getLocation());
}

@@ -405,3 +420,5 @@ }]);

var schemaDirective = directiveName === GraphQLRelayDirective.name ? GraphQLRelayDirectiveInstance : context.schema.getDirective(directiveName);
invariant(schemaDirective, 'You supplied a directive named `%s`, but no such directive exists.', directiveName);
if (!schemaDirective) {
throw new RelayTransformError(util.format('You supplied a directive named `%s`, but no such directive exists.', directiveName), this.getLocation());
}
schemaDirective.args.forEach(function (schemaArg) {

@@ -413,2 +430,7 @@ _this12.argTypes[schemaArg.name] = new RelayQLArgumentType(schemaArg.type);

_createClass(RelayQLDirective, [{
key: 'getLocation',
value: function getLocation() {
return this.ast.loc;
}
}, {
key: 'getName',

@@ -426,3 +448,5 @@ value: function getName() {

var argType = _this13.argTypes[argName];
invariant(argType, 'You supplied an argument named `%s` on directive `%s`, but no ' + 'such argument exists on that directive.', argName, _this13.getName());
if (!argType) {
throw new RelayTransformError(util.format('You supplied an argument named `%s` on directive `%s`, but no ' + 'such argument exists on that directive.', argName, _this13.getName()), _this13.getLocation());
}
return new RelayQLArgument(_this13.context, arg, argType);

@@ -446,2 +470,7 @@ });

_createClass(RelayQLArgument, [{
key: 'getLocation',
value: function getLocation() {
return this.ast.loc;
}
}, {
key: 'getName',

@@ -464,3 +493,5 @@ value: function getName() {

value: function getVariableName() {
invariant(this.ast.value.kind === 'Variable', 'Cannot get variable name of an argument value.');
if (this.ast.value.kind !== 'Variable') {
throw new RelayTransformError('Cannot get variable name of an argument value.', this.getLocation());
}
return this.ast.value.name.value;

@@ -473,3 +504,6 @@ }

invariant(!this.isVariable(), 'Cannot get value of an argument variable.');
if (this.isVariable()) {
throw new RelayTransformError('Cannot get value of an argument variable.', this.getLocation());
}
var value = this.ast.value;

@@ -861,5 +895,5 @@ if (value.kind === 'ListValue') {

case 'Variable':
invariant(false, 'Unexpected nested variable `%s`; variables are supported as top-' + 'level arguments - `node(id: $id)` - or directly within lists - ' + '`nodes(ids: [$id])`.', value.name.value);
throw new RelayTransformError(util.format('Unexpected nested variable `%s`; variables are supported as top-' + 'level arguments - `node(id: $id)` - or directly within lists - ' + '`nodes(ids: [$id])`.', value.name.value), value.loc);
default:
invariant(false, 'Unexpected value kind: %s', value.kind);
throw new RelayTransformError(util.format('Unexpected value kind: %s', value.kind), value.loc);
}

@@ -866,0 +900,0 @@ }

@@ -44,2 +44,4 @@ // @generated

var invariant = require('./invariant');
var util = require('util');
var RelayTransformError = require('./RelayTransformError');

@@ -96,3 +98,3 @@ module.exports = function (t, options) {

} else {
invariant(false, 'Unsupported definition: %s', definition);
throw new RelayTransformError(util.format('Unsupported definition: %s', definition), definition.getLocation());
}

@@ -109,3 +111,5 @@ return t.callExpression(t.functionExpression(null, substitutions.map(function (substitution) {

var rootFields = query.getFields();
invariant(rootFields.length === 1, 'There are %d fields supplied to the query named `%s`, but queries ' + 'must have exactly one field.', rootFields.length, query.getName());
if (rootFields.length !== 1) {
throw new RelayTransformError(util.format('There are %d fields supplied to the query named `%s`, but queries ' + 'must have exactly one field.', rootFields.length, query.getName()), query.getLocation());
}
var rootField = rootFields[0];

@@ -131,3 +135,6 @@ var rootFieldType = rootField.getType();

}
invariant(rootFieldArgs.length <= 1, 'Invalid root field `%s`; Relay only supports root fields with zero ' + 'or one argument.', rootField.getName());
if (rootFieldArgs.length > 1) {
throw new RelayTransformError(util.format('Invalid root field `%s`; Relay only supports root fields with zero ' + 'or one argument.', rootField.getName()), query.getLocation());
}
var calls = NULL;

@@ -205,4 +212,5 @@ if (rootFieldArgs.length === 1) {

var selectVariablesValue = selectVariables.getValue();
invariant(Array.isArray(selectVariablesValue), 'The variables argument to the @relay directive should be an array ' + 'of strings.');
if (!Array.isArray(selectVariablesValue)) {
throw new RelayTransformError('The variables argument to the @relay directive should be an array ' + 'of strings.', fragment.getLocation());
}
return t.callExpression(t.memberExpression(identify(this.tagName), t.identifier('__createFragment')), [fragmentCode, t.objectExpression(selectVariablesValue.map(function (item) {

@@ -230,3 +238,5 @@ var value = item.getValue();

var rootFields = mutation.getFields();
invariant(rootFields.length === 1, 'There are %d fields supplied to the mutation named `%s`, but ' + 'mutations must have exactly one field.', rootFields.length, mutation.getName());
if (rootFields.length !== 1) {
throw new RelayTransformError(util.format('There are %d fields supplied to the mutation named `%s`, but ' + 'mutations must have exactly one field.', rootFields.length, mutation.getName()), mutation.getLocation());
}
var rootField = rootFields[0];

@@ -263,3 +273,5 @@ var rootFieldType = rootField.getType();

var rootFields = subscription.getFields();
invariant(rootFields.length === 1, 'There are %d fields supplied to the subscription named `%s`, but ' + 'subscriptions must have exactly one field.', rootFields.length, subscription.getName());
if (rootFields.length !== 1) {
throw new RelayTransformError(util.format('There are %d fields supplied to the subscription named `%s`, but ' + 'subscriptions must have exactly one field.', rootFields.length, subscription.getName()), subscription.getLocation());
}
var rootField = rootFields[0];

@@ -305,3 +317,5 @@ var rootFieldType = rootField.getType();

// Assume that all spreads exist via template substitution.
invariant(selection.getDirectives().length === 0, 'Directives are not yet supported for `${fragment}`-style fragment ' + 'references.');
if (selection.getDirectives().length !== 0) {
throw new RelayTransformError('Directives are not yet supported for `${fragment}`-style fragment ' + 'references.', selection.getLocation());
}
printedFragments.push(_this2.printFragmentReference(selection));

@@ -314,3 +328,3 @@ didPrintFragmentReference = true;

} else {
invariant(false, 'Unsupported selection type `%s`.', selection);
throw new RelayTransformError(util.format('Unsupported selection type `%s`.', selection), selection.getLocation());
}

@@ -522,3 +536,3 @@ });

if (arg.isVariable()) {
invariant(!arg.isVariable(), 'You supplied `$%s` as the `%s` argument to the `@relay` ' + 'directive, but `@relay` require scalar argument values.', arg.getVariableName(), arg.getName());
throw new RelayTransformError(util.format('You supplied `$%s` as the `%s` argument to the `@relay` ' + 'directive, but `@relay` require scalar argument values.', arg.getVariableName(), arg.getName()), node.getLocation());
}

@@ -584,3 +598,5 @@ if (arg.getName() !== 'variables') {

var argNames = Object.keys(argTypes);
invariant(argNames.length !== 1 || argNames[0] !== 'id', 'You defined a `node(id: %s)` field on type `%s`, but Relay requires ' + 'the `node` field to be defined on the root type. See the Object ' + 'Identification Guide: \n' + 'http://facebook.github.io/relay/docs/graphql-object-identification.html', argNames[0] && argTypes[argNames[0]].getName({ modifiers: true }), parentType.getName({ modifiers: false }));
if (argNames.length === 1 && argNames[0] === 'id') {
throw new RelayTransformError(util.format('You defined a `node(id: %s)` field on type `%s`, but Relay requires ' + 'the `node` field to be defined on the root type. See the Object ' + 'Identification Guide: \n' + 'http://facebook.github.io/relay/docs/graphql-object-identification.html', argNames[0] && argTypes[argNames[0]].getName({ modifiers: true }), parentType.getName({ modifiers: false })), field.getLocation());
}
}

@@ -595,5 +611,14 @@ }

invariant(!first || !last || first.isVariable() && last.isVariable(), 'Connection arguments `%s(first: <count>, last: <count>)` are ' + 'not supported unless both are variables. Use `(first: <count>)`, ' + '`(last: <count>)`, or `(first: $<var>, last: $<var>)`.', field.getName());
invariant(!first || !before || first.isVariable() && before.isVariable(), 'Connection arguments `%s(before: <cursor>, first: <count>)` are ' + 'not supported unless both are variables. Use `(first: <count>)`, ' + '`(after: <cursor>, first: <count>)`, `(before: <cursor>, last: <count>)`, ' + 'or `(before: $<var>, first: $<var>)`.', field.getName());
invariant(!last || !after || last.isVariable() && after.isVariable(), 'Connection arguments `%s(after: <cursor>, last: <count>)` are ' + 'not supported unless both are variables. Use `(last: <count>)`, ' + '`(before: <cursor>, last: <count>)`, `(after: <cursor>, first: <count>)`, ' + 'or `(after: $<var>, last: $<var>)`.', field.getName());
var condition = !first || !last || first.isVariable() && last.isVariable();
if (!condition) {
throw new RelayTransformError(util.format('Connection arguments `%s(first: <count>, last: <count>)` are ' + 'not supported unless both are variables. Use `(first: <count>)`, ' + '`(last: <count>)`, or `(first: $<var>, last: $<var>)`.', field.getName()), field.getLocation());
}
condition = !first || !before || first.isVariable() && before.isVariable();
if (!condition) {
throw new RelayTransformError(util.format('Connection arguments `%s(before: <cursor>, first: <count>)` are ' + 'not supported unless both are variables. Use `(first: <count>)`, ' + '`(after: <cursor>, first: <count>)`, `(before: <cursor>, last: <count>)`, ' + 'or `(before: $<var>, first: $<var>)`.', field.getName()), field.getLocation());
}
condition = !last || !after || last.isVariable() && after.isVariable();
if (!condition) {
throw new RelayTransformError(util.format('Connection arguments `%s(after: <cursor>, last: <count>)` are ' + 'not supported unless both are variables. Use `(last: <count>)`, ' + '`(before: <cursor>, last: <count>)`, `(after: <cursor>, first: <count>)`, ' + 'or `(after: $<var>, last: $<var>)`.', field.getName()), field.getLocation());
}

@@ -606,3 +631,7 @@ // Use `any` because we already check `isConnection` before validating.

if (subfield.getName() === FIELDS.edges || subfield.getName() === FIELDS.pageInfo) {
invariant(field.isPattern() || field.hasArgument('find') || field.hasArgument('first') || field.hasArgument('last'), 'You supplied the `%s` field on a connection named `%s`, but you did ' + 'not supply an argument necessary to do so. Use either the `find`, ' + '`first`, or `last` argument.', subfield.getName(), field.getName());
var _condition = field.isPattern() || field.hasArgument('find') || field.hasArgument('first') || field.hasArgument('last');
if (!_condition) {
throw new RelayTransformError(util.format('You supplied the `%s` field on a connection named `%s`, but you did ' + 'not supply an argument necessary to do so. Use either the `find`, ' + '`first`, or `last` argument.', subfield.getName(), field.getName()), field.getLocation());
}
} else {

@@ -612,3 +641,6 @@ // Suggest `edges{node{...}}` instead of `nodes{...}`.

var isNodesLikeField = subfieldType.isList() && subfieldType.getName({ modifiers: false }) === connectionNodeType.getName({ modifiers: false });
invariant(!isNodesLikeField, 'You supplied a field named `%s` on a connection named `%s`, but ' + 'pagination is not supported on connections without using `%s`. ' + 'Use `%s{%s{%s{...}}}` instead.', subfield.getName(), field.getName(), FIELDS.edges, field.getName(), FIELDS.edges, FIELDS.node);
if (isNodesLikeField) {
throw new RelayTransformError(util.format('You supplied a field named `%s` on a connection named `%s`, but ' + 'pagination is not supported on connections without using `%s`. ' + 'Use `%s{%s{%s{...}}}` instead.', subfield.getName(), field.getName(), FIELDS.edges, field.getName(), FIELDS.edges, FIELDS.node), field.getLocation());
}
}

@@ -621,7 +653,14 @@ });

var declaredArgNames = Object.keys(declaredArgs);
invariant(declaredArgNames.length === 1, 'Your schema defines a mutation field `%s` that takes %d arguments, ' + 'but mutation fields must have exactly one argument named `%s`.', rootField.getName(), declaredArgNames.length, INPUT_ARGUMENT_NAME);
invariant(declaredArgNames[0] === INPUT_ARGUMENT_NAME, 'Your schema defines a mutation field `%s` that takes an argument ' + 'named `%s`, but mutation fields must have exactly one argument ' + 'named `%s`.', rootField.getName(), declaredArgNames[0], INPUT_ARGUMENT_NAME);
if (declaredArgNames.length !== 1) {
throw new RelayTransformError(util.format('Your schema defines a mutation field `%s` that takes %d arguments, ' + 'but mutation fields must have exactly one argument named `%s`.', rootField.getName(), declaredArgNames.length, INPUT_ARGUMENT_NAME), rootField.getLocation());
}
if (declaredArgNames[0] !== INPUT_ARGUMENT_NAME) {
throw new RelayTransformError(util.format('Your schema defines a mutation field `%s` that takes an argument ' + 'named `%s`, but mutation fields must have exactly one argument ' + 'named `%s`.', rootField.getName(), declaredArgNames[0], INPUT_ARGUMENT_NAME), rootField.getLocation());
}
var rootFieldArgs = rootField.getArguments();
invariant(rootFieldArgs.length <= 1, 'There are %d arguments supplied to the mutation field named `%s`, ' + 'but mutation fields must have exactly one `%s` argument.', rootFieldArgs.length, rootField.getName(), INPUT_ARGUMENT_NAME);
if (rootFieldArgs.length > 1) {
throw new RelayTransformError(util.format('There are %d arguments supplied to the mutation field named `%s`, ' + 'but mutation fields must have exactly one `%s` argument.', rootFieldArgs.length, rootField.getName(), INPUT_ARGUMENT_NAME), rootField.getLocation());
}
}

@@ -628,0 +667,0 @@

{
"name": "babel-relay-plugin",
"version": "0.9.0",
"version": "0.9.1",
"description": "Babel Relay Plugin for transpiling GraphQL queries for use with Relay.",

@@ -37,3 +37,3 @@ "license": "BSD-3-Clause",

"eslint": "^2.10.2",
"flow-bin": "^0.25.0",
"flow-bin": "^0.27.0",
"glob": "^7.0.3",

@@ -40,0 +40,0 @@ "jest-cli": "^12.1.1",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc