GraphQL API (Beta)¶

暴英达
2023-12-01

MongoDB Logo
ServerDriversCloudToolsGuides
Get MongoDB
Close ×
MongoDB Stitch

Introduction
Tutorials
Users & Authentication
MongoDB Atlas
GraphQL
    GraphQL API Overview
    Expose Data in a Collection
    Authenticate GraphQL Requests
    Connect from a Client Application
    Execute a GraphQL Operation
    Define a Custom Query or Mutation
    GraphQL Types, Resolvers, and Operators
MongoDB Mobile
Functions
Triggers
External Services
Values & Secrets
Application Deployment
Hosting
Troubleshooting
Stitch Administration
Application Logs
Client SDKs
Release Notes

Stitch > GraphQL 

GraphQL API (Beta)

On this page

Overview
Why GraphQL?
GraphQL Operations
    Queries
    Mutations

Overview

Version Requirement

GraphQL requires MongoDB version 4.0 or greater.

The Stitch GraphQL API (Beta Release) allows client applications to access data stored in a linked MongoDB cluster using any standard GraphQL client. Stitch automatically creates GraphQL types for every linked collection that has a defined document schema and evaluates role-based permissions for all GraphQL requests.

Create an Atlas Cluster for Free

The GraphQL API lets you access data that you have stored in a MongoDB Atlas cluster. To get started, create a free cluster and link it to your Stitch application.

If you don’t have any data yet but you still want to explore the GraphQL API, consider adding a sample data set to your cluster.

To learn how to make data available through the GraphQL API, see Expose Data in a Collection.

To learn how to connect to the GraphQL API from a client application, see Connect from a Client Application.

To learn how to run queries and mutations from your preferred client, see Execute a GraphQL Operation.

To learn how to define custom queries and mutations, see Define a Custom Query or Mutation.

To learn about the types and operations that you can use with the Stitch GraphQL API, see GraphQL Types & Resolvers.

Temporary GraphQL Limitations

The Stitch GraphQL API is in a public beta and does not currently support GraphQL subscriptions. We plan to include support for subscriptions and other GraphQL features in future releases.
Why GraphQL?

GraphQL is a declarative, strongly-typed query language for client applications. Clients define the exact data shape and contents that they need in a single request which eliminates overfetching problems and circumvents the need for multiple costly round trips to the server.

To learn more about GraphQL, check out the official GraphQL tutorial.
GraphQL Operations

Stitch automatically generates types and resolvers for data that you expose to the GraphQL API. The generated types and operations are all named after the base type name for each exposed collection. If you don’t define a type name, Stitch uses the collection name instead.

For more information on how to expose a collection and name its data type, see Expose Data in a Collection.
Queries

A GraphQL query is a read operation that requests specific fields from one or more types. Stitch automatically generates query types for documents in each collection that has a defined schema.

For more information and examples, including a list of all automatically generated query types, see Query Resolvers.

For an example of how to run a query from a client application, see Run a GraphQL Query.

Find a single movie by name

query {
movie(query: { title: “The Matrix” }) {
_id
title
year
runtime
}
}

Find all movies from the year 2000

query {
movies(query: { year: 2000 }) {
_id
title
year
runtime
}
}

Find the ten longest movies from the year 2000

query {
movies(
query: { year: 2000 }
sortBy: RUNTIME_DESC
limit: 10
) {
_id
title
year
runtime
}
}

Mutations

A GraphQL mutation is a write operation that creates, modifies, or deletes one or more documents. Stitch automatically generates mutation types for documents in each collection that has a defined schema. Stitch uses transactions to ensure safe writes via mutations.

For more information and examples, including a list of all automatically generated mutation types, see Mutation Resolvers.

For an example of how to run a mutation from a client application, see Run a GraphQL Mutation.

Insert a new movie

mutation {
insertOneMovie(data: {
title: “Little Women”
director: “Greta Gerwig”
year: 2019
runtime: 135
}) {
_id
title
}
}

Update the year of a movie

mutation {
updateOneMovie(
query: { title: “The Matrix” }
set: { year: 1999 }
) {
_id
title
}
}

Delete a movie

mutation {
deleteOneMovie(query: { title: “The Room” }) {
_id
title
}
}

Delete multiple movies

mutation {
deleteManyMovies(query: { director: “Tommy Wiseau” }) {
_id
title
}
}

← CRUD & Aggregation APIs Expose Data in a Collection →

© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
Was this page helpful?
Yes
No

 类似资料: