WunderGraph for Backend Developers
A typical task for backend developers is to create an abstraction on top of existing internal or external services. The pattern to describe the solution to this problem is usually called Backend for Frontend or short BFF. But how do you implement a BFF actually? What are the steps you need to take?
Let's say you have a frontend application that needs to integrate with multiple services. The frontend application needs to integrate with a payment service, a user service, and a product service.
Here are the steps you need to take to build the BFF for this use case:
- You have to make decisions what technology to use for the BFF
- What kind of contract will the BFF have with the frontend?
- Should you expose the BFF as a REST API or GraphQL, or even RPC?
- How do you handle authentication? You'll probably want to delegate this to a Identity Provider
- How will you ingest upstream API definitions?
- How do you combine multiple APIs of different API styles (OpenAPI, GraphQL, SOAP, etc.)?
- How do you handle caching?
- How do you handle error handling?
- How should you implement tracing, logging and analytics?
- What about security? Input validation, etc.?
- If you're building a BFF for a Frontend, how will you generate a type-safe and developer-friendly client?
- How will you implement realtime APIs (e.g. GraphQL subscriptions) if that's a requirement?
- How will you package and deploy your BFF?
- How will you monitor your BFF?
As you can see, there are a lot of decisions to make. It's probably going to take a few months until you have a good first version of your BFF ready, and that's really just the starting point of your journey. You'll eventually run into edge cases and limitations of your first design, so you'll have to refactor some big chunks of the codebase after a year.
But why? Why not start with a BFF Framework that already made a lot of these decisions for you? Over the last few years, we've seen all kinds of use cases and requirements for BFFs from one-man startups to large enterprises. With a Framework like WunderGraph, you'll not always agree with all the decisions that were made, but you'll have a head start of many man-years of experience, allowing you to really focus on the functionality that brings value to your business instead of inventing the wheel again and again.
So, how does WunderGraph help you to build a BFF?
There are three core concepts in WunderGraph that make the process of building a BFF much easier. Instead of manually generating SDKs, or even worse, writing glue code by hand. WunderGraph abstracts away this whole process and gives you a developer-friendly API to build your BFF.
- APIs as Dependencies
- API Namespacing
- API Composition
APIs as Dependencies
In WunderGraph, you can introspect APIs using their specification, e.g. an OpenAPI definition, a GraphQL schema, or a WSDL. This is similar to how you would install a package from npm. WunderGraph will introspect all APIs and generate a GraphQL Schema across all APIs as well as a GraphQL Execution Layer that allows you to talk to this unified API layer.
But there's a problem: When combining multiple APIs, you'll run into naming conflicts.
To solve this problem, we've introduced the concept of API Namespacing. You can and should apply a namespace to every API you add to your BFF. This doesn't just solve the naming conflict problem, but also makes it very convenient to "look up" the right API when you're implementing your BFF.
API namespacing is similar to modules in programming languages. You can import a module from an external dependency, and you're able to rename it when importing it to avoid naming conflicts.
The last core concept is API Composition, building on top of the previous two.
API Composition is the overarching concept behind WunderGraph. Instead of manually writing glue code or integrating APIs semi-automatically, API Composition allows you to compose APIs like modules.
Add your API dependencies to your BFF. WunderGraph generates a GraphQL & TypeScript ORM on top of your APIs. You implement your BFF either by writing GraphQL Operations or, in more complex scenarios, by writing custom TypeScript Operations that use the TypeScript ORM.
You don't have to manually import or generate SDKs. All APIs are automatically instrumented with tracing, logging, and analytics. All you have to do is to implement the business logic of your BFF.
Write your operations
You can use GraphQL to query your database or write TypeScript operations if you need to do more complex things, like transforming data.
Note that the
pg_ prefix is the namespace that we configured for the Postgres datasource.
In this operation we fetch the current user and also fetch the subscription details from Stripe.
Share Postman collection and OpenAPI Specification
WunderGraph also generates an Postman collection and OpenAPI Specification for your BFF.
Open API Specification
Our goal with WunderGraph is to make Developers more productive when building BFFs, and to improve the quality and security of BFFs while at the same time reducing the complexity and time to market.