May 23, 2017

Apollo Optics is now free for small projects

Danielle Man

Danielle Man

Update: There is a new version of Optics! Apollo Engine has everything Optics does plus error tracking, query caching, and more. Engine is free for 1 million requests a month and it’s easy to switch from Optics to Engine using our migration guide.

We want Optics to be something every GraphQL developer can use to understand how their server works, even if it’s still in development. So today we’re excited to announce that we’ve added a completely free tier up to 10,000 requests a month. Now, you can instrument your API and see what it’s doing without worrying about running out of time in a free trial!

Developers from companies like Expo and Product Hunt have said that they like Optics because it provides a fine-grained view into the execution of their GraphQL queries. Last weekend at GraphQL Europe, Optics got mentions in several talks, including in Brooks’ talk about launching GitHub’s public GraphQL API.

Optics lets you see in detail how each query is resolved on your server, and shows aggregate statistics of how your schema fields are being used.

The Optics dashboard

If you haven’t heard about Apollo Optics yet, and want to learn more about what it does and why you might want a GraphQL-specific server performance tool, check out our landing page!

Optics quick start

If you have a GraphQL server, it only takes a few minutes to instrument your schema and see your data in Optics. The quick directions below are for instrumenting a Node.js Express server. If you’re using a different server technology, check out the docs for JavaScript and Ruby. An implementation for Sangria (Scala) is coming soon as well!

1. Install

Simple enough:

npm install optics-agent --save

2. Import

In the file which connects your GraphQL schema to your Express server, add an import at the top:

import OpticsAgent from 'optics-agent';

3. Instrument the schema, middleware and context

The Optics agent relies on a variety of data points, which are available at different stages in the execution cycle. Currently, you need to add code in three places.

First, wrap your GraphQL Schema object with instrumentSchema:

// Before: graphqlExpress({ schema: schema })
graphqlExpress((req) => {
  // other options...
  schema: OpticsAgent.instrumentSchema(schema)
})

Second, add the HTTP middleware before your GraphQL middleware:

expressServer.use(OpticsAgent.middleware());

Third, add an opticsContext field to your GraphQL context:

graphqlExpress((req) => { 
  // other options...
  context: {
    // other context fields...
    opticsContext: OpticsAgent.context(req),
  }
})

That’s all the code we have to add. You’re almost done!

4. Sign into Optics and set the API key

Go to optics.apollodata.com and create an endpoint to get an API key. Restart your server and pass your API key with an environment variable, like so:

OPTICS_API_KEY=<my_key> npm start

The agent will automatically pick it up from there. Now, open up GraphiQL, run a query, and in a few seconds you’ll see data starting to show up in your Optics dashboard. You’re all set up!


We’re really excited to make Optics available to more people, and we hope you’ll find it a valuable tool to understand how your GraphQL schema and queries work! If you have any feedback or want to chat about Optics, please join the #optics channel in the Apollo Slack!

Written by

Danielle Man

Danielle Man

Read more by Danielle Man