@omnigraph/sqlite
Version:
50 lines (49 loc) • 2.03 kB
JavaScript
import { path, process } from '@graphql-mesh/cross-helpers';
import { createDefaultExecutor } from '@graphql-tools/delegate';
export default function loadGraphQLSchemaFromSQLite(_name, opts) {
return loadGraphQLSchemaFromOptions(opts);
}
export function loadGraphQLSchemaFromOptions(opts) {
const tuql$ = import('tuql').catch(e => {
throw new Error('You need to install `tuql` package in order to use SQLite data source.');
});
const cwd = opts.cwd || process.cwd();
if (opts.infile != null) {
const absolutePath = path.isAbsolute(opts.infile) ? opts.infile : path.join(cwd, opts.infile);
return tuql$.then(tuql => tuql.buildSchemaFromInfile(absolutePath));
}
if (opts.db != null) {
const absolutePath = path.isAbsolute(opts.db) ? opts.db : path.join(cwd, opts.db);
return tuql$.then(tuql => tuql.buildSchemaFromDatabase(absolutePath));
}
throw new Error('Invalid options');
}
export function loadSQLiteSubgraph(name, opts) {
return ({ cwd }) => ({
name,
schema$: loadGraphQLSchemaFromOptions(opts).then(schema => {
const extensionsObj = (schema.extensions = schema.extensions || {});
extensionsObj.directives ||= {};
extensionsObj.directives.transport = {
kind: 'sqlite',
subgraph: name,
location: opts.infile || opts.db,
options: {
type: opts.infile != null ? 'infile' : 'db',
},
};
return schema;
}),
});
}
export function getSubgraphExecutor(transportContext) {
const loaderOpts = {};
if (transportContext.transportEntry.options.type === 'infile') {
loaderOpts.infile = transportContext.transportEntry.location;
}
else {
loaderOpts.db = transportContext.transportEntry.location;
}
loaderOpts.cwd = transportContext.cwd;
return loadGraphQLSchemaFromOptions(loaderOpts).then(schema => createDefaultExecutor(schema));
}