graphql-request
Version:
Minimal GraphQL client supporting Node and browsers for scripts or simple apps.
45 lines • 1.75 kB
JavaScript
import { tryCatch } from '../../lib/prelude.js';
import { isOperationDefinitionNode } from '../lib/graphql.js';
/**
* Refactored imports from `graphql` to be more specific, this helps import only the required files (100KiB)
* instead of the entire package (greater than 500KiB) where tree-shaking is not supported.
* @see https://github.com/jasonkuhrt/graphql-request/pull/543
*/
import { OperationTypeNode } from 'graphql';
import { parse } from 'graphql';
import { print } from 'graphql';
/**
* helpers
*/
const extractOperationName = (document) => {
let operationName = undefined;
const defs = document.definitions.filter(isOperationDefinitionNode);
if (defs.length === 1) {
operationName = defs[0].name?.value;
}
return operationName;
};
const extractIsMutation = (document) => {
let isMutation = false;
const defs = document.definitions.filter(isOperationDefinitionNode);
if (defs.length === 1) {
isMutation = defs[0].operation === OperationTypeNode.MUTATION;
}
return isMutation;
};
export const analyzeDocument = (document, excludeOperationName) => {
const expression = typeof document === `string` ? document : print(document);
let isMutation = false;
let operationName = undefined;
if (excludeOperationName) {
return { expression, isMutation, operationName };
}
const docNode = tryCatch(() => (typeof document === `string` ? parse(document) : document));
if (docNode instanceof Error) {
return { expression, isMutation, operationName };
}
operationName = extractOperationName(docNode);
isMutation = extractIsMutation(docNode);
return { expression, operationName, isMutation };
};
//# sourceMappingURL=analyzeDocument.js.map