w3resource

Deploy GraphQL API with Apollo Graph Manager


It has been really great that we made it this far. Great job! We have already learned how to build a GraphQL API with Apollo. How to connect it to REST and SQL data sources, and then, how to send GraphQL queries. Now we are going to graph deployed.

Our Apollo GraphQL API project can be deployed to any of the cloud services available, such as Heroku, AWS Lambda, or Netlify. To do this, we need to have an Apollo Graph Manager account. You can sign up using this link, if you don't have an account yet.

Publishing your schema to Graph Manager

Before we can have our graph deployed, we need to publish our schema to the Apollo Graph Manager Cloud service. If you are familiar with Node JS, you must have heard about npm.

Just like npm is a registry for JavaScript packages, Apollo Graph Manager contains a schema registry that makes it simple to pull the most recent schema from the cloud. We will be publishing our schema from the CLI in this example. But in a production application, you should set up this publishing script as part of your CI workflow.

Getting a Graph Manager API key

First, we need an Apollo Graph Manager API key. To get this, navigate to Apollo Graph Manager, login, and click on New Graph menu. A prompt will instruct you to name your graph. Click Create Graph. You'll see your API key prefixed by service:. Copy that key so we can save it as an environment variable.

Let's save our key as an environment variable. It is always important not to commit our our Graph Manager API key into version control.

Make a copy of the .env.example file located in server/ and call it .env. Add the Graph Manager API key that you copied from the previous step to the file as shown below

//.env
ENGINE_API_KEY=service:<your-service-name>:<hash-from-apollo-engine>

Your entry should look like this:

ENGINE_API_KEY=service:my-service-439:E4VSTiXeFWaSSBgFWXOiSA

Our key is now stored under the environment variable ENGINE_API_KEY.

Check and publish with the Apollo CLI

It's time to publish our schema to Graph Manager. To do this, start your server in one terminal window by running npm start command. In another terminal window, run:

npx apollo service:push --endpoint=http://localhost:4000

npx is a tool bundled with npm for easily running packages that are not installed globally.

This command publishes your schema to the Apollo registry. Once your schema is uploaded, you should be able to see your schema in the Apollo Graph Manager explorer.

For subsequent publishes, we may first want to check for any breaking changes in our new schema against the old version. We do this by running:

npx apollo service:check --endpoint=http://localhost:4000

Why publish my schema on Graph Manager?

Publishing our schema to Apollo Graph Manager enables us to utilize many features necessary for running a graph API in production. Some of these features include:

  • Schema explorer: This Graph Manager's powerful schema registry, allows you to quickly explore all the types and fields in your schema with usage statistics on each field. This metric gives you insight on the cost of a field. And also, if a certain field in so much demand?
  • Schema history: This amazing feature allows developers to confidently iterate a graph's schema by validating the new schema against field-level usage data from the previous schema. This empowers developers to avoid breaking changes by providing insights into which clients will be broken by a new schema.
  • Performance analytics: Fine-grained insights into every field, resolvers and operations of your graph's execution
  • Client awareness: Report client identity (name and version) to your server for insights on client activity.

Some of the amazing features of GraphQL which we just described are only available on paid plans. So, to utilize the all the amazing GraphQL has to offer, you can always migrate to a paid plan. But for the sake of this tutorial, a free plan is enough for us.

Previous: Implementing Paginated Queries in GraphQL with Apollo Server.
Next: Connect GraphQL API to a React Client with Apollo.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/apollo-graphql/run-your-graph-in-production.php