ra-data-graphql
Version:
A GraphQL data provider for react-admin
85 lines • 3.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isResourceExcluded = exports.isResourceIncluded = exports.introspectSchema = void 0;
const graphql_1 = require("graphql");
const client_1 = require("@apollo/client");
const constants_1 = require("./constants.cjs");
/**
* @param {ApolloClient} client The Apollo client
* @param {Object} options The introspection options
*/
const introspectSchema = async (client, options) => {
const schema = options.schema ? options.schema : await fetchSchema(client);
const queries = getQueriesFromSchema(schema);
const types = getTypesFromSchema(schema);
const resources = getResources(types, queries, options);
return {
types,
queries,
resources,
schema,
};
};
exports.introspectSchema = introspectSchema;
const fetchSchema = (client) => client
.query({
fetchPolicy: 'network-only',
query: (0, client_1.gql) `
${(0, graphql_1.getIntrospectionQuery)()}
`,
})
.then(({ data: { __schema } }) => __schema);
const getQueriesFromSchema = (schema) => schema.types.reduce((acc, type) => {
if (type.name !== schema.queryType?.name &&
type.name !== schema.mutationType?.name &&
type.fields) {
return acc;
}
return [...acc, ...(type.fields || [])];
}, []);
const getTypesFromSchema = (schema) => schema.types.filter(type => type.name !== (schema.queryType && schema.queryType.name) &&
type.name !== (schema.mutationType && schema.mutationType.name));
const getResources = (types, queries, options) => {
const filteredResources = types.filter(type => isResource(type, queries, options));
return filteredResources.map(type => buildResource(type, queries, options));
};
const isResource = (type, queries, options) => {
if ((0, exports.isResourceIncluded)(type, options))
return true;
if ((0, exports.isResourceExcluded)(type, options))
return false;
const operations = Object.keys(options.operationNames).map(operation => options.operationNames[operation](type));
const hasAtLeastOneOperation = operations.some(operation => queries.find(({ name }) => name === operation));
return hasAtLeastOneOperation;
};
const isResourceIncluded = (type, { include } = {}) => {
if (Array.isArray(include)) {
return include.includes(type.name);
}
if (typeof include === 'function') {
return include(type);
}
return false;
};
exports.isResourceIncluded = isResourceIncluded;
const isResourceExcluded = (type, { exclude } = {}) => {
if (Array.isArray(exclude)) {
return exclude.includes(type.name);
}
if (typeof exclude === 'function') {
return exclude(type);
}
return false;
};
exports.isResourceExcluded = isResourceExcluded;
const buildResource = (type, queries, options) => constants_1.ALL_TYPES.reduce((acc, raFetchMethod) => {
const query = queries.find(({ name }) => options.operationNames[raFetchMethod] &&
name === options.operationNames[raFetchMethod](type));
if (!query)
return acc;
return {
...acc,
[raFetchMethod]: query,
};
}, { type });
//# sourceMappingURL=introspection.js.map