@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
45 lines (44 loc) • 1.7 kB
JavaScript
import { createRequire } from "module";
import debugFactory from "debug";
import fg from "fast-glob";
import { BREAK, GraphQLSchema, visit } from "graphql";
import { ModuleCache } from "./cache.js";
const schemaCache = new ModuleCache(), debug = debugFactory("graphql-eslint:schema"), require2 = createRequire(import.meta.url);
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
}, typeDefs = project.loadSchemaSync(project.schema, "DocumentNode", opts);
let isFederation = !1;
visit(typeDefs, {
SchemaExtension(node) {
const linkDirective = node.directives?.find((d) => d.name.value === "link");
if (!linkDirective) return BREAK;
const urlArgument = linkDirective.arguments?.find((a) => a.name.value === "url");
if (!urlArgument) return BREAK;
if (urlArgument.value.kind !== "StringValue") return BREAK;
isFederation = urlArgument.value.value.includes("specs.apollo.dev/federation/");
}
});
let schema;
if (isFederation) {
const { buildSubgraphSchema } = require2("@apollo/subgraph");
schema = buildSubgraphSchema({ typeDefs });
} else
schema = project.loadSchemaSync(project.schema, "GraphQLSchema", opts);
if (debug.enabled) {
debug("Schema loaded: %o", schema instanceof GraphQLSchema);
const schemaPaths = fg.sync(project.schema, { absolute: !0 });
debug("Schema pointers %O", schemaPaths);
}
return schemaCache.set(schemaKey, schema), schema;
}
export {
getSchema
};