@foal/graphiql
Version:
GraphiQL integration for FoalTS
93 lines (92 loc) • 4 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphiQLController = void 0;
// std
const fs_1 = require("fs");
const promises_1 = require("node:fs/promises");
const path_1 = require("path");
// 3p
const core_1 = require("@foal/core");
/**
* Controller to render a GraphiQL page.
*
* @export
* @class GraphiQLController
*/
class GraphiQLController {
options = {};
apiEndpoint = '/graphql';
cssThemeURL;
async index(ctx) {
if (!ctx.request.path.endsWith('/')) {
return new core_1.HttpResponseMovedPermanently(ctx.request.path + '/');
}
const template = await (0, promises_1.readFile)((0, path_1.join)(__dirname, 'templates/index.html'), 'utf8');
const page = (0, core_1.renderToString)(template, {
options: JSON.stringify(this.options),
endpoint: this.apiEndpoint,
cssTheme: this.cssThemeURL ? `\n<link rel="stylesheet" href="${this.cssThemeURL}" />` : ''
});
return new core_1.HttpResponseOK(page);
}
getReactProduction() {
return this.createHttpResponseFile('react.production.min.js', 'application/javascript');
}
getReactDomProduction() {
return this.createHttpResponseFile('react-dom.production.min.js', 'application/javascript');
}
getGraphiqlCss() {
return this.createHttpResponseFile('graphiql.min.css', 'text/css');
}
getGraphiqlJs() {
return this.createHttpResponseFile('graphiql.min.js', 'application/javascript');
}
async createHttpResponseFile(filename, contentType) {
const filePath = (0, path_1.join)(__dirname, 'static', filename);
const stream = (0, fs_1.createReadStream)(filePath);
const stats = await (0, promises_1.stat)(filePath);
return new core_1.HttpResponseOK(stream, { stream: true })
.setHeader('Content-Type', contentType)
.setHeader('Content-Length', stats.size.toString());
}
}
exports.GraphiQLController = GraphiQLController;
__decorate([
(0, core_1.Get)('/'),
__metadata("design:type", Function),
__metadata("design:paramtypes", [core_1.Context]),
__metadata("design:returntype", Promise)
], GraphiQLController.prototype, "index", null);
__decorate([
(0, core_1.Get)('/react.production.min.js'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], GraphiQLController.prototype, "getReactProduction", null);
__decorate([
(0, core_1.Get)('/react-dom.production.min.js'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], GraphiQLController.prototype, "getReactDomProduction", null);
__decorate([
(0, core_1.Get)('/graphiql.min.css'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], GraphiQLController.prototype, "getGraphiqlCss", null);
__decorate([
(0, core_1.Get)('/graphiql.min.js'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], GraphiQLController.prototype, "getGraphiqlJs", null);