123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915 |
- import { visit } from 'graphql/language/visitor';
- import { InvariantError, invariant } from 'ts-invariant';
- import { __assign, __spreadArrays } from 'tslib';
- import stringify from 'fast-json-stable-stringify';
- export { equal as isEqual } from '@wry/equality';
- 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);
- }
- 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);
- });
- 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 {
- throw process.env.NODE_ENV === "production" ? new InvariantError(17) : new InvariantError("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\"" +
- 'is not supported. Use variables instead of inline arguments to ' +
- 'overcome this limitation.');
- }
- }
- 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);
- }
- 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 {
- return directives['connection']['key'];
- }
- }
- var completeFieldName = fieldName;
- if (args) {
- var stringifiedArgs = 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;
- }
- 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;
- }
- 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' &&
- typeof idObject.generated === 'boolean';
- }
- 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 process.env.NODE_ENV === "production" ? new InvariantError(18) : new InvariantError("Variable nodes are not supported by valueFromNode");
- }
- 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, 10);
- 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;
- }
- 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);
- });
- return directiveObj_1;
- }
- return null;
- }
- function shouldInclude(selection, variables) {
- if (variables === void 0) { variables = {}; }
- return getInclusionDirectives(selection.directives).every(function (_a) {
- var directive = _a.directive, ifArgument = _a.ifArgument;
- var evaledValue = false;
- if (ifArgument.value.kind === 'Variable') {
- evaledValue = variables[ifArgument.value.name.value];
- process.env.NODE_ENV === "production" ? invariant(evaledValue !== void 0, 13) : invariant(evaledValue !== void 0, "Invalid variable referenced in @" + directive.name.value + " directive.");
- }
- else {
- evaledValue = ifArgument.value.value;
- }
- return directive.name.value === 'skip' ? !evaledValue : evaledValue;
- });
- }
- function getDirectiveNames(doc) {
- var names = [];
- visit(doc, {
- Directive: function (node) {
- names.push(node.name.value);
- },
- });
- return names;
- }
- function hasDirectives(names, doc) {
- return getDirectiveNames(doc).some(function (name) { return names.indexOf(name) > -1; });
- }
- function hasClientExports(document) {
- return (document &&
- hasDirectives(['client'], document) &&
- hasDirectives(['export'], document));
- }
- function isInclusionDirective(_a) {
- var value = _a.name.value;
- return value === 'skip' || value === 'include';
- }
- function getInclusionDirectives(directives) {
- return directives ? directives.filter(isInclusionDirective).map(function (directive) {
- var directiveArguments = directive.arguments;
- var directiveName = directive.name.value;
- process.env.NODE_ENV === "production" ? invariant(directiveArguments && directiveArguments.length === 1, 14) : invariant(directiveArguments && directiveArguments.length === 1, "Incorrect number of arguments for the @" + directiveName + " directive.");
- var ifArgument = directiveArguments[0];
- process.env.NODE_ENV === "production" ? invariant(ifArgument.name && ifArgument.name.value === 'if', 15) : invariant(ifArgument.name && ifArgument.name.value === 'if', "Invalid argument for the @" + directiveName + " directive.");
- var ifValue = ifArgument.value;
- process.env.NODE_ENV === "production" ? invariant(ifValue &&
- (ifValue.kind === 'Variable' || ifValue.kind === 'BooleanValue'), 16) : invariant(ifValue &&
- (ifValue.kind === 'Variable' || ifValue.kind === 'BooleanValue'), "Argument for the @" + directiveName + " directive must be a variable or a boolean value.");
- return { directive: directive, ifArgument: ifArgument };
- }) : [];
- }
- function getFragmentQueryDocument(document, fragmentName) {
- var actualFragmentName = fragmentName;
- var fragments = [];
- document.definitions.forEach(function (definition) {
- if (definition.kind === 'OperationDefinition') {
- throw process.env.NODE_ENV === "production" ? new InvariantError(11) : new InvariantError("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.');
- }
- if (definition.kind === 'FragmentDefinition') {
- fragments.push(definition);
- }
- });
- if (typeof actualFragmentName === 'undefined') {
- process.env.NODE_ENV === "production" ? invariant(fragments.length === 1, 12) : invariant(fragments.length === 1, "Found " + fragments.length + " fragments. `fragmentName` must be provided when there is not exactly 1 fragment.");
- actualFragmentName = fragments[0].name.value;
- }
- var query = __assign(__assign({}, document), { definitions: __spreadArrays([
- {
- kind: 'OperationDefinition',
- operation: 'query',
- selectionSet: {
- kind: 'SelectionSet',
- selections: [
- {
- kind: 'FragmentSpread',
- name: {
- kind: 'Name',
- value: actualFragmentName,
- },
- },
- ],
- },
- }
- ], document.definitions) });
- return query;
- }
- function assign(target) {
- var sources = [];
- for (var _i = 1; _i < arguments.length; _i++) {
- sources[_i - 1] = arguments[_i];
- }
- sources.forEach(function (source) {
- if (typeof source === 'undefined' || source === null) {
- return;
- }
- Object.keys(source).forEach(function (key) {
- target[key] = source[key];
- });
- });
- return target;
- }
- function getMutationDefinition(doc) {
- checkDocument(doc);
- var mutationDef = doc.definitions.filter(function (definition) {
- return definition.kind === 'OperationDefinition' &&
- definition.operation === 'mutation';
- })[0];
- process.env.NODE_ENV === "production" ? invariant(mutationDef, 1) : invariant(mutationDef, 'Must contain a mutation definition.');
- return mutationDef;
- }
- function checkDocument(doc) {
- process.env.NODE_ENV === "production" ? invariant(doc && doc.kind === 'Document', 2) : invariant(doc && doc.kind === 'Document', "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");
- var operations = doc.definitions
- .filter(function (d) { return d.kind !== 'FragmentDefinition'; })
- .map(function (definition) {
- if (definition.kind !== 'OperationDefinition') {
- throw process.env.NODE_ENV === "production" ? new InvariantError(3) : new InvariantError("Schema type definitions not allowed in queries. Found: \"" + definition.kind + "\"");
- }
- return definition;
- });
- process.env.NODE_ENV === "production" ? invariant(operations.length <= 1, 4) : invariant(operations.length <= 1, "Ambiguous GraphQL document: contains " + operations.length + " operations");
- return doc;
- }
- function getOperationDefinition(doc) {
- checkDocument(doc);
- return doc.definitions.filter(function (definition) { return definition.kind === 'OperationDefinition'; })[0];
- }
- function getOperationDefinitionOrDie(document) {
- var def = getOperationDefinition(document);
- process.env.NODE_ENV === "production" ? invariant(def, 5) : invariant(def, "GraphQL document is missing an operation");
- 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);
- }
- function getFragmentDefinitions(doc) {
- return doc.definitions.filter(function (definition) { return definition.kind === 'FragmentDefinition'; });
- }
- function getQueryDefinition(doc) {
- var queryDef = getOperationDefinition(doc);
- process.env.NODE_ENV === "production" ? invariant(queryDef && queryDef.operation === 'query', 6) : invariant(queryDef && queryDef.operation === 'query', 'Must contain a query definition.');
- return queryDef;
- }
- function getFragmentDefinition(doc) {
- process.env.NODE_ENV === "production" ? invariant(doc.kind === 'Document', 7) : invariant(doc.kind === 'Document', "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");
- process.env.NODE_ENV === "production" ? invariant(doc.definitions.length <= 1, 8) : invariant(doc.definitions.length <= 1, 'Fragment must have exactly one definition.');
- var fragmentDef = doc.definitions[0];
- process.env.NODE_ENV === "production" ? invariant(fragmentDef.kind === 'FragmentDefinition', 9) : invariant(fragmentDef.kind === 'FragmentDefinition', 'Must be a fragment definition.');
- return fragmentDef;
- }
- 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) {
- fragmentDefinition = definition;
- }
- }
- if (fragmentDefinition) {
- return fragmentDefinition;
- }
- throw process.env.NODE_ENV === "production" ? new InvariantError(10) : new InvariantError('Expected a parsed GraphQL query with a query, mutation, subscription, or a fragment.');
- }
- 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;
- });
- return assign.apply(void 0, __spreadArrays([{}], defaultValues));
- }
- return {};
- }
- 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;
- }
- function filterInPlace(array, test, context) {
- var target = 0;
- array.forEach(function (elem, i) {
- if (test.call(this, elem, i, array)) {
- array[target++] = elem;
- }
- }, context);
- array.length = target;
- return array;
- }
- var TYPENAME_FIELD = {
- kind: 'Field',
- name: {
- kind: 'Name',
- value: '__typename',
- },
- };
- function isEmpty(op, fragments) {
- return op.selectionSet.selections.every(function (selection) {
- return selection.kind === 'FragmentSpread' &&
- isEmpty(fragments[selection.name.value], fragments);
- });
- }
- function nullIfDocIsEmpty(doc) {
- return isEmpty(getOperationDefinition(doc) || getFragmentDefinition(doc), createFragmentMap(getFragmentDefinitions(doc)))
- ? null
- : doc;
- }
- function getDirectiveMatcher(directives) {
- return function directiveMatcher(directive) {
- return directives.some(function (dir) {
- return (dir.name && dir.name === directive.name.value) ||
- (dir.test && dir.test(directive));
- });
- };
- }
- function removeDirectivesFromDocument(directives, doc) {
- var variablesInUse = Object.create(null);
- var variablesToRemove = [];
- var fragmentSpreadsInUse = Object.create(null);
- var fragmentSpreadsToRemove = [];
- var modifiedDoc = nullIfDocIsEmpty(visit(doc, {
- Variable: {
- enter: function (node, _key, parent) {
- if (parent.kind !== 'VariableDefinition') {
- variablesInUse[node.name.value] = true;
- }
- },
- },
- Field: {
- enter: function (node) {
- if (directives && node.directives) {
- var shouldRemoveField = directives.some(function (directive) { return directive.remove; });
- if (shouldRemoveField &&
- node.directives &&
- node.directives.some(getDirectiveMatcher(directives))) {
- if (node.arguments) {
- node.arguments.forEach(function (arg) {
- if (arg.value.kind === 'Variable') {
- variablesToRemove.push({
- name: arg.value.name.value,
- });
- }
- });
- }
- if (node.selectionSet) {
- getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(function (frag) {
- fragmentSpreadsToRemove.push({
- name: frag.name.value,
- });
- });
- }
- return null;
- }
- }
- },
- },
- FragmentSpread: {
- enter: function (node) {
- fragmentSpreadsInUse[node.name.value] = true;
- },
- },
- Directive: {
- enter: function (node) {
- if (getDirectiveMatcher(directives)(node)) {
- return null;
- }
- },
- },
- }));
- if (modifiedDoc &&
- filterInPlace(variablesToRemove, function (v) { return !variablesInUse[v.name]; }).length) {
- modifiedDoc = removeArgumentsFromDocument(variablesToRemove, modifiedDoc);
- }
- if (modifiedDoc &&
- filterInPlace(fragmentSpreadsToRemove, function (fs) { return !fragmentSpreadsInUse[fs.name]; })
- .length) {
- modifiedDoc = removeFragmentSpreadFromDocument(fragmentSpreadsToRemove, modifiedDoc);
- }
- return modifiedDoc;
- }
- function addTypenameToDocument(doc) {
- return visit(checkDocument(doc), {
- SelectionSet: {
- enter: function (node, _key, parent) {
- if (parent &&
- parent.kind === 'OperationDefinition') {
- return;
- }
- var selections = node.selections;
- if (!selections) {
- return;
- }
- var skip = selections.some(function (selection) {
- return (isField(selection) &&
- (selection.name.value === '__typename' ||
- selection.name.value.lastIndexOf('__', 0) === 0));
- });
- if (skip) {
- return;
- }
- var field = parent;
- if (isField(field) &&
- field.directives &&
- field.directives.some(function (d) { return d.name.value === 'export'; })) {
- return;
- }
- return __assign(__assign({}, node), { selections: __spreadArrays(selections, [TYPENAME_FIELD]) });
- },
- },
- });
- }
- 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'; })) {
- process.env.NODE_ENV === "production" || invariant.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;
- },
- };
- function removeConnectionDirectiveFromDocument(doc) {
- return removeDirectivesFromDocument([connectionRemoveConfig], checkDocument(doc));
- }
- function hasDirectivesInSelectionSet(directives, selectionSet, nestedCheck) {
- if (nestedCheck === void 0) { nestedCheck = true; }
- return (selectionSet &&
- selectionSet.selections &&
- selectionSet.selections.some(function (selection) {
- return hasDirectivesInSelection(directives, selection, nestedCheck);
- }));
- }
- function hasDirectivesInSelection(directives, selection, nestedCheck) {
- if (nestedCheck === void 0) { nestedCheck = true; }
- if (!isField(selection)) {
- return true;
- }
- if (!selection.directives) {
- return false;
- }
- return (selection.directives.some(getDirectiveMatcher(directives)) ||
- (nestedCheck &&
- hasDirectivesInSelectionSet(directives, selection.selectionSet, nestedCheck)));
- }
- function getDirectivesFromDocument(directives, doc) {
- checkDocument(doc);
- var parentPath;
- return nullIfDocIsEmpty(visit(doc, {
- SelectionSet: {
- enter: function (node, _key, _parent, path) {
- var currentPath = path.join('-');
- if (!parentPath ||
- currentPath === parentPath ||
- !currentPath.startsWith(parentPath)) {
- if (node.selections) {
- var selectionsWithDirectives = node.selections.filter(function (selection) { return hasDirectivesInSelection(directives, selection); });
- if (hasDirectivesInSelectionSet(directives, node, false)) {
- parentPath = currentPath;
- }
- return __assign(__assign({}, node), { selections: selectionsWithDirectives });
- }
- else {
- return null;
- }
- }
- },
- },
- }));
- }
- function getArgumentMatcher(config) {
- return function argumentMatcher(argument) {
- return config.some(function (aConfig) {
- return argument.value &&
- argument.value.kind === 'Variable' &&
- argument.value.name &&
- (aConfig.name === argument.value.name.value ||
- (aConfig.test && aConfig.test(argument)));
- });
- };
- }
- function removeArgumentsFromDocument(config, doc) {
- var argMatcher = getArgumentMatcher(config);
- return nullIfDocIsEmpty(visit(doc, {
- OperationDefinition: {
- enter: function (node) {
- return __assign(__assign({}, node), { variableDefinitions: node.variableDefinitions.filter(function (varDef) {
- return !config.some(function (arg) { return arg.name === varDef.variable.name.value; });
- }) });
- },
- },
- Field: {
- enter: function (node) {
- var shouldRemoveField = config.some(function (argConfig) { return argConfig.remove; });
- if (shouldRemoveField) {
- var argMatchCount_1 = 0;
- node.arguments.forEach(function (arg) {
- if (argMatcher(arg)) {
- argMatchCount_1 += 1;
- }
- });
- if (argMatchCount_1 === 1) {
- return null;
- }
- }
- },
- },
- Argument: {
- enter: function (node) {
- if (argMatcher(node)) {
- return null;
- }
- },
- },
- }));
- }
- function removeFragmentSpreadFromDocument(config, doc) {
- function enter(node) {
- if (config.some(function (def) { return def.name === node.name.value; })) {
- return null;
- }
- }
- return nullIfDocIsEmpty(visit(doc, {
- FragmentSpread: { enter: enter },
- FragmentDefinition: { enter: enter },
- }));
- }
- function getAllFragmentSpreadsFromSelectionSet(selectionSet) {
- var allFragments = [];
- selectionSet.selections.forEach(function (selection) {
- if ((isField(selection) || isInlineFragment(selection)) &&
- selection.selectionSet) {
- getAllFragmentSpreadsFromSelectionSet(selection.selectionSet).forEach(function (frag) { return allFragments.push(frag); });
- }
- else if (selection.kind === 'FragmentSpread') {
- allFragments.push(selection);
- }
- });
- return allFragments;
- }
- function buildQueryFromSelectionSet(document) {
- var definition = getMainDefinition(document);
- var definitionOperation = definition.operation;
- if (definitionOperation === 'query') {
- return document;
- }
- var modifiedDoc = visit(document, {
- OperationDefinition: {
- enter: function (node) {
- return __assign(__assign({}, node), { operation: 'query' });
- },
- },
- });
- return modifiedDoc;
- }
- function removeClientSetsFromDocument(document) {
- checkDocument(document);
- var modifiedDoc = removeDirectivesFromDocument([
- {
- test: function (directive) { return directive.name.value === 'client'; },
- remove: true,
- },
- ], document);
- if (modifiedDoc) {
- modifiedDoc = visit(modifiedDoc, {
- FragmentDefinition: {
- enter: function (node) {
- if (node.selectionSet) {
- var isTypenameOnly = node.selectionSet.selections.every(function (selection) {
- return isField(selection) && selection.name.value === '__typename';
- });
- if (isTypenameOnly) {
- return null;
- }
- }
- },
- },
- });
- }
- return modifiedDoc;
- }
- var canUseWeakMap = typeof WeakMap === 'function' && !(typeof navigator === 'object' &&
- navigator.product === 'ReactNative');
- var toString = Object.prototype.toString;
- function cloneDeep(value) {
- return cloneDeepHelper(value, new Map());
- }
- function cloneDeepHelper(val, seen) {
- switch (toString.call(val)) {
- case "[object Array]": {
- if (seen.has(val))
- return seen.get(val);
- var copy_1 = val.slice(0);
- seen.set(val, copy_1);
- copy_1.forEach(function (child, i) {
- copy_1[i] = cloneDeepHelper(child, seen);
- });
- return copy_1;
- }
- case "[object Object]": {
- if (seen.has(val))
- return seen.get(val);
- var copy_2 = Object.create(Object.getPrototypeOf(val));
- seen.set(val, copy_2);
- Object.keys(val).forEach(function (key) {
- copy_2[key] = cloneDeepHelper(val[key], seen);
- });
- return copy_2;
- }
- default:
- return val;
- }
- }
- function getEnv() {
- if (typeof process !== 'undefined' && process.env.NODE_ENV) {
- return process.env.NODE_ENV;
- }
- 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 tryFunctionOrLogError(f) {
- try {
- return f();
- }
- catch (e) {
- if (console.error) {
- console.error(e);
- }
- }
- }
- function graphQLResultHasError(result) {
- return result.errors && result.errors.length;
- }
- function deepFreeze(o) {
- Object.freeze(o);
- Object.getOwnPropertyNames(o).forEach(function (prop) {
- if (o[prop] !== null &&
- (typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
- !Object.isFrozen(o[prop])) {
- deepFreeze(o[prop]);
- }
- });
- return o;
- }
- function maybeDeepFreeze(obj) {
- if (isDevelopment() || isTest()) {
- var symbolIsPolyfilled = typeof Symbol === 'function' && typeof Symbol('') === 'string';
- if (!symbolIsPolyfilled) {
- return deepFreeze(obj);
- }
- }
- return obj;
- }
- var hasOwnProperty = Object.prototype.hasOwnProperty;
- function mergeDeep() {
- var sources = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- sources[_i] = arguments[_i];
- }
- return mergeDeepArray(sources);
- }
- function mergeDeepArray(sources) {
- var target = sources[0] || {};
- var count = sources.length;
- if (count > 1) {
- var pastCopies = [];
- target = shallowCopyForMerge(target, pastCopies);
- for (var i = 1; i < count; ++i) {
- target = mergeHelper(target, sources[i], pastCopies);
- }
- }
- return target;
- }
- function isObject(obj) {
- return obj !== null && typeof obj === 'object';
- }
- function mergeHelper(target, source, pastCopies) {
- if (isObject(source) && isObject(target)) {
- if (Object.isExtensible && !Object.isExtensible(target)) {
- target = shallowCopyForMerge(target, pastCopies);
- }
- Object.keys(source).forEach(function (sourceKey) {
- var sourceValue = source[sourceKey];
- if (hasOwnProperty.call(target, sourceKey)) {
- var targetValue = target[sourceKey];
- if (sourceValue !== targetValue) {
- target[sourceKey] = mergeHelper(shallowCopyForMerge(targetValue, pastCopies), sourceValue, pastCopies);
- }
- }
- else {
- target[sourceKey] = sourceValue;
- }
- });
- return target;
- }
- return source;
- }
- function shallowCopyForMerge(value, pastCopies) {
- if (value !== null &&
- typeof value === 'object' &&
- pastCopies.indexOf(value) < 0) {
- if (Array.isArray(value)) {
- value = value.slice(0);
- }
- else {
- value = __assign({ __proto__: Object.getPrototypeOf(value) }, value);
- }
- pastCopies.push(value);
- }
- return value;
- }
- var haveWarned = Object.create({});
- function warnOnceInDevelopment(msg, type) {
- if (type === void 0) { type = 'warn'; }
- if (!isProduction() && !haveWarned[msg]) {
- if (!isTest()) {
- haveWarned[msg] = true;
- }
- if (type === 'error') {
- console.error(msg);
- }
- else {
- console.warn(msg);
- }
- }
- }
- function stripSymbols(data) {
- return JSON.parse(JSON.stringify(data));
- }
- export { addTypenameToDocument, argumentsObjectFromField, assign, buildQueryFromSelectionSet, canUseWeakMap, checkDocument, cloneDeep, createFragmentMap, getDefaultValues, getDirectiveInfoFromField, getDirectiveNames, getDirectivesFromDocument, getEnv, getFragmentDefinition, getFragmentDefinitions, getFragmentQueryDocument, getInclusionDirectives, getMainDefinition, getMutationDefinition, getOperationDefinition, getOperationDefinitionOrDie, getOperationName, getQueryDefinition, getStoreKeyName, graphQLResultHasError, hasClientExports, hasDirectives, isDevelopment, isEnv, isField, isIdValue, isInlineFragment, isJsonValue, isNumberValue, isProduction, isScalarValue, isTest, maybeDeepFreeze, mergeDeep, mergeDeepArray, removeArgumentsFromDocument, removeClientSetsFromDocument, removeConnectionDirectiveFromDocument, removeDirectivesFromDocument, removeFragmentSpreadFromDocument, resultKeyNameFromField, shouldInclude, storeKeyNameFromField, stripSymbols, toIdValue, tryFunctionOrLogError, valueFromNode, valueToObjectRepresentation, variablesInOperation, warnOnceInDevelopment };
- //# sourceMappingURL=bundle.esm.js.map
|