adminjs-graphql
Version:
adminjs GraphQL adapter
218 lines • 8.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.configureResource = exports.buildResource = void 0;
/**
* Builds a `GraphQLResource` from pieces.
*/
function buildResource(pieces) {
var _a, _b, _c, _d, _e, _f;
const IDField = pieces.ID || "ID";
const singular = pieces.singular || pieces.type[0].toLowerCase() + pieces.type.slice(1);
const plural = pieces.plural || singular + "s";
const fragmentString = (_b = (typeof pieces.fragment === "string"
? pieces.fragment
: (_a = pieces.fragment.loc) === null || _a === void 0 ? void 0 : _a.source.body)) === null || _b === void 0 ? void 0 : _b.trim();
const queryDirectives = (_c = pieces.queryDirectives) !== null && _c !== void 0 ? _c : "";
const inputType = pieces.inputType || `${pieces.type}Input`;
const upperTail = singular[0].toUpperCase() + singular.slice(1);
const createMutation = ((_d = pieces.mutations) === null || _d === void 0 ? void 0 : _d.create) || "create" + upperTail;
const updateMutation = ((_e = pieces.mutations) === null || _e === void 0 ? void 0 : _e.update) || "update" + upperTail;
const deleteMutation = ((_f = pieces.mutations) === null || _f === void 0 ? void 0 : _f.delete) || "delete" + upperTail;
const mapInputValue = (v) => {
var _a;
return {
...Object.keys(v).reduce((value, key) => {
var _a, _b;
value[(_b = (_a = pieces.inputFieldMap) === null || _a === void 0 ? void 0 : _a[key]) !== null && _b !== void 0 ? _b : key] = v[key];
return value;
}, {}),
...(_a = pieces.mapInputValue) === null || _a === void 0 ? void 0 : _a.call(pieces, v),
};
};
if (!fragmentString) {
throw new Error("Unexpected empty fragment");
}
if (!fragmentString.startsWith("{") || !fragmentString.endsWith("}")) {
throw new Error("Fragment must be specified within curly brackets");
}
return {
id: pieces.type,
count: (filter) => {
var _a;
return ({
query: `
query($filter: [FilterInput!]) ${queryDirectives} {
count: ${((_a = pieces.queries) === null || _a === void 0 ? void 0 : _a.count) || `${singular}Count`}(filter: $filter)
}`,
variables: {
filter: filter.map((entry) => {
var _a, _b;
return ({
...entry,
field: (_b = (_a = pieces.inputFieldMap) === null || _a === void 0 ? void 0 : _a[entry.field]) !== null && _b !== void 0 ? _b : entry.field,
});
}),
},
parseResult(response) {
return response.count;
},
});
},
find: (filter, options) => {
var _a;
return ({
query: `
query($filter: [FilterInput!], $sorting: SortingInput, $offset: Int, $limit: Int) ${queryDirectives} {
q: ${((_a = pieces.queries) === null || _a === void 0 ? void 0 : _a.find) || plural}(filter: $filter, sorting: $sorting, offset: $offset, limit:$limit) {
...fields
}
}
fragment fields on ${pieces.type} ${fragmentString} `,
variables: (() => {
var _a, _b, _c;
const sortField = (_a = options.sort) === null || _a === void 0 ? void 0 : _a.sortBy;
const sortOrder = (_c = (_b = options.sort) === null || _b === void 0 ? void 0 : _b.direction) === null || _c === void 0 ? void 0 : _c.toUpperCase();
const sorting = sortField
? {
sorting: {
by: sortField,
order: sortOrder !== null && sortOrder !== void 0 ? sortOrder : "ASC",
},
}
: undefined;
const offset = options.offset
? {
offset: options.offset,
}
: undefined;
const limit = options.limit
? {
limit: options.limit,
}
: undefined;
return {
filter: filter.map((entry) => {
var _a, _b;
return ({
...entry,
field: (_b = (_a = pieces.inputFieldMap) === null || _a === void 0 ? void 0 : _a[entry.field]) !== null && _b !== void 0 ? _b : entry.field,
});
}),
...sorting,
...offset,
...limit,
};
})(),
parseResult(response) {
return response.q;
},
});
},
findOne: (ID) => {
var _a;
return ({
query: `
query($ID: ID!) ${queryDirectives} {
q: ${((_a = pieces.queries) === null || _a === void 0 ? void 0 : _a.get) || singular} (${IDField}: $ID) {
...fields
}
}
fragment fields on ${pieces.type} ${fragmentString} `,
variables: {
ID,
},
parseResult(response) {
return response.q;
},
});
},
create: (entity) => ({
query: `
mutation($input: ${inputType}!) ${queryDirectives} {
m: ${createMutation} (input: $input) {
...fields
}
}
fragment fields on ${pieces.type} ${fragmentString} `,
variables: {
input: mapInputValue(entity),
},
parseResult(response) {
return response.m;
},
}),
update: (ID, entity) => ({
query: `
mutation($ID: ID!, $update: ${inputType}!) ${queryDirectives} {
m: ${updateMutation} (${IDField}: $ID, update: $update) {
...fields
}
}
fragment fields on ${pieces.type} ${fragmentString} `,
variables: {
ID,
update: bodyOf(mapInputValue(entity)),
},
parseResult(response) {
return response.m;
},
}),
delete: (ID) => ({
query: `
mutation($ID: ID!) ${queryDirectives} {
m: ${deleteMutation} (${IDField}: $ID)
}
`,
variables: {
ID,
},
parseResult() {
//
},
}),
};
}
exports.buildResource = buildResource;
/**
* Extends `buildResource` with configuration options to
* build a fully configured resource.
*
* This keeps GraphQL resource definition and AdminBro
* configuration of the resource in one place.
*/
function configureResource(options) {
const resource = {
...buildResource({
...options.pieces,
type: options.type,
}),
...options.extras,
};
const configuration = (connection) => {
var _a;
return ({
resource: connection.r[options.type],
options: (_a = options.options) !== null && _a !== void 0 ? _a : {},
features: options.features,
});
};
const translations = options.resourceTranslations
? {
[options.type]: options.resourceTranslations,
}
: undefined;
return {
resource,
configuration,
translations,
};
}
exports.configureResource = configureResource;
function bodyOf(entity) {
const body = {
...entity,
};
delete body.ID;
return body;
}
//# sourceMappingURL=builder.js.map