@apollo/client
Version:
A fully-featured caching GraphQL client.
94 lines • 4.23 kB
JavaScript
import { invariant } from "../../utilities/globals/index.js";
export var DocumentType;
(function (DocumentType) {
DocumentType[DocumentType["Query"] = 0] = "Query";
DocumentType[DocumentType["Mutation"] = 1] = "Mutation";
DocumentType[DocumentType["Subscription"] = 2] = "Subscription";
})(DocumentType || (DocumentType = {}));
var cache = new Map();
export function operationName(type) {
var name;
switch (type) {
case DocumentType.Query:
name = 'Query';
break;
case DocumentType.Mutation:
name = 'Mutation';
break;
case DocumentType.Subscription:
name = 'Subscription';
break;
}
return name;
}
export function parser(document) {
var cached = cache.get(document);
if (cached)
return cached;
var variables, type, name;
__DEV__ ? invariant(!!document && !!document.kind, "Argument of ".concat(document, " passed to parser was not a valid GraphQL ") +
"DocumentNode. You may need to use 'graphql-tag' or another method " +
"to convert your operation into a document") : invariant(!!document && !!document.kind, 33);
var fragments = [];
var queries = [];
var mutations = [];
var subscriptions = [];
for (var _i = 0, _a = document.definitions; _i < _a.length; _i++) {
var x = _a[_i];
if (x.kind === 'FragmentDefinition') {
fragments.push(x);
continue;
}
if (x.kind === 'OperationDefinition') {
switch (x.operation) {
case 'query':
queries.push(x);
break;
case 'mutation':
mutations.push(x);
break;
case 'subscription':
subscriptions.push(x);
break;
}
}
}
__DEV__ ? invariant(!fragments.length ||
(queries.length || mutations.length || subscriptions.length), "Passing only a fragment to 'graphql' is not yet supported. " +
"You must include a query, subscription or mutation as well") : invariant(!fragments.length ||
(queries.length || mutations.length || subscriptions.length), 34);
__DEV__ ? invariant(queries.length + mutations.length + subscriptions.length <= 1, "react-apollo only supports a query, subscription, or a mutation per HOC. " +
"".concat(document, " had ").concat(queries.length, " queries, ").concat(subscriptions.length, " ") +
"subscriptions and ".concat(mutations.length, " mutations. ") +
"You can use 'compose' to join multiple operation types to a component") : invariant(queries.length + mutations.length + subscriptions.length <= 1, 35);
type = queries.length ? DocumentType.Query : DocumentType.Mutation;
if (!queries.length && !mutations.length)
type = DocumentType.Subscription;
var definitions = queries.length
? queries
: mutations.length
? mutations
: subscriptions;
__DEV__ ? invariant(definitions.length === 1, "react-apollo only supports one definition per HOC. ".concat(document, " had ") +
"".concat(definitions.length, " definitions. ") +
"You can use 'compose' to join multiple operation types to a component") : invariant(definitions.length === 1, 36);
var definition = definitions[0];
variables = definition.variableDefinitions || [];
if (definition.name && definition.name.kind === 'Name') {
name = definition.name.value;
}
else {
name = 'data';
}
var payload = { name: name, type: type, variables: variables };
cache.set(document, payload);
return payload;
}
export function verifyDocumentType(document, type) {
var operation = parser(document);
var requiredOperationName = operationName(type);
var usedOperationName = operationName(operation.type);
__DEV__ ? invariant(operation.type === type, "Running a ".concat(requiredOperationName, " requires a graphql ") +
"".concat(requiredOperationName, ", but a ").concat(usedOperationName, " was used instead.")) : invariant(operation.type === type, 37);
}
//# sourceMappingURL=index.js.map