Skip to content

feat/AB#91885_Store-GraphQL-schema-in-public-blob-storage-file #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ RABBITMQ_PORT=
# MODULES
FRONT_OFFICE_URI=
BACK_OFFICE_URI=

# PUBLIC BLOB STORAGE
PUBLIC_FILE_NAME=
3 changes: 3 additions & 0 deletions config/custom-environment-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ module.exports = {
clientId: 'MICROSOFT_GRAPH_CLIENT_ID',
clientSecret: 'MICROSOFT_GRAPH_CLIENT_SECRET',
},
public: {
fileName: 'PUBLIC_FILE_NAME',
},
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import config from 'config';
import { logger } from './services/logger.service';
import { checkConfig } from '@utils/server/checkConfig.util';
import buildSchema from '@utils/schema/buildSchema';
import { uploadSchemaFile } from '@utils/files/uploadSchemaFile';

// Needed for survey.model, as xmlhttprequest is not defined in servers
global.XMLHttpRequest = require('xhr2');
Expand Down Expand Up @@ -51,4 +52,5 @@ mongoose.connection.once('open', () => {
// subscriberSafe();
pullJobScheduler();
customNotificationScheduler();
uploadSchemaFile();
});
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import permissions from './permissions';
import roles from './roles';
import gis from './gis';
import style from './style';
import schema from './schema';
import config from 'config';

/** Express router instance */
Expand All @@ -29,5 +30,6 @@ router.use('/summarycards', summarycards);
router.use('/roles', roles);
router.use('/gis', gis);
router.use('/style', style);
router.use('/schema', schema);

export { router };
22 changes: 22 additions & 0 deletions src/routes/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import express from 'express';
import config from 'config';
import { logger } from '@services/logger.service';

/** Routes for schema. */
const router = express.Router();

/** Return complete GraphQL schema URL public endpoint */
router.get('/url', async (req: any, res) => {
try {
// Current timestamp
const timestamp = Date.now();
const storageEndpoint = config.get('public.fileName');
const schemaUrl = `${storageEndpoint}/introspection/schema.graphql?${timestamp}`;
return res.status(200).send({ url: schemaUrl });
} catch (err) {
logger.error(err.message, { stack: err.stack });
return res.status(500).send(req.t('common.errors.internalServerError'));
}
});

export default router;
73 changes: 73 additions & 0 deletions src/utils/files/uploadSchemaFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fs from 'fs';
import config from 'config';
import i18next from 'i18next';
import { logger } from '@services/logger.service';
import { BlobServiceClient } from '@azure/storage-blob';
import { printSchema } from 'graphql';
import buildSchema from '@utils/schema/buildSchema';

/** Storage connection string */
const STORAGE_CONNECTION_STRING: string = config.get('public.fileName');
// const STORAGE_CONNECTION_STRING: string = config.get(
// 'blobStorage.connectionString'
// );

/**
* Generate and save GraphQL schema file.
*/
const generateAndSaveSchema = async () => {
// GraphQL schema object
const schema = await buildSchema();
// Print schema to string
const schemaString = printSchema(schema);
// Save schema to file and return it
fs.writeFileSync('schema.graphql', schemaString);
return fs.readFileSync('schema.graphql');
};

/**
* Upload GraphQL schema file in public storage.
*
* @param containerName main container name
* @param blobName blob name
* @param file GraphQL schema file
*/
const uploadFile = async (
containerName: string,
blobName: string,
file: any
) => {
try {
const blobServiceClient = BlobServiceClient.fromConnectionString(
STORAGE_CONNECTION_STRING
);
const containerClient = blobServiceClient.getContainerClient(containerName);
if (!(await containerClient.exists())) {
await containerClient.create();
}
const blockBlobClient = containerClient.getBlockBlobClient(blobName);

await blockBlobClient.uploadFile(file);
} catch (err) {
logger.error(err.message, { stack: err.stack });
// NO WORKING, always cath error
console.log('err: ', err);
throw new Error(
i18next.t('utils.files.uploadFile.errors.fileCannotBeUploaded')
);
}
};

/**
* Generates, save and upload in a public storage the GraphQL schema file.
*/
export const uploadSchemaFile = async () => {
try {
// Call function to generate and save schema
const file = await generateAndSaveSchema();
// Upload file
await uploadFile('public', 'introspection/schema.graphql', file);
} catch (err) {
logger.error(err.message);
}
};
Loading