Run GraphQL Operations from a CLI
On this page
Atlas Device Sync, Atlas Edge Server, Data API, and HTTPS Endpoints are deprecated. Refer to the deprecation page for details.
Atlas Device Sync, Atlas Edge Server, Data API, HTTPS Endpoints, GraphQL, and Static Hosting are deprecated. Commands related to these features are also deprecated.
Overview
You can access your App's Atlas GraphQL API
through a terminal or command line interface. GraphQL operates over HTTP, so the
CLI can be a standard HTTP client, like curl
, or a specialized GraphQL CLI,
like graphqurl.
To send GraphQL requests to your app, you'll need the following:
Your App ID.
A valid user access token. For details on how to get an access token, see Authenticate GraphQL Requests.
Run a Query
gq https://services.cloud.mongodb.com/api/client/v2.0/app/<Your App ID>/graphql \ -H 'Authorization: Bearer <Valid Access Token>' \ -q 'query AllMoviesFromYear($year: Int!) { movies(query: { year: $year }) { title year runtime } }' \ -v 'year=2000'
curl https://services.cloud.mongodb.com/api/client/v2.0/app/<Your App ID>/graphql \ -X POST \ -H 'Authorization: Bearer <Valid Access Token>' \ -d '{ "query": "query AllMoviesFromYear($year: Int!) { movies(query: { year: $year }) { title year runtime } }", "variables": { "year": 2000 } }'
Run a Mutation
gq https://services.cloud.mongodb.com/api/client/v2.0/app/<Your App ID>/graphql \ -H 'Authorization: Bearer <Valid Access Token>' \ -q 'mutation UpdateMovieTitle($oldTitle: String!, $newTitle: String!) { updateOneMovie(query: { title: $oldTitle } set: { title: $newTitle }) { title year } }' -v 'oldTitle=The Matrix Reloaded' -v 'newTitle=The Matrix 2'
curl https://services.cloud.mongodb.com/api/client/v2.0/app/<Your App ID>/graphql \ -X POST \ -H 'Authorization: Bearer <Valid Access Token>' \ -d '{ "query": "mutation UpdateMovieTitle($oldTitle: String!, $newTitle: String!) { updateOneMovie(query: { title: $oldTitle } set: { title: $newTitle }) { title year } }", "variables": { "oldTitle": "The Matrix Reloaded", "newTitle": "The Matrix 2" } }'