Directive-driven GraphQL Development

Rate this content
Bookmark

You've heard of schema-first and code-first GraphQL development. You've seen tools that autogenerate GraphQL schemas from Swagger and SQL. But there's another way to build GraphQL APIs that's flexible, maintainable, and evolvable — just writing GraphQL SDL with directives!

This talk has been presented at GraphQL Galaxy 2021, check out the latest edition of this Tech Conference.

FAQ

Directive-Driven GraphQL is a development approach for building GraphQL APIs by defining the API using the schema definition language (SDL) and the behavior of the API using a domain-specific language (DSL). It uses GraphQL directives to combine these elements in the same file, passing them to a black box implementation that manages data fetching behavior from various data sources.

The four approaches to building a GraphQL API include schema first, code first, data-driven, and directive-driven. Schema first involves defining the schema and then coding the resolvers. Code first starts with writing resolvers and deriving the schema. Data-driven generates both schema and resolvers from data source descriptions. Directive-driven uses directives to define API behavior and schema.

The directive-driven approach aligns with principles like good API design, agility in schema evolution, independence from specific implementations or data sources, and cost-effectiveness in development. It allows for declarative programming, enabling clearer and more maintainable code, and supports evolving APIs without extensive rewrites, thereby reducing tech debt.

Lenny Burdett's prototype for integrating gRPC with GraphQL involves three components: a boilerplate generator, a validator, and a runtime. The generator creates a GraphQL schema with directives based on a gRPC service definition. The validator ensures type and field accuracy, while the runtime executes the API, using directives to determine behavior based on gRPC calls.

The directive-driven approach addresses several challenges including maintaining good schema design across large teams, the ability to evolve the API as needs change, avoiding lock-in to specific technologies, and reducing the cost and complexity of development. It provides a flexible, declarative, and efficient way to build and manage GraphQL APIs.

The directive-driven approach is beneficial for organizations using Apollo on top of Federation by allowing efficient exposure of GraphQL APIs over existing APIs with minimal changes. It supports entity referencing and API composition across subgraphs, reducing coupling between different domains and teams, thereby simplifying data fetching and integration in distributed systems.

Lenny Burdette
Lenny Burdette
21 min
10 Dec, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Lenny Burdett discusses Directive-Driven GraphQL, a prototype for building GraphQL APIs. The approach involves adding and removing parts of the schema, and offers advantages over imperative systems. The prototype integrates GraphQL with gRPC, allowing for easy editing and reshaping of the schema. The gRPC DSL and directives determine the behavior of the API. The directive-driven approach also supports Apollo Federation and future work includes support for GraphQL unions and real-time data subscriptions.

1. Introduction to Directive-Driven GraphQL

Short description:

Hi, my name is Lenny Burdett and I'm a solutions architect at Apollo. Today at GraphQL Galaxy, I'll be discussing Directive-Driven GraphQL, a prototype I've been working on. I'll cover the different approaches to building GraphQL APIs, focusing on the data-driven and directive-driven methods. These approaches offer unique advantages and challenges, and I'll share my insights based on my experience working with large GraphQL platforms.

Hi, my name is Lenny Burdett and I'm a solutions architect at Apollo. I'm excited to be here today at GraphQL Galaxy to talk about a prototype I've been working on, based on a concept I'm calling Directive-Driven GraphQL. I'll start out with a short discussion on the various approaches to building GraphQL APIs and how one might choose between them. Then I'll give a quick demo of my prototype for building GraphQL APIs on top of GRPC APIs. Then I'll go deep on some design choices in the prototype that illustrate the power of the directive-driven approach.

So I propose that there are four distinct approaches to building a GraphQL API. The first two are well-known terms in the GraphQL community, schema first and code first. With schema first, you write your schema and GraphQL files and then you write your resolvers in code. With code first, you write your resolvers and then you derive your schema probably using metaprogramming or reflection. And there's plenty of literature about choosing between these approaches. So I'm not going to spend any time on that discussion. Instead, I want to spend time on the remaining two approaches which don't have well-known names as far as I know. If they do, please let me know. For this talk, I'll refer to them as data-driven and directive-driven. With a data-driven approach, you generate both your schema and resolvers from a description of your data source. Some examples of this approach are post GraphQL for Postgres and GraphQL Mesh for a variety of data sources. The directive-driven approach, which I'll define in a minute, is still pretty novel. I don't know many examples in the GraphQL community yet. I first encountered this approach when a colleague of mine at Square built this really neat directive-driven implementation for Elasticsearch.

I work with a bunch of companies building really large GraphQL platforms. Choosing a development approach is one of the earliest decisions they need to make. Like any engineering decision, it's always a matter of balancing trade-offs. Having a set of values helps guide your decision-making process. Here's some of the values that I hold, based on my personal experiences and working with customers, that I'll use to evaluate the GraphQL development approaches in this talk. If you have different values, you'll probably make different decisions, and of course that's entirely okay. First, I always want to prioritize good API design and provide a collaborative, agile schema design process. Ideally, my API strikes a balance between solving specific use cases, and being generalized enough to support new and unforeseen use cases. This is a challenge, especially at scale with dozens of teams and clients. So I want to avoid anything that gets in the way of good schema design. Second, I need the ability to gradually evolve my API as client and business needs change.

2. Directive-Driven GraphQL

Short description:

This includes being able to add types and fields, and also being able to remove unused parts to my schema to keep things manageable. GraphQL is a great abstraction layer on top of these implementation details. We believe that declarative systems are preferable to imperative systems. A directive-driven system involves two different three-letter acronyms working together. I've been working on a prototype of a directive-driven approach for building a GraphQL API on top of gRPC APIs. gRPC is an open source RPC framework that originally came from Google.

This includes being able to add types and fields, and also being able to remove unused parts to my schema to keep things manageable. Third, I don't want to be locked into any particular implementation or data source under the hood. Any successful system is eventually going to need a rewrite, or some kind of migration. You might start with Postgres, but after you add a few million users, you might realize that you'd be better off with a combination of DynamoDB and Elasticsearch.

GraphQL is a great abstraction layer on top of these implementation details. I don't want my choices today to restrict what's possible tomorrow. This one's a big Apollo value and one I hold as well. We believe that declarative systems are preferable to imperative systems. Since you're watching our GraphQL talk, you probably agree, GraphQL's declarative query language is a big improvement over writing imperative code to coordinate a bunch of REST API calls. Declarative systems are usually easier to understand, support great tools for static analysis, and are better for collaboration.

Apollo Federation's declarative composition model is one example of our investment in this idea. And lastly, the quicker and cheaper it is to build a system, the faster we can get to market and the less tech debt will accrue along the way. Unsurprisingly, I believe that a directive-driven approach to GraphQL fits these values the best. So let's dive into that idea and hopefully you'll agree.

A directive-driven system involves two different three-letter acronyms working together. First, you define your API using the schema definition language, or STL, and you define the behavior of your API using some domain-specific language, or DSL. With GraphQL directives, these can go together in the same file. To run a directive-driven API, you pass both the STL and DSL to some black box implementation. It uses the STL to provide the GraphQL API, and uses the DSL to determine the behavior of how it fetches data from your data sources.

I've been working on a prototype of a directive-driven approach for building a GraphQL API on top of gRPC APIs. I've definitely got a long way to go, but I'm excited to show off what I have here today. Before I dive into the prototype, I want to briefly touch on gRPC for anyone not familiar with it. gRPC is an open source RPC framework that originally came from Google. I happened to use it for many years when I worked at Square. I think it's really great for east-west communication between services. It's super fast and efficient. However, I always found it lacking when using it in web and mobile clients. It has a lot of the same downsides that REST APIs have. My dissatisfaction with gRPC for end-user clients is what drove me to learn GraphQL in the first place. The wire format for gRPC is called protocol buffers.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
The Talk discusses the challenges and advancements in using GraphQL and React together. It introduces RedwoodJS, a framework that simplifies frontend-backend integration and provides features like code generation, scaffolding, and authentication. The Talk demonstrates how to set up a Redwood project, generate layouts and models, and perform CRUD operations. Redwood automates many GraphQL parts and provides an easy way for developers to get started with GraphQL. It also highlights the benefits of Redwood and suggests checking out RedwoodJS.com for more information.
Adopting GraphQL in an Enterprise
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
Adopting GraphQL in an Enterprise
Today's Talk is about adopting GraphQL in an enterprise. It discusses the challenges of using REST APIs and the benefits of GraphQL. The Talk explores different approaches to adopting GraphQL, including coexistence with REST APIs. It emphasizes the power of GraphQL and provides tips for successful adoption. Overall, the Talk highlights the advantages of GraphQL in terms of efficiency, collaboration, and control over APIs.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
GraphQL has made a huge impact in the way we build client applications, websites, and mobile apps. Despite the dominance of resolvers, the GraphQL specification does not mandate their use. Introducing Graphast, a new project that compiles GraphQL operations into execution and output plans, providing advanced optimizations. In GraphFast, instead of resolvers, we have plan resolvers that deal with future data. Graphfast plan resolvers are short and efficient, supporting all features of modern GraphQL.

Workshops on related topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced 2022React Advanced 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura