Tutorial: Adding Custom App Modifications

Adding Custom App Modifications

From time to time, it may be useful for you (the developer) to add custom functionality to Flitter during initialization. This may be custom Express add-ons, tweaking settings of the underlying Express app, or any number of other things. As such, Flitter comes with a MiscUnit for these customizations. This unit in the file app/MiscUnit.js and follows the exact same structure as every other unit. It is loaded after the core Flitter units, but before any secondary or custom units. See libflitter/Unit for more information on how to add functionality to the unit.

Example: Adding the express-graphql Middleware

As an example, we'll configure the underlying Express app to use the express-graphql package on the route /graphql. To do this, modify the app/MiscUnit.js file:

const Unit = require('libflitter/Unit')

class MiscUnit extends Unit {
    go(app, context){
          // Get the GraphQL package
        const graphqlHTTP = require('express-graphql')

        // Tell the underlying express middleware to use it
        app.express.use('/graphql', graphqlHTTP({
            schema: MyGraphQLSchema,
            graphiql: true
        }))
    }
}

module.exports = exports = MiscUnit

Here, we modify the go() method. This method is passed an instance of the FlitterApp application, which we can then use to access the Express server instance like normal.