apollo-utilities
Advanced tools
Comparing version 1.0.11 to 1.0.12
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.apollo = global.apollo || {}, global.apollo.utilities = {}))); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.apollo = global.apollo || {}, global.apollo.utilities = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
var __assign = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
var __assign = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
function isScalarValue(value) { | ||
return ['StringValue', 'BooleanValue', 'EnumValue'].indexOf(value.kind) > -1; | ||
} | ||
return t; | ||
}; | ||
function isScalarValue(value) { | ||
return ['StringValue', 'BooleanValue', 'EnumValue'].indexOf(value.kind) > -1; | ||
} | ||
function isNumberValue(value) { | ||
return ['IntValue', 'FloatValue'].indexOf(value.kind) > -1; | ||
} | ||
function isStringValue(value) { | ||
return value.kind === 'StringValue'; | ||
} | ||
function isBooleanValue(value) { | ||
return value.kind === 'BooleanValue'; | ||
} | ||
function isIntValue(value) { | ||
return value.kind === 'IntValue'; | ||
} | ||
function isFloatValue(value) { | ||
return value.kind === 'FloatValue'; | ||
} | ||
function isVariable(value) { | ||
return value.kind === 'Variable'; | ||
} | ||
function isObjectValue(value) { | ||
return value.kind === 'ObjectValue'; | ||
} | ||
function isListValue(value) { | ||
return value.kind === 'ListValue'; | ||
} | ||
function isEnumValue(value) { | ||
return value.kind === 'EnumValue'; | ||
} | ||
function isNullValue(value) { | ||
return value.kind === 'NullValue'; | ||
} | ||
function valueToObjectRepresentation(argObj, name, value, variables) { | ||
if (isIntValue(value) || isFloatValue(value)) { | ||
argObj[name.value] = Number(value.value); | ||
function isNumberValue(value) { | ||
return ['IntValue', 'FloatValue'].indexOf(value.kind) > -1; | ||
} | ||
else if (isBooleanValue(value) || isStringValue(value)) { | ||
argObj[name.value] = value.value; | ||
function isStringValue(value) { | ||
return value.kind === 'StringValue'; | ||
} | ||
else if (isObjectValue(value)) { | ||
var nestedArgObj_1 = {}; | ||
value.fields.map(function (obj) { | ||
return valueToObjectRepresentation(nestedArgObj_1, obj.name, obj.value, variables); | ||
}); | ||
argObj[name.value] = nestedArgObj_1; | ||
function isBooleanValue(value) { | ||
return value.kind === 'BooleanValue'; | ||
} | ||
else if (isVariable(value)) { | ||
var variableValue = (variables || {})[value.name.value]; | ||
argObj[name.value] = variableValue; | ||
function isIntValue(value) { | ||
return value.kind === 'IntValue'; | ||
} | ||
else if (isListValue(value)) { | ||
argObj[name.value] = value.values.map(function (listValue) { | ||
var nestedArgArrayObj = {}; | ||
valueToObjectRepresentation(nestedArgArrayObj, name, listValue, variables); | ||
return nestedArgArrayObj[name.value]; | ||
}); | ||
function isFloatValue(value) { | ||
return value.kind === 'FloatValue'; | ||
} | ||
else if (isEnumValue(value)) { | ||
argObj[name.value] = value.value; | ||
function isVariable(value) { | ||
return value.kind === 'Variable'; | ||
} | ||
else if (isNullValue(value)) { | ||
argObj[name.value] = null; | ||
function isObjectValue(value) { | ||
return value.kind === 'ObjectValue'; | ||
} | ||
else { | ||
throw new Error("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\" is not supported.\n Use variables instead of inline arguments to overcome this limitation."); | ||
function isListValue(value) { | ||
return value.kind === 'ListValue'; | ||
} | ||
} | ||
function storeKeyNameFromField(field, variables) { | ||
var directivesObj = null; | ||
if (field.directives) { | ||
directivesObj = {}; | ||
field.directives.forEach(function (directive) { | ||
directivesObj[directive.name.value] = {}; | ||
if (directive.arguments) { | ||
directive.arguments.forEach(function (_a) { | ||
var name = _a.name, value = _a.value; | ||
return valueToObjectRepresentation(directivesObj[directive.name.value], name, value, variables); | ||
}); | ||
} | ||
}); | ||
function isEnumValue(value) { | ||
return value.kind === 'EnumValue'; | ||
} | ||
var argObj = null; | ||
if (field.arguments && field.arguments.length) { | ||
argObj = {}; | ||
field.arguments.forEach(function (_a) { | ||
var name = _a.name, value = _a.value; | ||
return valueToObjectRepresentation(argObj, name, value, variables); | ||
}); | ||
function isNullValue(value) { | ||
return value.kind === 'NullValue'; | ||
} | ||
return getStoreKeyName(field.name.value, argObj, directivesObj); | ||
} | ||
var KNOWN_DIRECTIVES = [ | ||
'connection', | ||
'include', | ||
'skip', | ||
'client', | ||
'rest', | ||
'export', | ||
]; | ||
function getStoreKeyName(fieldName, args, directives) { | ||
if (directives && | ||
directives['connection'] && | ||
directives['connection']['key']) { | ||
if (directives['connection']['filter'] && | ||
directives['connection']['filter'].length > 0) { | ||
var filterKeys = directives['connection']['filter'] | ||
? directives['connection']['filter'] | ||
: []; | ||
filterKeys.sort(); | ||
var queryArgs_1 = args; | ||
var filteredArgs_1 = {}; | ||
filterKeys.forEach(function (key) { | ||
filteredArgs_1[key] = queryArgs_1[key]; | ||
function valueToObjectRepresentation(argObj, name, value, variables) { | ||
if (isIntValue(value) || isFloatValue(value)) { | ||
argObj[name.value] = Number(value.value); | ||
} | ||
else if (isBooleanValue(value) || isStringValue(value)) { | ||
argObj[name.value] = value.value; | ||
} | ||
else if (isObjectValue(value)) { | ||
var nestedArgObj_1 = {}; | ||
value.fields.map(function (obj) { | ||
return valueToObjectRepresentation(nestedArgObj_1, obj.name, obj.value, variables); | ||
}); | ||
return directives['connection']['key'] + "(" + JSON.stringify(filteredArgs_1) + ")"; | ||
argObj[name.value] = nestedArgObj_1; | ||
} | ||
else if (isVariable(value)) { | ||
var variableValue = (variables || {})[value.name.value]; | ||
argObj[name.value] = variableValue; | ||
} | ||
else if (isListValue(value)) { | ||
argObj[name.value] = value.values.map(function (listValue) { | ||
var nestedArgArrayObj = {}; | ||
valueToObjectRepresentation(nestedArgArrayObj, name, listValue, variables); | ||
return nestedArgArrayObj[name.value]; | ||
}); | ||
} | ||
else if (isEnumValue(value)) { | ||
argObj[name.value] = value.value; | ||
} | ||
else if (isNullValue(value)) { | ||
argObj[name.value] = null; | ||
} | ||
else { | ||
return directives['connection']['key']; | ||
throw new Error("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\" is not supported.\n Use variables instead of inline arguments to overcome this limitation."); | ||
} | ||
} | ||
var completeFieldName = fieldName; | ||
if (args) { | ||
var stringifiedArgs = JSON.stringify(args); | ||
completeFieldName += "(" + stringifiedArgs + ")"; | ||
function storeKeyNameFromField(field, variables) { | ||
var directivesObj = null; | ||
if (field.directives) { | ||
directivesObj = {}; | ||
field.directives.forEach(function (directive) { | ||
directivesObj[directive.name.value] = {}; | ||
if (directive.arguments) { | ||
directive.arguments.forEach(function (_a) { | ||
var name = _a.name, value = _a.value; | ||
return valueToObjectRepresentation(directivesObj[directive.name.value], name, value, variables); | ||
}); | ||
} | ||
}); | ||
} | ||
var argObj = null; | ||
if (field.arguments && field.arguments.length) { | ||
argObj = {}; | ||
field.arguments.forEach(function (_a) { | ||
var name = _a.name, value = _a.value; | ||
return valueToObjectRepresentation(argObj, name, value, variables); | ||
}); | ||
} | ||
return getStoreKeyName(field.name.value, argObj, directivesObj); | ||
} | ||
if (directives) { | ||
Object.keys(directives).forEach(function (key) { | ||
if (KNOWN_DIRECTIVES.indexOf(key) !== -1) | ||
return; | ||
if (directives[key] && Object.keys(directives[key]).length) { | ||
completeFieldName += "@" + key + "(" + JSON.stringify(directives[key]) + ")"; | ||
var KNOWN_DIRECTIVES = [ | ||
'connection', | ||
'include', | ||
'skip', | ||
'client', | ||
'rest', | ||
'export', | ||
]; | ||
function getStoreKeyName(fieldName, args, directives) { | ||
if (directives && | ||
directives['connection'] && | ||
directives['connection']['key']) { | ||
if (directives['connection']['filter'] && | ||
directives['connection']['filter'].length > 0) { | ||
var filterKeys = directives['connection']['filter'] | ||
? directives['connection']['filter'] | ||
: []; | ||
filterKeys.sort(); | ||
var queryArgs_1 = args; | ||
var filteredArgs_1 = {}; | ||
filterKeys.forEach(function (key) { | ||
filteredArgs_1[key] = queryArgs_1[key]; | ||
}); | ||
return directives['connection']['key'] + "(" + JSON.stringify(filteredArgs_1) + ")"; | ||
} | ||
else { | ||
completeFieldName += "@" + key; | ||
return directives['connection']['key']; | ||
} | ||
}); | ||
} | ||
var completeFieldName = fieldName; | ||
if (args) { | ||
var stringifiedArgs = JSON.stringify(args); | ||
completeFieldName += "(" + stringifiedArgs + ")"; | ||
} | ||
if (directives) { | ||
Object.keys(directives).forEach(function (key) { | ||
if (KNOWN_DIRECTIVES.indexOf(key) !== -1) | ||
return; | ||
if (directives[key] && Object.keys(directives[key]).length) { | ||
completeFieldName += "@" + key + "(" + JSON.stringify(directives[key]) + ")"; | ||
} | ||
else { | ||
completeFieldName += "@" + key; | ||
} | ||
}); | ||
} | ||
return completeFieldName; | ||
} | ||
return completeFieldName; | ||
} | ||
function argumentsObjectFromField(field, variables) { | ||
if (field.arguments && field.arguments.length) { | ||
var argObj_1 = {}; | ||
field.arguments.forEach(function (_a) { | ||
var name = _a.name, value = _a.value; | ||
return valueToObjectRepresentation(argObj_1, name, value, variables); | ||
}); | ||
return argObj_1; | ||
function argumentsObjectFromField(field, variables) { | ||
if (field.arguments && field.arguments.length) { | ||
var argObj_1 = {}; | ||
field.arguments.forEach(function (_a) { | ||
var name = _a.name, value = _a.value; | ||
return valueToObjectRepresentation(argObj_1, name, value, variables); | ||
}); | ||
return argObj_1; | ||
} | ||
return null; | ||
} | ||
return null; | ||
} | ||
function resultKeyNameFromField(field) { | ||
return field.alias ? field.alias.value : field.name.value; | ||
} | ||
function isField(selection) { | ||
return selection.kind === 'Field'; | ||
} | ||
function isInlineFragment(selection) { | ||
return selection.kind === 'InlineFragment'; | ||
} | ||
function isIdValue(idObject) { | ||
return idObject && idObject.type === 'id'; | ||
} | ||
function toIdValue(idConfig, generated) { | ||
if (generated === void 0) { generated = false; } | ||
return __assign({ type: 'id', generated: generated }, (typeof idConfig === 'string' | ||
? { id: idConfig, typename: undefined } | ||
: idConfig)); | ||
} | ||
function isJsonValue(jsonObject) { | ||
return (jsonObject != null && | ||
typeof jsonObject === 'object' && | ||
jsonObject.type === 'json'); | ||
} | ||
function defaultValueFromVariable(node) { | ||
throw new Error("Variable nodes are not supported by valueFromNode"); | ||
} | ||
/** | ||
* Evaluate a ValueNode and yield its value in its natural JS form. | ||
*/ | ||
function valueFromNode(node, onVariable) { | ||
if (onVariable === void 0) { onVariable = defaultValueFromVariable; } | ||
switch (node.kind) { | ||
case 'Variable': | ||
return onVariable(node); | ||
case 'NullValue': | ||
return null; | ||
case 'IntValue': | ||
return parseInt(node.value); | ||
case 'FloatValue': | ||
return parseFloat(node.value); | ||
case 'ListValue': | ||
return node.values.map(function (v) { return valueFromNode(v, onVariable); }); | ||
case 'ObjectValue': { | ||
var value = {}; | ||
for (var _i = 0, _a = node.fields; _i < _a.length; _i++) { | ||
var field = _a[_i]; | ||
value[field.name.value] = valueFromNode(field.value, onVariable); | ||
function resultKeyNameFromField(field) { | ||
return field.alias ? field.alias.value : field.name.value; | ||
} | ||
function isField(selection) { | ||
return selection.kind === 'Field'; | ||
} | ||
function isInlineFragment(selection) { | ||
return selection.kind === 'InlineFragment'; | ||
} | ||
function isIdValue(idObject) { | ||
return idObject && idObject.type === 'id'; | ||
} | ||
function toIdValue(idConfig, generated) { | ||
if (generated === void 0) { generated = false; } | ||
return __assign({ type: 'id', generated: generated }, (typeof idConfig === 'string' | ||
? { id: idConfig, typename: undefined } | ||
: idConfig)); | ||
} | ||
function isJsonValue(jsonObject) { | ||
return (jsonObject != null && | ||
typeof jsonObject === 'object' && | ||
jsonObject.type === 'json'); | ||
} | ||
function defaultValueFromVariable(node) { | ||
throw new Error("Variable nodes are not supported by valueFromNode"); | ||
} | ||
/** | ||
* Evaluate a ValueNode and yield its value in its natural JS form. | ||
*/ | ||
function valueFromNode(node, onVariable) { | ||
if (onVariable === void 0) { onVariable = defaultValueFromVariable; } | ||
switch (node.kind) { | ||
case 'Variable': | ||
return onVariable(node); | ||
case 'NullValue': | ||
return null; | ||
case 'IntValue': | ||
return parseInt(node.value); | ||
case 'FloatValue': | ||
return parseFloat(node.value); | ||
case 'ListValue': | ||
return node.values.map(function (v) { return valueFromNode(v, onVariable); }); | ||
case 'ObjectValue': { | ||
var value = {}; | ||
for (var _i = 0, _a = node.fields; _i < _a.length; _i++) { | ||
var field = _a[_i]; | ||
value[field.name.value] = valueFromNode(field.value, onVariable); | ||
} | ||
return value; | ||
} | ||
return value; | ||
default: | ||
return node.value; | ||
} | ||
default: | ||
return node.value; | ||
} | ||
} | ||
function getDirectiveInfoFromField(field, variables) { | ||
if (field.directives && field.directives.length) { | ||
var directiveObj_1 = {}; | ||
field.directives.forEach(function (directive) { | ||
directiveObj_1[directive.name.value] = argumentsObjectFromField(directive, variables); | ||
function getDirectiveInfoFromField(field, variables) { | ||
if (field.directives && field.directives.length) { | ||
var directiveObj_1 = {}; | ||
field.directives.forEach(function (directive) { | ||
directiveObj_1[directive.name.value] = argumentsObjectFromField(directive, variables); | ||
}); | ||
return directiveObj_1; | ||
} | ||
return null; | ||
} | ||
function shouldInclude(selection, variables) { | ||
if (variables === void 0) { variables = {}; } | ||
if (!selection.directives) { | ||
return true; | ||
} | ||
var res = true; | ||
selection.directives.forEach(function (directive) { | ||
// TODO should move this validation to GraphQL validation once that's implemented. | ||
if (directive.name.value !== 'skip' && directive.name.value !== 'include') { | ||
// Just don't worry about directives we don't understand | ||
return; | ||
} | ||
//evaluate the "if" argument and skip (i.e. return undefined) if it evaluates to true. | ||
var directiveArguments = directive.arguments || []; | ||
var directiveName = directive.name.value; | ||
if (directiveArguments.length !== 1) { | ||
throw new Error("Incorrect number of arguments for the @" + directiveName + " directive."); | ||
} | ||
var ifArgument = directiveArguments[0]; | ||
if (!ifArgument.name || ifArgument.name.value !== 'if') { | ||
throw new Error("Invalid argument for the @" + directiveName + " directive."); | ||
} | ||
var ifValue = directiveArguments[0].value; | ||
var evaledValue = false; | ||
if (!ifValue || ifValue.kind !== 'BooleanValue') { | ||
// means it has to be a variable value if this is a valid @skip or @include directive | ||
if (ifValue.kind !== 'Variable') { | ||
throw new Error("Argument for the @" + directiveName + " directive must be a variable or a boolean value."); | ||
} | ||
else { | ||
evaledValue = variables[ifValue.name.value]; | ||
if (evaledValue === undefined) { | ||
throw new Error("Invalid variable referenced in @" + directiveName + " directive."); | ||
} | ||
} | ||
} | ||
else { | ||
evaledValue = ifValue.value; | ||
} | ||
if (directiveName === 'skip') { | ||
evaledValue = !evaledValue; | ||
} | ||
if (!evaledValue) { | ||
res = false; | ||
} | ||
}); | ||
return directiveObj_1; | ||
return res; | ||
} | ||
return null; | ||
} | ||
function shouldInclude(selection, variables) { | ||
if (variables === void 0) { variables = {}; } | ||
if (!selection.directives) { | ||
return true; | ||
function flattenSelections(selection) { | ||
if (!selection.selectionSet || | ||
!(selection.selectionSet.selections.length > 0)) | ||
return [selection]; | ||
return [selection].concat(selection.selectionSet.selections | ||
.map(function (selectionNode) { | ||
return [selectionNode].concat(flattenSelections(selectionNode)); | ||
}) | ||
.reduce(function (selections, selected) { return selections.concat(selected); }, [])); | ||
} | ||
var res = true; | ||
selection.directives.forEach(function (directive) { | ||
// TODO should move this validation to GraphQL validation once that's implemented. | ||
if (directive.name.value !== 'skip' && directive.name.value !== 'include') { | ||
// Just don't worry about directives we don't understand | ||
return; | ||
var added = new Map(); | ||
function getDirectiveNames(doc) { | ||
var cached = added.get(doc); | ||
if (cached) | ||
return cached; | ||
// operation => [names of directives]; | ||
var directives = doc.definitions | ||
.filter(function (definition) { | ||
return definition.selectionSet && definition.selectionSet.selections; | ||
}) | ||
.map(function (x) { return flattenSelections(x); }) | ||
.reduce(function (selections, selected) { return selections.concat(selected); }, []) | ||
.filter(function (selection) { | ||
return selection.directives && selection.directives.length > 0; | ||
}) | ||
.map(function (selection) { return selection.directives; }) | ||
.reduce(function (directives, directive) { return directives.concat(directive); }, []) | ||
.map(function (directive) { return directive.name.value; }); | ||
added.set(doc, directives); | ||
return directives; | ||
} | ||
function hasDirectives(names, doc) { | ||
return getDirectiveNames(doc).some(function (name) { return names.indexOf(name) > -1; }); | ||
} | ||
var __assign$1 = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
//evaluate the "if" argument and skip (i.e. return undefined) if it evaluates to true. | ||
var directiveArguments = directive.arguments || []; | ||
var directiveName = directive.name.value; | ||
if (directiveArguments.length !== 1) { | ||
throw new Error("Incorrect number of arguments for the @" + directiveName + " directive."); | ||
} | ||
var ifArgument = directiveArguments[0]; | ||
if (!ifArgument.name || ifArgument.name.value !== 'if') { | ||
throw new Error("Invalid argument for the @" + directiveName + " directive."); | ||
} | ||
var ifValue = directiveArguments[0].value; | ||
var evaledValue = false; | ||
if (!ifValue || ifValue.kind !== 'BooleanValue') { | ||
// means it has to be a variable value if this is a valid @skip or @include directive | ||
if (ifValue.kind !== 'Variable') { | ||
throw new Error("Argument for the @" + directiveName + " directive must be a variable or a boolean value."); | ||
return t; | ||
}; | ||
/** | ||
* Returns a query document which adds a single query operation that only | ||
* spreads the target fragment inside of it. | ||
* | ||
* So for example a document of: | ||
* | ||
* ```graphql | ||
* fragment foo on Foo { a b c } | ||
* ``` | ||
* | ||
* Turns into: | ||
* | ||
* ```graphql | ||
* { ...foo } | ||
* | ||
* fragment foo on Foo { a b c } | ||
* ``` | ||
* | ||
* The target fragment will either be the only fragment in the document, or a | ||
* fragment specified by the provided `fragmentName`. If there is more then one | ||
* fragment, but a `fragmentName` was not defined then an error will be thrown. | ||
*/ | ||
function getFragmentQueryDocument(document, fragmentName) { | ||
var actualFragmentName = fragmentName; | ||
// Build an array of all our fragment definitions that will be used for | ||
// validations. We also do some validations on the other definitions in the | ||
// document while building this list. | ||
var fragments = []; | ||
document.definitions.forEach(function (definition) { | ||
// Throw an error if we encounter an operation definition because we will | ||
// define our own operation definition later on. | ||
if (definition.kind === 'OperationDefinition') { | ||
throw new Error("Found a " + definition.operation + " operation" + (definition.name ? " named '" + definition.name.value + "'" : '') + ". " + | ||
'No operations are allowed when using a fragment as a query. Only fragments are allowed.'); | ||
} | ||
else { | ||
evaledValue = variables[ifValue.name.value]; | ||
if (evaledValue === undefined) { | ||
throw new Error("Invalid variable referenced in @" + directiveName + " directive."); | ||
} | ||
// Add our definition to the fragments array if it is a fragment | ||
// definition. | ||
if (definition.kind === 'FragmentDefinition') { | ||
fragments.push(definition); | ||
} | ||
}); | ||
// If the user did not give us a fragment name then let us try to get a | ||
// name from a single fragment in the definition. | ||
if (typeof actualFragmentName === 'undefined') { | ||
if (fragments.length !== 1) { | ||
throw new Error("Found " + fragments.length + " fragments. `fragmentName` must be provided when there is not exactly 1 fragment."); | ||
} | ||
actualFragmentName = fragments[0].name.value; | ||
} | ||
else { | ||
evaledValue = ifValue.value; | ||
} | ||
if (directiveName === 'skip') { | ||
evaledValue = !evaledValue; | ||
} | ||
if (!evaledValue) { | ||
res = false; | ||
} | ||
}); | ||
return res; | ||
} | ||
function flattenSelections(selection) { | ||
if (!selection.selectionSet || | ||
!(selection.selectionSet.selections.length > 0)) | ||
return [selection]; | ||
return [selection].concat(selection.selectionSet.selections | ||
.map(function (selectionNode) { | ||
return [selectionNode].concat(flattenSelections(selectionNode)); | ||
}) | ||
.reduce(function (selections, selected) { return selections.concat(selected); }, [])); | ||
} | ||
var added = new Map(); | ||
function getDirectiveNames(doc) { | ||
var cached = added.get(doc); | ||
if (cached) | ||
return cached; | ||
// operation => [names of directives]; | ||
var directives = doc.definitions | ||
.filter(function (definition) { | ||
return definition.selectionSet && definition.selectionSet.selections; | ||
}) | ||
.map(function (x) { return flattenSelections(x); }) | ||
.reduce(function (selections, selected) { return selections.concat(selected); }, []) | ||
.filter(function (selection) { | ||
return selection.directives && selection.directives.length > 0; | ||
}) | ||
.map(function (selection) { return selection.directives; }) | ||
.reduce(function (directives, directive) { return directives.concat(directive); }, []) | ||
.map(function (directive) { return directive.name.value; }); | ||
added.set(doc, directives); | ||
return directives; | ||
} | ||
function hasDirectives(names, doc) { | ||
return getDirectiveNames(doc).some(function (name) { return names.indexOf(name) > -1; }); | ||
} | ||
// Generate a query document with an operation that simply spreads the | ||
// fragment inside of it. | ||
var query = __assign$1({}, document, { definitions: [ | ||
{ | ||
kind: 'OperationDefinition', | ||
operation: 'query', | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ | ||
kind: 'FragmentSpread', | ||
name: { | ||
kind: 'Name', | ||
value: actualFragmentName, | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
].concat(document.definitions) }); | ||
return query; | ||
} | ||
var __assign$1 = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
/** | ||
* Returns a query document which adds a single query operation that only | ||
* spreads the target fragment inside of it. | ||
* | ||
* So for example a document of: | ||
* | ||
* ```graphql | ||
* fragment foo on Foo { a b c } | ||
* ``` | ||
* | ||
* Turns into: | ||
* | ||
* ```graphql | ||
* { ...foo } | ||
* | ||
* fragment foo on Foo { a b c } | ||
* ``` | ||
* | ||
* The target fragment will either be the only fragment in the document, or a | ||
* fragment specified by the provided `fragmentName`. If there is more then one | ||
* fragment, but a `fragmentName` was not defined then an error will be thrown. | ||
*/ | ||
function getFragmentQueryDocument(document, fragmentName) { | ||
var actualFragmentName = fragmentName; | ||
// Build an array of all our fragment definitions that will be used for | ||
// validations. We also do some validations on the other definitions in the | ||
// document while building this list. | ||
var fragments = []; | ||
document.definitions.forEach(function (definition) { | ||
// Throw an error if we encounter an operation definition because we will | ||
// define our own operation definition later on. | ||
if (definition.kind === 'OperationDefinition') { | ||
throw new Error("Found a " + definition.operation + " operation" + (definition.name ? " named '" + definition.name.value + "'" : '') + ". " + | ||
'No operations are allowed when using a fragment as a query. Only fragments are allowed.'); | ||
function assign(target) { | ||
var sources = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
sources[_i - 1] = arguments[_i]; | ||
} | ||
// Add our definition to the fragments array if it is a fragment | ||
// definition. | ||
if (definition.kind === 'FragmentDefinition') { | ||
fragments.push(definition); | ||
} | ||
}); | ||
// If the user did not give us a fragment name then let us try to get a | ||
// name from a single fragment in the definition. | ||
if (typeof actualFragmentName === 'undefined') { | ||
if (fragments.length !== 1) { | ||
throw new Error("Found " + fragments.length + " fragments. `fragmentName` must be provided when there is not exactly 1 fragment."); | ||
} | ||
actualFragmentName = fragments[0].name.value; | ||
sources.forEach(function (source) { | ||
if (typeof source === 'undefined' || source === null) { | ||
return; | ||
} | ||
Object.keys(source).forEach(function (key) { | ||
target[key] = source[key]; | ||
}); | ||
}); | ||
return target; | ||
} | ||
// Generate a query document with an operation that simply spreads the | ||
// fragment inside of it. | ||
var query = __assign$1({}, document, { definitions: [ | ||
{ | ||
kind: 'OperationDefinition', | ||
operation: 'query', | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ | ||
kind: 'FragmentSpread', | ||
name: { | ||
kind: 'Name', | ||
value: actualFragmentName, | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
].concat(document.definitions) }); | ||
return query; | ||
} | ||
function assign(target) { | ||
var sources = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
sources[_i - 1] = arguments[_i]; | ||
function getMutationDefinition(doc) { | ||
checkDocument(doc); | ||
var mutationDef = doc.definitions.filter(function (definition) { | ||
return definition.kind === 'OperationDefinition' && | ||
definition.operation === 'mutation'; | ||
})[0]; | ||
if (!mutationDef) { | ||
throw new Error('Must contain a mutation definition.'); | ||
} | ||
return mutationDef; | ||
} | ||
sources.forEach(function (source) { | ||
if (typeof source === 'undefined' || source === null) { | ||
return; | ||
// Checks the document for errors and throws an exception if there is an error. | ||
function checkDocument(doc) { | ||
if (doc.kind !== 'Document') { | ||
throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql"); | ||
} | ||
Object.keys(source).forEach(function (key) { | ||
target[key] = source[key]; | ||
var operations = doc.definitions | ||
.filter(function (d) { return d.kind !== 'FragmentDefinition'; }) | ||
.map(function (definition) { | ||
if (definition.kind !== 'OperationDefinition') { | ||
throw new Error("Schema type definitions not allowed in queries. Found: \"" + definition.kind + "\""); | ||
} | ||
return definition; | ||
}); | ||
}); | ||
return target; | ||
} | ||
function getMutationDefinition(doc) { | ||
checkDocument(doc); | ||
var mutationDef = doc.definitions.filter(function (definition) { | ||
return definition.kind === 'OperationDefinition' && | ||
definition.operation === 'mutation'; | ||
})[0]; | ||
if (!mutationDef) { | ||
throw new Error('Must contain a mutation definition.'); | ||
if (operations.length > 1) { | ||
throw new Error("Ambiguous GraphQL document: contains " + operations.length + " operations"); | ||
} | ||
} | ||
return mutationDef; | ||
} | ||
// Checks the document for errors and throws an exception if there is an error. | ||
function checkDocument(doc) { | ||
if (doc.kind !== 'Document') { | ||
throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql"); | ||
function getOperationDefinition(doc) { | ||
checkDocument(doc); | ||
return doc.definitions.filter(function (definition) { return definition.kind === 'OperationDefinition'; })[0]; | ||
} | ||
var operations = doc.definitions | ||
.filter(function (d) { return d.kind !== 'FragmentDefinition'; }) | ||
.map(function (definition) { | ||
if (definition.kind !== 'OperationDefinition') { | ||
throw new Error("Schema type definitions not allowed in queries. Found: \"" + definition.kind + "\""); | ||
function getOperationDefinitionOrDie(document) { | ||
var def = getOperationDefinition(document); | ||
if (!def) { | ||
throw new Error("GraphQL document is missing an operation"); | ||
} | ||
return definition; | ||
}); | ||
if (operations.length > 1) { | ||
throw new Error("Ambiguous GraphQL document: contains " + operations.length + " operations"); | ||
return def; | ||
} | ||
} | ||
function getOperationDefinition(doc) { | ||
checkDocument(doc); | ||
return doc.definitions.filter(function (definition) { return definition.kind === 'OperationDefinition'; })[0]; | ||
} | ||
function getOperationDefinitionOrDie(document) { | ||
var def = getOperationDefinition(document); | ||
if (!def) { | ||
throw new Error("GraphQL document is missing an operation"); | ||
function getOperationName(doc) { | ||
return (doc.definitions | ||
.filter(function (definition) { | ||
return definition.kind === 'OperationDefinition' && definition.name; | ||
}) | ||
.map(function (x) { return x.name.value; })[0] || null); | ||
} | ||
return def; | ||
} | ||
function getOperationName(doc) { | ||
return (doc.definitions | ||
.filter(function (definition) { | ||
return definition.kind === 'OperationDefinition' && definition.name; | ||
}) | ||
.map(function (x) { return x.name.value; })[0] || null); | ||
} | ||
// Returns the FragmentDefinitions from a particular document as an array | ||
function getFragmentDefinitions(doc) { | ||
return doc.definitions.filter(function (definition) { return definition.kind === 'FragmentDefinition'; }); | ||
} | ||
function getQueryDefinition(doc) { | ||
var queryDef = getOperationDefinition(doc); | ||
if (!queryDef || queryDef.operation !== 'query') { | ||
throw new Error('Must contain a query definition.'); | ||
// Returns the FragmentDefinitions from a particular document as an array | ||
function getFragmentDefinitions(doc) { | ||
return doc.definitions.filter(function (definition) { return definition.kind === 'FragmentDefinition'; }); | ||
} | ||
return queryDef; | ||
} | ||
function getFragmentDefinition(doc) { | ||
if (doc.kind !== 'Document') { | ||
throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql"); | ||
function getQueryDefinition(doc) { | ||
var queryDef = getOperationDefinition(doc); | ||
if (!queryDef || queryDef.operation !== 'query') { | ||
throw new Error('Must contain a query definition.'); | ||
} | ||
return queryDef; | ||
} | ||
if (doc.definitions.length > 1) { | ||
throw new Error('Fragment must have exactly one definition.'); | ||
function getFragmentDefinition(doc) { | ||
if (doc.kind !== 'Document') { | ||
throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql"); | ||
} | ||
if (doc.definitions.length > 1) { | ||
throw new Error('Fragment must have exactly one definition.'); | ||
} | ||
var fragmentDef = doc.definitions[0]; | ||
if (fragmentDef.kind !== 'FragmentDefinition') { | ||
throw new Error('Must be a fragment definition.'); | ||
} | ||
return fragmentDef; | ||
} | ||
var fragmentDef = doc.definitions[0]; | ||
if (fragmentDef.kind !== 'FragmentDefinition') { | ||
throw new Error('Must be a fragment definition.'); | ||
} | ||
return fragmentDef; | ||
} | ||
/** | ||
* Returns the first operation definition found in this document. | ||
* If no operation definition is found, the first fragment definition will be returned. | ||
* If no definitions are found, an error will be thrown. | ||
*/ | ||
function getMainDefinition(queryDoc) { | ||
checkDocument(queryDoc); | ||
var fragmentDefinition; | ||
for (var _i = 0, _a = queryDoc.definitions; _i < _a.length; _i++) { | ||
var definition = _a[_i]; | ||
if (definition.kind === 'OperationDefinition') { | ||
var operation = definition.operation; | ||
if (operation === 'query' || | ||
operation === 'mutation' || | ||
operation === 'subscription') { | ||
return definition; | ||
/** | ||
* Returns the first operation definition found in this document. | ||
* If no operation definition is found, the first fragment definition will be returned. | ||
* If no definitions are found, an error will be thrown. | ||
*/ | ||
function getMainDefinition(queryDoc) { | ||
checkDocument(queryDoc); | ||
var fragmentDefinition; | ||
for (var _i = 0, _a = queryDoc.definitions; _i < _a.length; _i++) { | ||
var definition = _a[_i]; | ||
if (definition.kind === 'OperationDefinition') { | ||
var operation = definition.operation; | ||
if (operation === 'query' || | ||
operation === 'mutation' || | ||
operation === 'subscription') { | ||
return definition; | ||
} | ||
} | ||
if (definition.kind === 'FragmentDefinition' && !fragmentDefinition) { | ||
// we do this because we want to allow multiple fragment definitions | ||
// to precede an operation definition. | ||
fragmentDefinition = definition; | ||
} | ||
} | ||
if (definition.kind === 'FragmentDefinition' && !fragmentDefinition) { | ||
// we do this because we want to allow multiple fragment definitions | ||
// to precede an operation definition. | ||
fragmentDefinition = definition; | ||
if (fragmentDefinition) { | ||
return fragmentDefinition; | ||
} | ||
throw new Error('Expected a parsed GraphQL query with a query, mutation, subscription, or a fragment.'); | ||
} | ||
if (fragmentDefinition) { | ||
return fragmentDefinition; | ||
} | ||
throw new Error('Expected a parsed GraphQL query with a query, mutation, subscription, or a fragment.'); | ||
} | ||
// Utility function that takes a list of fragment definitions and makes a hash out of them | ||
// that maps the name of the fragment to the fragment definition. | ||
function createFragmentMap(fragments) { | ||
if (fragments === void 0) { fragments = []; } | ||
var symTable = {}; | ||
fragments.forEach(function (fragment) { | ||
symTable[fragment.name.value] = fragment; | ||
}); | ||
return symTable; | ||
} | ||
function getDefaultValues(definition) { | ||
if (definition && | ||
definition.variableDefinitions && | ||
definition.variableDefinitions.length) { | ||
var defaultValues = definition.variableDefinitions | ||
.filter(function (_a) { | ||
var defaultValue = _a.defaultValue; | ||
return defaultValue; | ||
}) | ||
.map(function (_a) { | ||
var variable = _a.variable, defaultValue = _a.defaultValue; | ||
var defaultValueObj = {}; | ||
valueToObjectRepresentation(defaultValueObj, variable.name, defaultValue); | ||
return defaultValueObj; | ||
// Utility function that takes a list of fragment definitions and makes a hash out of them | ||
// that maps the name of the fragment to the fragment definition. | ||
function createFragmentMap(fragments) { | ||
if (fragments === void 0) { fragments = []; } | ||
var symTable = {}; | ||
fragments.forEach(function (fragment) { | ||
symTable[fragment.name.value] = fragment; | ||
}); | ||
return assign.apply(void 0, [{}].concat(defaultValues)); | ||
return symTable; | ||
} | ||
return {}; | ||
} | ||
/** | ||
* Returns the names of all variables declared by the operation. | ||
*/ | ||
function variablesInOperation(operation) { | ||
var names = new Set(); | ||
if (operation.variableDefinitions) { | ||
for (var _i = 0, _a = operation.variableDefinitions; _i < _a.length; _i++) { | ||
var definition = _a[_i]; | ||
names.add(definition.variable.name.value); | ||
function getDefaultValues(definition) { | ||
if (definition && | ||
definition.variableDefinitions && | ||
definition.variableDefinitions.length) { | ||
var defaultValues = definition.variableDefinitions | ||
.filter(function (_a) { | ||
var defaultValue = _a.defaultValue; | ||
return defaultValue; | ||
}) | ||
.map(function (_a) { | ||
var variable = _a.variable, defaultValue = _a.defaultValue; | ||
var defaultValueObj = {}; | ||
valueToObjectRepresentation(defaultValueObj, variable.name, defaultValue); | ||
return defaultValueObj; | ||
}); | ||
return assign.apply(void 0, [{}].concat(defaultValues)); | ||
} | ||
return {}; | ||
} | ||
return names; | ||
} | ||
/** | ||
* Returns the names of all variables declared by the operation. | ||
*/ | ||
function variablesInOperation(operation) { | ||
var names = new Set(); | ||
if (operation.variableDefinitions) { | ||
for (var _i = 0, _a = operation.variableDefinitions; _i < _a.length; _i++) { | ||
var definition = _a[_i]; | ||
names.add(definition.variable.name.value); | ||
} | ||
} | ||
return names; | ||
} | ||
/** | ||
* Deeply clones a value to create a new instance. | ||
*/ | ||
function cloneDeep(value) { | ||
// If the value is an array, create a new array where every item has been cloned. | ||
if (Array.isArray(value)) { | ||
return value.map(function (item) { return cloneDeep(item); }); | ||
} | ||
// If the value is an object, go through all of the object’s properties and add them to a new | ||
// object. | ||
if (value !== null && typeof value === 'object') { | ||
var nextValue = {}; | ||
for (var key in value) { | ||
if (value.hasOwnProperty(key)) { | ||
nextValue[key] = cloneDeep(value[key]); | ||
/** | ||
* Deeply clones a value to create a new instance. | ||
*/ | ||
function cloneDeep(value) { | ||
// If the value is an array, create a new array where every item has been cloned. | ||
if (Array.isArray(value)) { | ||
return value.map(function (item) { return cloneDeep(item); }); | ||
} | ||
// If the value is an object, go through all of the object’s properties and add them to a new | ||
// object. | ||
if (value !== null && typeof value === 'object') { | ||
var nextValue = {}; | ||
for (var key in value) { | ||
if (value.hasOwnProperty(key)) { | ||
nextValue[key] = cloneDeep(value[key]); | ||
} | ||
} | ||
return nextValue; | ||
} | ||
return nextValue; | ||
// Otherwise this is some primitive value and it is therefore immutable so we can just return it | ||
// directly. | ||
return value; | ||
} | ||
// Otherwise this is some primitive value and it is therefore immutable so we can just return it | ||
// directly. | ||
return value; | ||
} | ||
var TYPENAME_FIELD = { | ||
kind: 'Field', | ||
name: { | ||
kind: 'Name', | ||
value: '__typename', | ||
}, | ||
}; | ||
function isNotEmpty(op, fragments) { | ||
// keep selections that are still valid | ||
return (op.selectionSet.selections.filter(function (selectionSet) { | ||
// anything that doesn't match the compound filter is okay | ||
return !(selectionSet && | ||
// look into fragments to verify they should stay | ||
selectionSet.kind === 'FragmentSpread' && | ||
// see if the fragment in the map is valid (recursively) | ||
!isNotEmpty(fragments[selectionSet.name.value], fragments)); | ||
}).length > 0); | ||
} | ||
function getDirectiveMatcher(directives) { | ||
return function directiveMatcher(directive) { | ||
return directives.some(function (dir) { | ||
if (dir.name && dir.name === directive.name.value) | ||
return true; | ||
if (dir.test && dir.test(directive)) | ||
return true; | ||
return false; | ||
}); | ||
var TYPENAME_FIELD = { | ||
kind: 'Field', | ||
name: { | ||
kind: 'Name', | ||
value: '__typename', | ||
}, | ||
}; | ||
} | ||
function addTypenameToSelectionSet(selectionSet, isRoot) { | ||
if (isRoot === void 0) { isRoot = false; } | ||
if (selectionSet.selections) { | ||
if (!isRoot) { | ||
var alreadyHasThisField = selectionSet.selections.some(function (selection) { | ||
return (selection.kind === 'Field' && | ||
selection.name.value === '__typename'); | ||
function isNotEmpty(op, fragments) { | ||
// keep selections that are still valid | ||
return (op.selectionSet.selections.filter(function (selectionSet) { | ||
// anything that doesn't match the compound filter is okay | ||
return !(selectionSet && | ||
// look into fragments to verify they should stay | ||
selectionSet.kind === 'FragmentSpread' && | ||
// see if the fragment in the map is valid (recursively) | ||
!isNotEmpty(fragments[selectionSet.name.value], fragments)); | ||
}).length > 0); | ||
} | ||
function getDirectiveMatcher(directives) { | ||
return function directiveMatcher(directive) { | ||
return directives.some(function (dir) { | ||
if (dir.name && dir.name === directive.name.value) | ||
return true; | ||
if (dir.test && dir.test(directive)) | ||
return true; | ||
return false; | ||
}); | ||
if (!alreadyHasThisField) { | ||
selectionSet.selections.push(TYPENAME_FIELD); | ||
}; | ||
} | ||
function addTypenameToSelectionSet(selectionSet, isRoot) { | ||
if (isRoot === void 0) { isRoot = false; } | ||
if (selectionSet.selections) { | ||
if (!isRoot) { | ||
var alreadyHasThisField = selectionSet.selections.some(function (selection) { | ||
return (selection.kind === 'Field' && | ||
selection.name.value === '__typename'); | ||
}); | ||
if (!alreadyHasThisField) { | ||
selectionSet.selections.push(TYPENAME_FIELD); | ||
} | ||
} | ||
selectionSet.selections.forEach(function (selection) { | ||
// Must not add __typename if we're inside an introspection query | ||
if (selection.kind === 'Field') { | ||
if (selection.name.value.lastIndexOf('__', 0) !== 0 && | ||
selection.selectionSet) { | ||
addTypenameToSelectionSet(selection.selectionSet); | ||
} | ||
} | ||
else if (selection.kind === 'InlineFragment') { | ||
if (selection.selectionSet) { | ||
addTypenameToSelectionSet(selection.selectionSet); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
function removeDirectivesFromSelectionSet(directives, selectionSet) { | ||
if (!selectionSet.selections) | ||
return selectionSet; | ||
// if any of the directives are set to remove this selectionSet, remove it | ||
var agressiveRemove = directives.some(function (dir) { return dir.remove; }); | ||
selectionSet.selections = selectionSet.selections | ||
.map(function (selection) { | ||
if (selection.kind !== 'Field' || | ||
!selection || | ||
!selection.directives) | ||
return selection; | ||
var directiveMatcher = getDirectiveMatcher(directives); | ||
var remove; | ||
selection.directives = selection.directives.filter(function (directive) { | ||
var shouldKeep = !directiveMatcher(directive); | ||
if (!remove && !shouldKeep && agressiveRemove) | ||
remove = true; | ||
return shouldKeep; | ||
}); | ||
return remove ? null : selection; | ||
}) | ||
.filter(function (x) { return !!x; }); | ||
selectionSet.selections.forEach(function (selection) { | ||
// Must not add __typename if we're inside an introspection query | ||
if (selection.kind === 'Field') { | ||
if (selection.name.value.lastIndexOf('__', 0) !== 0 && | ||
selection.selectionSet) { | ||
addTypenameToSelectionSet(selection.selectionSet); | ||
} | ||
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') && | ||
selection.selectionSet) { | ||
removeDirectivesFromSelectionSet(directives, selection.selectionSet); | ||
} | ||
else if (selection.kind === 'InlineFragment') { | ||
if (selection.selectionSet) { | ||
addTypenameToSelectionSet(selection.selectionSet); | ||
}); | ||
return selectionSet; | ||
} | ||
function removeDirectivesFromDocument(directives, doc) { | ||
var docClone = cloneDeep(doc); | ||
docClone.definitions.forEach(function (definition) { | ||
removeDirectivesFromSelectionSet(directives, definition.selectionSet); | ||
}); | ||
var operation = getOperationDefinitionOrDie(docClone); | ||
var fragments = createFragmentMap(getFragmentDefinitions(docClone)); | ||
return isNotEmpty(operation, fragments) ? docClone : null; | ||
} | ||
var added$1 = new Map(); | ||
function addTypenameToDocument(doc) { | ||
checkDocument(doc); | ||
var cached = added$1.get(doc); | ||
if (cached) | ||
return cached; | ||
var docClone = cloneDeep(doc); | ||
docClone.definitions.forEach(function (definition) { | ||
var isRoot = definition.kind === 'OperationDefinition'; | ||
addTypenameToSelectionSet(definition.selectionSet, isRoot); | ||
}); | ||
added$1.set(doc, docClone); | ||
return docClone; | ||
} | ||
var connectionRemoveConfig = { | ||
test: function (directive) { | ||
var willRemove = directive.name.value === 'connection'; | ||
if (willRemove) { | ||
if (!directive.arguments || | ||
!directive.arguments.some(function (arg) { return arg.name.value === 'key'; })) { | ||
console.warn('Removing an @connection directive even though it does not have a key. ' + | ||
'You may want to use the key parameter to specify a store key.'); | ||
} | ||
} | ||
return willRemove; | ||
}, | ||
}; | ||
var removed = new Map(); | ||
function removeConnectionDirectiveFromDocument(doc) { | ||
checkDocument(doc); | ||
var cached = removed.get(doc); | ||
if (cached) | ||
return cached; | ||
var docClone = removeDirectivesFromDocument([connectionRemoveConfig], doc); | ||
removed.set(doc, docClone); | ||
return docClone; | ||
} | ||
function hasDirectivesInSelectionSet(directives, selectionSet, nestedCheck) { | ||
if (nestedCheck === void 0) { nestedCheck = true; } | ||
if (!(selectionSet && selectionSet.selections)) { | ||
return false; | ||
} | ||
var matchedSelections = selectionSet.selections.filter(function (selection) { | ||
return hasDirectivesInSelection(directives, selection, nestedCheck); | ||
}); | ||
return matchedSelections.length > 0; | ||
} | ||
} | ||
function removeDirectivesFromSelectionSet(directives, selectionSet) { | ||
if (!selectionSet.selections) | ||
return selectionSet; | ||
// if any of the directives are set to remove this selectionSet, remove it | ||
var agressiveRemove = directives.some(function (dir) { return dir.remove; }); | ||
selectionSet.selections = selectionSet.selections | ||
.map(function (selection) { | ||
if (selection.kind !== 'Field' || | ||
!selection || | ||
!selection.directives) | ||
function hasDirectivesInSelection(directives, selection, nestedCheck) { | ||
if (nestedCheck === void 0) { nestedCheck = true; } | ||
if (selection.kind !== 'Field' || !selection) { | ||
return true; | ||
} | ||
if (!selection.directives) { | ||
return false; | ||
} | ||
var directiveMatcher = getDirectiveMatcher(directives); | ||
var matchedDirectives = selection.directives.filter(directiveMatcher); | ||
return (matchedDirectives.length > 0 || | ||
(nestedCheck && | ||
hasDirectivesInSelectionSet(directives, selection.selectionSet, nestedCheck))); | ||
} | ||
function getDirectivesFromSelectionSet(directives, selectionSet) { | ||
selectionSet.selections = selectionSet.selections | ||
.filter(function (selection) { | ||
return hasDirectivesInSelection(directives, selection, true); | ||
}) | ||
.map(function (selection) { | ||
if (hasDirectivesInSelection(directives, selection, false)) { | ||
return selection; | ||
} | ||
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') && | ||
selection.selectionSet) { | ||
selection.selectionSet = getDirectivesFromSelectionSet(directives, selection.selectionSet); | ||
} | ||
return selection; | ||
var directiveMatcher = getDirectiveMatcher(directives); | ||
var remove; | ||
selection.directives = selection.directives.filter(function (directive) { | ||
var shouldKeep = !directiveMatcher(directive); | ||
if (!remove && !shouldKeep && agressiveRemove) | ||
remove = true; | ||
return shouldKeep; | ||
}); | ||
return remove ? null : selection; | ||
}) | ||
.filter(function (x) { return !!x; }); | ||
selectionSet.selections.forEach(function (selection) { | ||
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') && | ||
selection.selectionSet) { | ||
removeDirectivesFromSelectionSet(directives, selection.selectionSet); | ||
} | ||
}); | ||
return selectionSet; | ||
} | ||
function removeDirectivesFromDocument(directives, doc) { | ||
var docClone = cloneDeep(doc); | ||
docClone.definitions.forEach(function (definition) { | ||
removeDirectivesFromSelectionSet(directives, definition.selectionSet); | ||
}); | ||
var operation = getOperationDefinitionOrDie(docClone); | ||
var fragments = createFragmentMap(getFragmentDefinitions(docClone)); | ||
return isNotEmpty(operation, fragments) ? docClone : null; | ||
} | ||
var added$1 = new Map(); | ||
function addTypenameToDocument(doc) { | ||
checkDocument(doc); | ||
var cached = added$1.get(doc); | ||
if (cached) | ||
return cached; | ||
var docClone = cloneDeep(doc); | ||
docClone.definitions.forEach(function (definition) { | ||
var isRoot = definition.kind === 'OperationDefinition'; | ||
addTypenameToSelectionSet(definition.selectionSet, isRoot); | ||
}); | ||
added$1.set(doc, docClone); | ||
return docClone; | ||
} | ||
var connectionRemoveConfig = { | ||
test: function (directive) { | ||
var willRemove = directive.name.value === 'connection'; | ||
if (willRemove) { | ||
if (!directive.arguments || | ||
!directive.arguments.some(function (arg) { return arg.name.value === 'key'; })) { | ||
console.warn('Removing an @connection directive even though it does not have a key. ' + | ||
'You may want to use the key parameter to specify a store key.'); | ||
return selectionSet; | ||
} | ||
function getDirectivesFromDocument(directives, doc, includeAllFragments) { | ||
if (includeAllFragments === void 0) { includeAllFragments = false; } | ||
checkDocument(doc); | ||
var docClone = cloneDeep(doc); | ||
docClone.definitions = docClone.definitions.map(function (definition) { | ||
if ((definition.kind === 'OperationDefinition' || | ||
(definition.kind === 'FragmentDefinition' && !includeAllFragments)) && | ||
definition.selectionSet) { | ||
definition.selectionSet = getDirectivesFromSelectionSet(directives, definition.selectionSet); | ||
} | ||
return definition; | ||
}); | ||
var operation = getOperationDefinitionOrDie(docClone); | ||
var fragments = createFragmentMap(getFragmentDefinitions(docClone)); | ||
return isNotEmpty(operation, fragments) ? docClone : null; | ||
} | ||
function getEnv() { | ||
if (typeof process !== 'undefined' && process.env.NODE_ENV) { | ||
return process.env.NODE_ENV; | ||
} | ||
return willRemove; | ||
}, | ||
}; | ||
var removed = new Map(); | ||
function removeConnectionDirectiveFromDocument(doc) { | ||
checkDocument(doc); | ||
var cached = removed.get(doc); | ||
if (cached) | ||
return cached; | ||
var docClone = removeDirectivesFromDocument([connectionRemoveConfig], doc); | ||
removed.set(doc, docClone); | ||
return docClone; | ||
} | ||
function hasDirectivesInSelectionSet(directives, selectionSet, nestedCheck) { | ||
if (nestedCheck === void 0) { nestedCheck = true; } | ||
if (!(selectionSet && selectionSet.selections)) { | ||
return false; | ||
// default environment | ||
return 'development'; | ||
} | ||
var matchedSelections = selectionSet.selections.filter(function (selection) { | ||
return hasDirectivesInSelection(directives, selection, nestedCheck); | ||
}); | ||
return matchedSelections.length > 0; | ||
} | ||
function hasDirectivesInSelection(directives, selection, nestedCheck) { | ||
if (nestedCheck === void 0) { nestedCheck = true; } | ||
if (selection.kind !== 'Field' || !selection) { | ||
return true; | ||
function isEnv(env) { | ||
return getEnv() === env; | ||
} | ||
if (!selection.directives) { | ||
return false; | ||
function isProduction() { | ||
return isEnv('production') === true; | ||
} | ||
var directiveMatcher = getDirectiveMatcher(directives); | ||
var matchedDirectives = selection.directives.filter(directiveMatcher); | ||
return (matchedDirectives.length > 0 || | ||
(nestedCheck && | ||
hasDirectivesInSelectionSet(directives, selection.selectionSet, nestedCheck))); | ||
} | ||
function getDirectivesFromSelectionSet(directives, selectionSet) { | ||
selectionSet.selections = selectionSet.selections | ||
.filter(function (selection) { | ||
return hasDirectivesInSelection(directives, selection, true); | ||
}) | ||
.map(function (selection) { | ||
if (hasDirectivesInSelection(directives, selection, false)) { | ||
return selection; | ||
function isDevelopment() { | ||
return isEnv('development') === true; | ||
} | ||
function isTest() { | ||
return isEnv('test') === true; | ||
} | ||
function tryFunctionOrLogError(f) { | ||
try { | ||
return f(); | ||
} | ||
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') && | ||
selection.selectionSet) { | ||
selection.selectionSet = getDirectivesFromSelectionSet(directives, selection.selectionSet); | ||
catch (e) { | ||
if (console.error) { | ||
console.error(e); | ||
} | ||
} | ||
return selection; | ||
}); | ||
return selectionSet; | ||
} | ||
function getDirectivesFromDocument(directives, doc, includeAllFragments) { | ||
if (includeAllFragments === void 0) { includeAllFragments = false; } | ||
checkDocument(doc); | ||
var docClone = cloneDeep(doc); | ||
docClone.definitions = docClone.definitions.map(function (definition) { | ||
if ((definition.kind === 'OperationDefinition' || | ||
(definition.kind === 'FragmentDefinition' && !includeAllFragments)) && | ||
definition.selectionSet) { | ||
definition.selectionSet = getDirectivesFromSelectionSet(directives, definition.selectionSet); | ||
} | ||
return definition; | ||
}); | ||
var operation = getOperationDefinitionOrDie(docClone); | ||
var fragments = createFragmentMap(getFragmentDefinitions(docClone)); | ||
return isNotEmpty(operation, fragments) ? docClone : null; | ||
} | ||
function getEnv() { | ||
if (typeof process !== 'undefined' && process.env.NODE_ENV) { | ||
return process.env.NODE_ENV; | ||
} | ||
// default environment | ||
return 'development'; | ||
} | ||
function isEnv(env) { | ||
return getEnv() === env; | ||
} | ||
function isProduction() { | ||
return isEnv('production') === true; | ||
} | ||
function isDevelopment() { | ||
return isEnv('development') === true; | ||
} | ||
function isTest() { | ||
return isEnv('test') === true; | ||
} | ||
function graphQLResultHasError(result) { | ||
return result.errors && result.errors.length; | ||
} | ||
function tryFunctionOrLogError(f) { | ||
try { | ||
return f(); | ||
} | ||
catch (e) { | ||
if (console.error) { | ||
console.error(e); | ||
/** | ||
* Performs a deep equality check on two JavaScript values. | ||
*/ | ||
function isEqual(a, b) { | ||
// If the two values are strictly equal, we are good. | ||
if (a === b) { | ||
return true; | ||
} | ||
} | ||
} | ||
function graphQLResultHasError(result) { | ||
return result.errors && result.errors.length; | ||
} | ||
/** | ||
* Performs a deep equality check on two JavaScript values. | ||
*/ | ||
function isEqual(a, b) { | ||
// If the two values are strictly equal, we are good. | ||
if (a === b) { | ||
return true; | ||
} | ||
// Dates are equivalent if their time values are equal. | ||
if (a instanceof Date && b instanceof Date) { | ||
return a.getTime() === b.getTime(); | ||
} | ||
// If a and b are both objects, we will compare their properties. This will compare arrays as | ||
// well. | ||
if (a != null && | ||
typeof a === 'object' && | ||
b != null && | ||
typeof b === 'object') { | ||
// Compare all of the keys in `a`. If one of the keys has a different value, or that key does | ||
// not exist in `b` return false immediately. | ||
for (var key in a) { | ||
if (Object.prototype.hasOwnProperty.call(a, key)) { | ||
if (!Object.prototype.hasOwnProperty.call(b, key)) { | ||
return false; | ||
// Dates are equivalent if their time values are equal. | ||
if (a instanceof Date && b instanceof Date) { | ||
return a.getTime() === b.getTime(); | ||
} | ||
// If a and b are both objects, we will compare their properties. This will compare arrays as | ||
// well. | ||
if (a != null && | ||
typeof a === 'object' && | ||
b != null && | ||
typeof b === 'object') { | ||
// Compare all of the keys in `a`. If one of the keys has a different value, or that key does | ||
// not exist in `b` return false immediately. | ||
for (var key in a) { | ||
if (Object.prototype.hasOwnProperty.call(a, key)) { | ||
if (!Object.prototype.hasOwnProperty.call(b, key)) { | ||
return false; | ||
} | ||
if (!isEqual(a[key], b[key])) { | ||
return false; | ||
} | ||
} | ||
if (!isEqual(a[key], b[key])) { | ||
} | ||
// Look through all the keys in `b`. If `b` has a key that `a` does not, return false. | ||
for (var key in b) { | ||
if (!Object.prototype.hasOwnProperty.call(a, key)) { | ||
return false; | ||
} | ||
} | ||
// If we made it this far the objects are equal! | ||
return true; | ||
} | ||
// Look through all the keys in `b`. If `b` has a key that `a` does not, return false. | ||
for (var key in b) { | ||
if (!Object.prototype.hasOwnProperty.call(a, key)) { | ||
return false; | ||
// Otherwise the values are not equal. | ||
return false; | ||
} | ||
// taken straight from https://github.com/substack/deep-freeze to avoid import hassles with rollup | ||
function deepFreeze(o) { | ||
Object.freeze(o); | ||
Object.getOwnPropertyNames(o).forEach(function (prop) { | ||
if (o.hasOwnProperty(prop) && | ||
o[prop] !== null && | ||
(typeof o[prop] === 'object' || typeof o[prop] === 'function') && | ||
!Object.isFrozen(o[prop])) { | ||
deepFreeze(o[prop]); | ||
} | ||
} | ||
// If we made it this far the objects are equal! | ||
return true; | ||
}); | ||
return o; | ||
} | ||
// Otherwise the values are not equal. | ||
return false; | ||
} | ||
// taken straight from https://github.com/substack/deep-freeze to avoid import hassles with rollup | ||
function deepFreeze(o) { | ||
Object.freeze(o); | ||
Object.getOwnPropertyNames(o).forEach(function (prop) { | ||
if (o.hasOwnProperty(prop) && | ||
o[prop] !== null && | ||
(typeof o[prop] === 'object' || typeof o[prop] === 'function') && | ||
!Object.isFrozen(o[prop])) { | ||
deepFreeze(o[prop]); | ||
function maybeDeepFreeze(obj) { | ||
if (isDevelopment() || isTest()) { | ||
// Polyfilled Symbols potentially cause infinite / very deep recursion while deep freezing | ||
// which is known to crash IE11 (https://github.com/apollographql/apollo-client/issues/3043). | ||
var symbolIsPolyfilled = typeof Symbol === 'function' && typeof Symbol('') === 'string'; | ||
if (!symbolIsPolyfilled) { | ||
return deepFreeze(obj); | ||
} | ||
} | ||
}); | ||
return o; | ||
} | ||
function maybeDeepFreeze(obj) { | ||
if (isDevelopment() || isTest()) { | ||
// Polyfilled Symbols potentially cause infinite / very deep recursion while deep freezing | ||
// which is known to crash IE11 (https://github.com/apollographql/apollo-client/issues/3043). | ||
var symbolIsPolyfilled = typeof Symbol === 'function' && typeof Symbol('') === 'string'; | ||
if (!symbolIsPolyfilled) { | ||
return deepFreeze(obj); | ||
} | ||
return obj; | ||
} | ||
return obj; | ||
} | ||
var haveWarned = Object.create({}); | ||
/** | ||
* Print a warning only once in development. | ||
* In production no warnings are printed. | ||
* In test all warnings are printed. | ||
* | ||
* @param msg The warning message | ||
* @param type warn or error (will call console.warn or console.error) | ||
*/ | ||
function warnOnceInDevelopment(msg, type) { | ||
if (type === void 0) { type = 'warn'; } | ||
if (isProduction()) { | ||
return; | ||
} | ||
if (!haveWarned[msg]) { | ||
if (!isTest()) { | ||
haveWarned[msg] = true; | ||
var haveWarned = Object.create({}); | ||
/** | ||
* Print a warning only once in development. | ||
* In production no warnings are printed. | ||
* In test all warnings are printed. | ||
* | ||
* @param msg The warning message | ||
* @param type warn or error (will call console.warn or console.error) | ||
*/ | ||
function warnOnceInDevelopment(msg, type) { | ||
if (type === void 0) { type = 'warn'; } | ||
if (isProduction()) { | ||
return; | ||
} | ||
switch (type) { | ||
case 'error': | ||
console.error(msg); | ||
break; | ||
default: | ||
console.warn(msg); | ||
if (!haveWarned[msg]) { | ||
if (!isTest()) { | ||
haveWarned[msg] = true; | ||
} | ||
switch (type) { | ||
case 'error': | ||
console.error(msg); | ||
break; | ||
default: | ||
console.warn(msg); | ||
} | ||
} | ||
} | ||
} | ||
exports.getDirectiveInfoFromField = getDirectiveInfoFromField; | ||
exports.shouldInclude = shouldInclude; | ||
exports.flattenSelections = flattenSelections; | ||
exports.getDirectiveNames = getDirectiveNames; | ||
exports.hasDirectives = hasDirectives; | ||
exports.getFragmentQueryDocument = getFragmentQueryDocument; | ||
exports.getMutationDefinition = getMutationDefinition; | ||
exports.checkDocument = checkDocument; | ||
exports.getOperationDefinition = getOperationDefinition; | ||
exports.getOperationDefinitionOrDie = getOperationDefinitionOrDie; | ||
exports.getOperationName = getOperationName; | ||
exports.getFragmentDefinitions = getFragmentDefinitions; | ||
exports.getQueryDefinition = getQueryDefinition; | ||
exports.getFragmentDefinition = getFragmentDefinition; | ||
exports.getMainDefinition = getMainDefinition; | ||
exports.createFragmentMap = createFragmentMap; | ||
exports.getDefaultValues = getDefaultValues; | ||
exports.variablesInOperation = variablesInOperation; | ||
exports.removeDirectivesFromDocument = removeDirectivesFromDocument; | ||
exports.addTypenameToDocument = addTypenameToDocument; | ||
exports.removeConnectionDirectiveFromDocument = removeConnectionDirectiveFromDocument; | ||
exports.getDirectivesFromDocument = getDirectivesFromDocument; | ||
exports.isScalarValue = isScalarValue; | ||
exports.isNumberValue = isNumberValue; | ||
exports.valueToObjectRepresentation = valueToObjectRepresentation; | ||
exports.storeKeyNameFromField = storeKeyNameFromField; | ||
exports.getStoreKeyName = getStoreKeyName; | ||
exports.argumentsObjectFromField = argumentsObjectFromField; | ||
exports.resultKeyNameFromField = resultKeyNameFromField; | ||
exports.isField = isField; | ||
exports.isInlineFragment = isInlineFragment; | ||
exports.isIdValue = isIdValue; | ||
exports.toIdValue = toIdValue; | ||
exports.isJsonValue = isJsonValue; | ||
exports.valueFromNode = valueFromNode; | ||
exports.assign = assign; | ||
exports.cloneDeep = cloneDeep; | ||
exports.getEnv = getEnv; | ||
exports.isEnv = isEnv; | ||
exports.isProduction = isProduction; | ||
exports.isDevelopment = isDevelopment; | ||
exports.isTest = isTest; | ||
exports.tryFunctionOrLogError = tryFunctionOrLogError; | ||
exports.graphQLResultHasError = graphQLResultHasError; | ||
exports.isEqual = isEqual; | ||
exports.maybeDeepFreeze = maybeDeepFreeze; | ||
exports.warnOnceInDevelopment = warnOnceInDevelopment; | ||
exports.getDirectiveInfoFromField = getDirectiveInfoFromField; | ||
exports.shouldInclude = shouldInclude; | ||
exports.flattenSelections = flattenSelections; | ||
exports.getDirectiveNames = getDirectiveNames; | ||
exports.hasDirectives = hasDirectives; | ||
exports.getFragmentQueryDocument = getFragmentQueryDocument; | ||
exports.getMutationDefinition = getMutationDefinition; | ||
exports.checkDocument = checkDocument; | ||
exports.getOperationDefinition = getOperationDefinition; | ||
exports.getOperationDefinitionOrDie = getOperationDefinitionOrDie; | ||
exports.getOperationName = getOperationName; | ||
exports.getFragmentDefinitions = getFragmentDefinitions; | ||
exports.getQueryDefinition = getQueryDefinition; | ||
exports.getFragmentDefinition = getFragmentDefinition; | ||
exports.getMainDefinition = getMainDefinition; | ||
exports.createFragmentMap = createFragmentMap; | ||
exports.getDefaultValues = getDefaultValues; | ||
exports.variablesInOperation = variablesInOperation; | ||
exports.removeDirectivesFromDocument = removeDirectivesFromDocument; | ||
exports.addTypenameToDocument = addTypenameToDocument; | ||
exports.removeConnectionDirectiveFromDocument = removeConnectionDirectiveFromDocument; | ||
exports.getDirectivesFromDocument = getDirectivesFromDocument; | ||
exports.isScalarValue = isScalarValue; | ||
exports.isNumberValue = isNumberValue; | ||
exports.valueToObjectRepresentation = valueToObjectRepresentation; | ||
exports.storeKeyNameFromField = storeKeyNameFromField; | ||
exports.getStoreKeyName = getStoreKeyName; | ||
exports.argumentsObjectFromField = argumentsObjectFromField; | ||
exports.resultKeyNameFromField = resultKeyNameFromField; | ||
exports.isField = isField; | ||
exports.isInlineFragment = isInlineFragment; | ||
exports.isIdValue = isIdValue; | ||
exports.toIdValue = toIdValue; | ||
exports.isJsonValue = isJsonValue; | ||
exports.valueFromNode = valueFromNode; | ||
exports.assign = assign; | ||
exports.cloneDeep = cloneDeep; | ||
exports.getEnv = getEnv; | ||
exports.isEnv = isEnv; | ||
exports.isProduction = isProduction; | ||
exports.isDevelopment = isDevelopment; | ||
exports.isTest = isTest; | ||
exports.tryFunctionOrLogError = tryFunctionOrLogError; | ||
exports.graphQLResultHasError = graphQLResultHasError; | ||
exports.isEqual = isEqual; | ||
exports.maybeDeepFreeze = maybeDeepFreeze; | ||
exports.warnOnceInDevelopment = warnOnceInDevelopment; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
//# sourceMappingURL=bundle.umd.js.map |
{ | ||
"name": "apollo-utilities", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Utilities for working with GraphQL ASTs", | ||
@@ -41,14 +41,14 @@ "author": "James Baxley <james@meteor.com>", | ||
"devDependencies": { | ||
"@types/graphql": "0.12.5", | ||
"@types/graphql": "0.12.7", | ||
"@types/jest": "21.1.10", | ||
"@types/lodash": "4.14.105", | ||
"@types/node": "8.9.5", | ||
"@types/lodash": "4.14.108", | ||
"@types/node": "8.10.11", | ||
"browserify": "15.2.0", | ||
"flow-bin": "0.68.0", | ||
"flow-bin": "0.71.0", | ||
"graphql": "0.13.2", | ||
"graphql-tag": "2.8.0", | ||
"graphql-tag": "2.9.2", | ||
"jest": "20.0.4", | ||
"lodash": "4.17.5", | ||
"lodash": "4.17.10", | ||
"rimraf": "2.6.2", | ||
"rollup": "0.56.4", | ||
"rollup": "0.58.2", | ||
"ts-jest": "20.0.14", | ||
@@ -55,0 +55,0 @@ "tslint": "5.9.1", |
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
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
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
276878
1