/moesif-express-graphql-example

An example of using Moesif with Express GraphQL server. https://www.moesif.com

Primary LanguageJavaScript

Moesif Express-GraphQL Example

Moesif is an API Analytics and monitoring platform for GraphQL.

This example builds upon the get started example from the official guide on GraphQL's website to demonstrate how you can set up Moesif quickly using the moesif express middleware.

Summary of key changes

Express-GraphQL server works with any express app. When you set up Express-GraphQL server with express, you can simply add the moesif-express middleware. Below summarizes the code needed. See index.js from this package for details.

const express = require('express');
const graphqlHTTP = require('express-graphql');
const { buildSchema } = require('graphql');
const moesifExpress = require('moesif-express');
const bodyParser = require('body-parser-graphql');

// ...


const app = express();

const moesifOptions = {
  applicationId: 'Your Moesif Application Id'
};

const moesifMiddleware = moesifExpress(moesifOptions);

// this adds moesif middleware to express app.
app.use(moesifMiddleware);

// body-parser that supports the application/graphql type
app.use(bodyParser.bodyParserGraphQL())

// this adds graphql server to the express app.
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));

const port = 6868;
app.listen({ port }, () => {
  console.log(`Running a GraphQL API server at http://localhost:${port}/graphql`);
})

Your Moesif Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.

You can always find your Moesif Application Id at any time by logging into the Moesif Portal, click on the top right menu, and then clicking Installation.

To run this example:

  • open up index.js and replace application id with the application id obtained from Moesif.
  • run npm install to install all dependencies.
  • start the app node index.js
  • use browser to navigate to http://localhost:6868/graphql
  • run some test queries, for example:
query {
  books { title }
}

You should see the GraphQL request captured in your Event Stream, it is where it will show up first.

Notes

  • You'll notice other requests besides GraphQL are captured also. That is ok and expected, since moesif-express works at a lower level than GraphQL. You can change this default by modify the skip option of moesif-express.
  • If you are using Express-GraphQL to create a wrapper around other restful APIs, you may consider capture all outgoing API calls so you get the big pictures of how everything is connected:
moesifMiddleware.startCaptureOutgoing();