UNPKG

@ace-fetch/graphql

Version:

Fetch Provider.

110 lines (109 loc) 4.61 kB
var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; /** * fork from https://github.com/apollographql/apollo-client/blob/main/src/react/parser/index.ts * for using without react */ import { invariant } from 'ts-invariant'; import { DocumentType } from '../constants'; var cache = new WeakMap(); 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; } // This parser is mostly used to safety check incoming documents. export function parser(document) { var e_1, _a; var cached = cache.get(document); if (cached) return cached; var variables, type, name; 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"); var fragments = []; var queries = []; var mutations = []; var subscriptions = []; try { for (var _b = __values(document.definitions), _c = _b.next(); !_c.done; _c = _b.next()) { var x = _c.value; 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; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } 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(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"); type = queries.length ? DocumentType.Query : DocumentType.Mutation; if (!queries.length && !mutations.length) type = DocumentType.Subscription; var definitions = queries.length ? queries : mutations.length ? mutations : subscriptions; 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"); var definition = definitions[0]; // eslint-disable-next-line prefer-const variables = definition.variableDefinitions || []; if (definition.name && definition.name.kind === 'Name') { name = definition.name.value; } else { name = 'data'; // fallback to using data if no name } 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); invariant(operation.type === type, "Running a ".concat(requiredOperationName, " requires a graphql ") + "".concat(requiredOperationName, ", but a ").concat(usedOperationName, " was used instead.")); }