UNPKG

@graphql-eslint/eslint-plugin

Version:
50 lines (47 loc) 2.7 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var _fastglob = require('fast-glob'); var _fastglob2 = _interopRequireDefault(_fastglob); var _graphql = require('graphql'); var _cachejs = require('./cache.js'); const schemaCache = new (0, _cachejs.ModuleCache)(); const debug = _debug2.default.call(void 0, "graphql-eslint:schema"); function getSchema(project) { const schemaKey = project.schema; if (!schemaKey) { return null; } const cache = schemaCache.get(schemaKey); if (cache) { return cache; } debug("Loading schema from %o", project.schema); const opts = { pluckConfig: project.extensions.pluckConfig }; const typeDefs = project.loadSchemaSync(project.schema, "DocumentNode", opts); let isFederation = false; _graphql.visit.call(void 0, typeDefs, { SchemaExtension(node) { const linkDirective = _optionalChain([node, 'access', _ => _.directives, 'optionalAccess', _2 => _2.find, 'call', _3 => _3((d) => d.name.value === "link")]); if (!linkDirective) return _graphql.BREAK; const urlArgument = _optionalChain([linkDirective, 'access', _4 => _4.arguments, 'optionalAccess', _5 => _5.find, 'call', _6 => _6((a) => a.name.value === "url")]); if (!urlArgument) return _graphql.BREAK; if (urlArgument.value.kind !== "StringValue") return _graphql.BREAK; isFederation = urlArgument.value.value.includes("specs.apollo.dev/federation/"); } }); let schema; if (isFederation) { const { buildSubgraphSchema } = require("@apollo/subgraph"); schema = buildSubgraphSchema({ typeDefs }); } else { schema = project.loadSchemaSync(project.schema, "GraphQLSchema", opts); } if (debug.enabled) { debug("Schema loaded: %o", schema instanceof _graphql.GraphQLSchema); const schemaPaths = _fastglob2.default.sync(project.schema, { absolute: true }); debug("Schema pointers %O", schemaPaths); } schemaCache.set(schemaKey, schema); return schema; } exports.getSchema = getSchema;