Navigation

Run GraphQL Operations from a CLI

Overview

You can access your app’s exposed 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. For details on how to find this, see Find 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://realm.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://realm.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://realm.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://realm.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" } }'