Phil Prasek

1 result

Building a Modern App Stack with Apollo GraphQL and MongoDB Atlas

Delivering new app experiences with legacy architectures is slow and painful. Many organizations invest massive amounts of resources to make their infrastructure more resilient and flexible yet find they’re still not delivering products at the speed they seek. API complexity means that, rather than delivering new experiences, frontend and backend teams must navigate scattered microservices, versioned REST endpoints, and complex database management. This article explains how teams can reduce complexity through the use of Apollo GraphQL and MongoDB Atlas . GraphQL can help teams integrate these scattered REST APIs and microservices into a unified schema that frontend developers can query, fetching only the data required to power an experience while being agnostic to where the data is sourced from. However, running everything through a single GraphQL server (read: monolith) with multiple teams rapidly contributing changes creates a bottleneck. The complexity of the API layer grows exponentially as the number of client devices, applications, and developers increases — and backend teams can no longer work autonomously or push changes on their own releases schedules. To be efficient with GraphQL, developers need: A unified API, so app developers can rapidly create new experiences A modular API layer, so each team can independently own their slice of the graph A seamless, high-performance data layer that scales alongside API consumption An app stack that delivers A supergraph is a GraphQL API designed to benefit frontend and backend teams simultaneously. It’s a unified API layer built with Apollo Federation , which is a declarative, modular GraphQL architecture. Unlike a monolithic schema, a supergraph is composed of smaller graphs called subgraphs, each with their own schema. Teams can evolve their subgraphs independently, and their changes will be automatically rolled into the overall supergraph, allowing them to deliver autonomously and incrementally. However, the efficiency of a supergraph depends on the capabilities and reliability of the underlying data layer. MongoDB Atlas — MongoDB’s fully managed developer data platform — comes with that promise. It offers a flexible document model that gives developers an intuitive way to work with GraphQL’s nested data structure, while providing a reliable data layer that can run anywhere, be deployed across multiple regions and cloud providers, and scale horizontally due to its distributed nature. Together, a supergraph and MongoDB Atlas create a composable app stack that eliminates complexity and empowers teams to innovate faster than ever before. Figure 1: Simplify app architecture with a composable supergraph and unified data access layer using Apollo Federation and MongoDB Atlas App dev experience When crafting a new app experience, developers will want to browse a unified schema, create queries that fetch exactly the data needed, measure API performance, and use the API in minutes instead of dedicating days or weeks trying to find the right API to stitch into each web, Android, iOS, tablet, and watch app individually. However, when apps have to use lots of REST APIs directly, the developer experience and end-user performance suffers. According to PayPal , UI developers were spending less than one-third of their time actually building UI. The remainder of that time was spent figuring out where and how to fetch data, filtering/mapping over that data, and orchestrating API calls. With a supergraph , developers can query a single GraphQL endpoint for all the data they need and discover, consume, and optimize without having to navigate a sea of REST APIs and microservices. A key characteristic of a principled GraphQL API is an abstract, demand-oriented schema , which provides the data needed to power the customer experience and abstracts the microservices and data layer underneath. The most powerful graphs serve as a facade on top of existing microservices by abstracting the lower-level backend domain models into a curated customer experience model that provides the high-level information displayed in the UI. This experience model allows for a consistent UX across web, mobile, and wearable apps. API dev experience Backend developers want the freedom to build and evolve services and capabilities autonomously. But this is a tall order when clients are simultaneously consuming services. It’s nearly impossible to refactor without introducing breaking changes and harder still to understand what the impact of those breaking changes will be. The result is that almost any change to the API requires coordination with all the client teams. With a supergraph and a flexible data layer behind it, teams can deliver changes independently to modular subgraphs that compose into the overall supergraph. Apollo Federation’s declarative architecture and powerful directives keep teams working autonomously without breaking clients. Choosing the right graph-native data layer Building a scalable supergraph starts with choosing the right data layer to power backend services. In the past, relational databases required ORMs or manual mapping of the underlying relational format to an object/document structure that apps could use, such as JSON. An impedance mismatch between what the database provided and what client apps needed resulted in performance and maintenance issues that slowed down app development and app performance. In contrast to relational databases, MongoDB’s document model and GraphQL share a simple nested data structure, which means developers can easily use them together without having to map GraphQL to relational data and define relationships. The added composability of Apollo Federation lets developers easily federate across multiple collections or databases, between single and multi-cloud Atlas clusters running in different regions, and even between Atlas and on-premises clusters. In this way, developers gain the flexibility of MongoDB’s document model and the freedom to iterate on their GraphQL schema with safety and confidence ensured by automated schema checks . Choosing the right subgraph architecture When it comes to choosing how to connect the subgraphs to the data layer, a few options are available: Traditional subgraph (microservices plus database) In many environments, there are years’ worth of existing microservices, REST APIs, and SOA services in production. Subgraphs ( written in any of 20-plus languages and frameworks ) can be added as a new layer on top of these existing microservices and composed into an experience-driven supergraph that serves as a ViewModel backend to power new app experiences for web, mobile, and wearable devices. This is a highly effective and proven model. Graph-native subgraph (direct to MongoDB) When new subgraphs are added in greenfield environments or to add net-new capabilities, the subgraphs can be designed to talk directly with the database without microservices or REST APIs in the middle. This approach isn’t always the right answer, especially for companies that have standardized on REST or gRPC in the backend. However, it is a simpler setup that can improve performance by removing a layer. Traditional subgraph (microservices plus MongoDB Atlas) MongoDB Atlas is a fully managed, multi-cloud, multi-region data layer for traditional microservices. With options such as the official MongoDB Drivers for 16 languages, a fully managed HTTPS–based Data API, or community managed ODMs such as Mongoose, developers have a range of options to build their supergraph's data layer with Atlas. Developers get the flexibility of choosing a path that provides them with an idiomatic and familiar way to work with the database in the language and development style that they are most familiar with. MongoDB Atlas GraphQL API (hosted subgraph API) MongoDB Atlas’s GraphQL API is automatically generated based on the underlying database document schema and can be directly composed as part of a supergraph. Developers who choose this approach can reduce the amount of time spent writing custom GraphQL resolvers, as these are automatically generated by MongoDB Atlas. When the Mongo document model closely matches the query shape — a paradigm that is common within document databases such as MongoDB — the queries can be served without transformation or mapping. This setup also applies to relationships between different types of documents in different collections; thus, the generated GraphQL schema will also allow devs to query collections that other teams may own in the same graph. If developers’ desired query shape differs from the underlying document model, such as when shaping schemas in a Server Driven UI (SDUI) pattern, they can leverage the @requires schema directive to pull in and transform multiple document fields into an experience-oriented property tuned for rendering by frontend apps. In this way, devs can benefit from both efficient data access and custom model mapping when needed. Figure 2: Composing a supergraph with Apollo Federation, custom built resolvers for MongoDB, and the hosted MongoDB Atlas GraphQL API endpoint Expand business use-cases with subgraphs Supergraphs make it easy to compose microservices, but when it comes to hosting, managing, and storing the data that performs the business logic, the MongoDB Atlas Application Data Platform can help teams build their app requirements faster. Need a search bar? The same data stored in an Atlas Cluster can be search-indexed and use Atlas Search to perform full-text search operations without additional setup or syncing data to another search technology. Want to embed graphs and charts? A time series collection can make it easy to query large chunks of data by timestamp, and MongoDB Atlas Charts lets devs use the same MongoDB database to build these inside applications. Other services, like custom Data APIs and data federation, help ensure that data can be queried and stored in the way that best fits a team’s needs. Focused on scale Engineering teams need to be able to anticipate both current and future needs. MongoDB Atlas delivers an application data platform that spans multiple regions, clouds, and deployment types to solve the data challenges of transactional workloads, modern apps, and microservices. Self-healing clusters ensure that developers are not scrambling to diagnose issues with their data nodes, and multi-region and multi-cloud deployments provide automatic failover for both models, respectively. Together, Apollo and MongoDB are committed to providing developers with the effective tools they need to simplify their architecture, improve app performance, ship faster, and grow their businesses. Register for Atlas today Learn more about the supergraph on the Apollo blog .

June 13, 2022