Execute a GraphQL Operation¶
Important
The MongoDB Stitch SDK is deprecated. For guidance on migrating to MongoDB Realm, please see Migrate Your App from Stitch to Realm.
On this page
Overview¶
You can access data that you’ve exposed from a collection through the GraphQL API. Stitch enforces collection rules for all GraphQL operations, so you need to authenticate each request.
Set Up¶
- Apollo (React)
- graphqurl (CLI)
You can use Apollo’s React hooks to execute GraphQL operations. The hooks are available in the @apollo/react-hooks package and all require the graphql-tag package to define operations.
Important
To execute a GraphQL operation with Apollo, you first need to connect your client application.
To install the required packages, run the following:
You can use the graphqurl CLI to execute GraphQL operations from a command line. You must authenticate all requests, so make sure to get a valid access token.
To install the graphqurl
CLI, run the following:
Run a Query¶
GraphQL Queries
The GraphQL API supports several different types of queries. For detailed information on all supported query types, including examples, see query resolvers.
- Apollo (React)
- graphqurl (CLI)
Define the query with the qgl
tag and then use the
useQuery() hook
to run the query whenever the component mounts.
Run a Mutation¶
GraphQL Mutations
The GraphQL API supports several different types of mutations. For detailed information on all supported mutation types, including examples, see mutation resolvers.
- Apollo (React)
- graphqurl (CLI)
Define the mutation with the qgl
tag and then use the
useMutation()
hook to get a function that executes the mutation.