UNPKG

@graphql-inspector/core

Version:

Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.

40 lines (39 loc) 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readDocument = readDocument; const graphql_1 = require("graphql"); function readDocument(source) { const result = { source, fragments: [], operations: [], hasFragments: false, hasOperations: false, }; const documentNode = (0, graphql_1.parse)(source.body); const filepath = source.name; const definitions = documentNode.definitions || []; for (const node of definitions) { if (isOperation(node)) { result.operations.push({ node, source: filepath, }); } else if (isFragment(node)) { result.fragments.push({ node, source: filepath, }); } } result.hasFragments = result.fragments.length > 0; result.hasOperations = result.operations.length > 0; return result; } function isOperation(node) { return node.kind === graphql_1.Kind.OPERATION_DEFINITION; } function isFragment(node) { return node.kind === graphql_1.Kind.FRAGMENT_DEFINITION; }