JavaScript Metrics Plugins for GraphQL Servers

All of the plugins for JavaScript-based servers take a fetch function as an argument, this enables using these plugins in any JavaScript environment. If you are running NodeJS you can use the node-fetch package, and in an edge environment (e.g. Cloudflare Workers) pass the global fetch function.

Before you Begin*

Before you start, read the About GraphQL Metrics and Get Started with GraphQL Metrics topics for preliminary instructions on setting up metrics logging requests through the Stellate Dashboard. The following sections provide information about the server plugins that you can use for collecting and sending metrics.

Plugin for apollo-server

To install and use the plugin for apollo-server, use the following code.

import { createStellateLoggerPlugin } from 'stellate/apollo-server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { ApolloServer } from '@apollo/server'

const stellatePlugin = createStellateLoggerPlugin({
  serviceName: '${serviceName}',
  token: '${loggingToken}',
  // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
  // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
  fetch: fetch,
})

const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [stellatePlugin],
})

const { url } = await startStandaloneServer(server, { listen: { port: 4000 } })

Plugin for graphql-yoga

import { createStellateLoggerPlugin } from 'stellate/graphql-yoga'
import { createServer } from 'node:http'
import { createYoga, createSchema } from 'graphql-yoga'

const stellatePlugin = createStellateLoggerPlugin({
  serviceName: '${serviceName}',
  token: '${loggingToken}',
  // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
  // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
  fetch: fetch,
})

const yoga = createYoga({ schema, plugins: [stellatePlugin] })
const server = createServer(yoga)
server.listen(4000)

Plugin for GraphQL Mesh

Install @envelop/core and stellate:

$ npm install @envelop/core stellate

Include it in your plugins file:

// envelopPlugins.ts
import { createStellateLoggerPlugin } from 'stellate/envelop'
import { MeshPlugin } from '@graphql-mesh/types'

const plugins: MeshPlugin = [
  createStellateLoggerPlugin({
    serviceName: '${serviceName}',
    token: '${loggingToken}',
    // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
    // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
    fetch: fetch,
  }),
]

// reference this file using 'additionalEnvelopPlugins' in your .meshrc.yaml config file
export default plugins

Plugin for GraphQL Envelop

Can be used with any envelop compatible plugin system, such as GraphQL Mesh, etc. Start by installing @envelop/core and stellate:

$ npm install @envelop/core stellate

Then add it to your envelop plugins:

import { createStellateLoggerPlugin } from 'stellate/envelop'
import { envelop } from '@envelop/core'

export const getEnveloped = envelop({
  plugins: [
    // ... other plugins
    createStellateLoggerPlugin({
      serviceName: '${serviceName}',
      token: '${loggingToken}',
      // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
      // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
      fetch: fetch,
    }),
  ],
})

server.on('req', async (req, res) => {
  // The request entry is important here so the stellage plugin has the request context
  getEnveloped({ request: req })
})

Discover more

About GraphQL Metrics

Get Started with GraphQL Metrics

Use the Edge Proxy for GraphQL Metrics

Build a Metrics Manual Call