You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/pages/blog/2024-06-28-why-i-like-graphql.mdx
+48-117
Original file line number
Diff line number
Diff line change
@@ -5,157 +5,88 @@ date: 2024-06-28
5
5
byline: Marc-André Giroux, edited by Jeff Auriemma
6
6
---
7
7
8
-
A recent post, [Why, after 6 years, I’m over GraphQL](https://bessey.dev/blog/2024/05/24/why-im-over-graphql/), made the rounds in the tech circle. The author argues that they would not recommend GraphQL anymore due to concerns like security, performance, and maintainability. In this post, I want to go over some interesting points made, and some points I think don't hold up to scrutiny.
8
+
This post is edited from its [original publication](https://www.magiroux.com/eight-years-of-graphql/) on Marc-André Giroux's blog. Though originally intended to respond to a specific article, we at the GraphQL Foundation felt that Marc-André's post would stand well on its own with a few light edits. What follows is a well-informed set of practices and principles that engineers should consider when using GraphQL in production.
9
9
10
-
Always be Persistin'
11
-
--------------------
10
+
## Use persisted queries
12
11
13
-
Ok, first of all, let's start with something maybe a little bold: **Persisted Queries are basically essential for building a solid GraphQL API**. If you are not using them, you're doing GraphQL on hard mode. It's not impossible, but it leads to difficult problems, some of them discussed in the post. After 8 years of GraphQL, this has only gotten more and more important to me. Persist all queries, as soon as possible in your GraphQL journey. You'll thank yourself later.
12
+
Ok, first of all, let's start with something maybe a little bold: **Persisted Queries are basically essential for building a solid GraphQL API**. If you are not using them, you're doing GraphQL on hard mode. It's not impossible, but it leads to difficult problems that show up in GraphQL debates from time to time. After 8 years of GraphQL, this has only gotten more and more important to me. Persist all queries, as soon as possible in your GraphQL journey. You'll thank yourself later.
14
13
15
-
It is a little sad that this is basically glossed over and mentionned only in a small note at the bottom:
16
-
17
-
> Persisted queries are also a mitigation for this and many attacks, but if you actually want to expose a customer facing GraphQL API, persisted queries are not an option.
18
-
19
-
I assume the author is talking about public APIs here. While I don't think this is necessarily inherently true (One could ask customers to register queries first, figuring out the DX for this would be an interesting task), it's still a valid point. That's why [We Don’t See Many Public GraphQL APIs](https://productionreadygraphql.com/blog/2019-10-21-why-we-dont-see-many-public-graphql-apis) out there, and why I would not pick GraphQL if I were to expose a public API today.
14
+
Public APIs are a bit different. To be clear, it's not necessarily true that you can't use Persisted Queries in public APIs; one could ask customers to register queries first. But figuring out the DX for this would be an interesting task. That's why [We Don’t See Many Public GraphQL APIs](https://productionreadygraphql.com/blog/2019-10-21-why-we-dont-see-many-public-graphql-apis) out there, and why I would not pick GraphQL if I were to expose a public API today.
20
15
21
16
For a public API, a coarser-grained, resource-based API works great, and can be described through OpenAPI. SDKs can be generated through amazing tools like [Kiota](https://learn.microsoft.com/en-us/openapi/kiota/overview). It's hard to beat a well-made SDK for a public API, and in my experience, that's actually what customers expect and want to use. Moving on.
22
17
23
-
Haxors
24
-
------
25
-
26
-
The author's first point is about GraphQL's allegedly bigger attack surface. Again this focuses more on completely public GraphQL APIs which are relatively rare:
27
-
28
-
> exposing a query language to untrusted clients increases the attack surface of the application
29
-
30
-
I think that's right, it's hard to argue about this, hence not exposing a query language to untrusted clients unless you're ready to handle the trade-offs. But let's see what they think is hard to get right here.
31
-
32
-
Authz is really hard...
33
-
-----------------------
34
-
35
-
Authorization is a challenge with GraphQL. The thing is it's almost always challenging no matter what API style you use. I'd go as far as saying it is a challenge with designing software in general. The example given in the post actually highlights this very well:
36
-
37
-
query {
38
-
user(id: 321) {
39
-
handle # ✅ I am allowed to view Users public info
40
-
email # 🛑 I shouldn't be able to see their PII just because I can view the User
41
-
}
42
-
user(id: 123) {
43
-
blockedUsers {
44
-
# 🛑 AndsometimesIshouldn't even be able to see their public info,
45
-
# becausecontextmatters!
46
-
handle
47
-
}
48
-
}
18
+
## Authorization
19
+
20
+
Authorization is a challenge with GraphQL. The thing is it's almost always challenging no matter what API style you use. I'd go as far as saying it is a challenge with designing software in general. Here's an example from a recent post that actually highlights this very well:
21
+
22
+
```graphql
23
+
query {
24
+
user(id: 321) {
25
+
handle # ✅ I am allowed to view Users public info
26
+
email # 🛑 I shouldn't be able to see their PII just because I can view the User
27
+
}
28
+
user(id: 123) {
29
+
blockedUsers {
30
+
# 🛑 And sometimes I shouldn't even be able to see their public info,
31
+
# because context matters!
32
+
handle
49
33
}
50
-
34
+
}
35
+
}
36
+
```
51
37
52
38
`handle` and `email` both have different authorization rules. This is actually quite tricky to handle with a `GET /user/:id` endpoint as well. There's really nothing that makes GraphQL harder here. Yes when fine-grained authorization is needed, you'll need fine-grained authorization checks at that level.
53
39
54
-
user(id: 123) {
55
-
blockedUsers {
56
-
# 🛑 AndsometimesIshouldn't even be able to see their public info,
57
-
# becausecontextmatters!
58
-
handle
59
-
}
60
-
}
61
-
40
+
```graphql
41
+
user(id: 123) {
42
+
blockedUsers {
43
+
# 🛑 And sometimes I shouldn't even be able to see their public info,
44
+
# because context matters!
45
+
handle
46
+
}
47
+
}
48
+
```
62
49
63
50
This part is interesting as well and another challenge of authorizing code and models in general. The same "object" accessed through different contexts can actually have different authorization rules. Again, this is common in all API styles as well. That's why I actually usually advise designing this through a different model entirely, since it's likely they'll evolve differently. Here this is even possibly an API design mistake instead. If `handle` should never be seen for `blockedUsers`, then it shouldn't even be part of the schema here. This is a super common mistake where folks try to reuse common models/types instead of being specific.
64
51
65
52
After 8 years of GraphQL, I realize more and more that authorization is much more than a GraphQL problem. The API layer part is easy, but most code-bases are much more complex and must guard against unauthorized access in much better ways. Companies like [oso](https://www.osohq.com/) and [authzed](https://authzed.com/) and two great examples of how to do this well but also how complex this thing can be in general.
There, muchbetter. ThetruthisthatnomatterwhatAPIstyleyouuse, whetherthat's a binary protocol over UDP or GraphQL, it is extremelly rare, especially as the API surface grows, that all use-cases and "requests" will be equally expensive for a server to process.
56
+
We cannot assume that all requests are equally hard on the server, whether we're using GraphQL or not. The truth is that no matter what API style you use, whether that's a binary protocol over UDP or GraphQL, it is extremely rare, especially as the API surface grows, that all use-cases and "requests" will be equally expensive for a server to process.
79
57
80
58
A very easy example to show this is simply a paginated endpoint:
To be 100% fair, of course GraphQL exposes this problem a bit more, earlier on, especially when not using persisted queries (Which should not happen!). And while folks building a small RPC API may not need to implement variable rate limiting or some sort of cost categories, they almost always end up having to.
94
73
95
74
And again: this focuses on public, unauthenticated public APIs. I think we can agree this is not [GraphQL's Sweet Spot](https://productionreadygraphql.com/blog/2020-05-14-sweetspot).
Theperformancesectionfocusesmainlyondataloaderandn+1s. Ithinktheauthormakessomegoodpointshere. It's true that a GraphQL API must be ready for many query shapes and use cases. It is wise to implement efficient data loader for most fields through dataloaders. In fact, that'swhyIdon't recommend using datafetching techniques that are overly coupled to the query shape, things like AST analysis, lookaheads, and things using context from sibling or parent fields.
Againthisisanextremellysimpleexample, whichhasatrivialsolution. Butit's true, an endpoint based API that is simple can usually be kept simple, rather than being part of multiple other use-cases that could affect its performance long term.
76
+
## Performance
109
77
110
-
OverallIthinkdataloaderisarequirementforGraphQL, andIagreethatit's part of the slight complexity add for a GraphQL API, even simple ones. Authorization n+1s are also an issue.
78
+
Many GraphQL critics talk about the N+1 problem and DataLoader. It's true that a GraphQL API must be ready for many query shapes and use cases. It is wise to implement efficient data loader for most fields through dataloaders. In fact, that's why I don't recommend using datafetching techniques that are overly coupled to the query shape, things like AST analysis, lookaheads, and things using context from sibling or parent fields. But an honest observer should acknowledge that this is a problem with REST as well. As the complexity of an endpoint or operation increases, an engineer should expect to have to make more trade-offs in the name of performance.
Overall I think dataloader is a requirement for GraphQL, and I agree with critics that it's part of the slight complexity add for a GraphQL API, even simple ones. Authorization N+1s are also an issue, though, again, REST has these issues as well.
113
81
114
-
Butthat's simply not true. Authz n+1s exist everywhere including in REST given a sufficiently complex API. Performant authz is a problem of its own.
That's of course an observation from the author'sexperience, butingeneral, thiscouldn't be further from the truth. Hell, this is even specifically stated on [GraphQL'swebsite](https://graphql.org/learn/thinking-in-graphs/):
122
-
123
-

124
-
125
-
Ifoneendsupwithcouplingit's because there'satendencytocouplebusinesslogicwiththetransportlayeringeneral. Butit's not like GraphQL encourages you to do so. It actually does the opposite.
What?WhatAPIstyleencouragesbreakingchanges?That's probably not a good idea. I think there'ssomeconfusionhere. GraphQLencouragescontinuousevolutionofyourAPI. Thisusuallyrelatestoversioningratherthanavoidingbreakingchanges. Insteadofbreakingchanges, withGraphQLyoudeprecateschemamembers, andonlyremovethemoncetheyarenotusedanymore (orareokwithbreaking).
84
+
One of the biggest "culture shocks" of adopting GraphQL is the concept of continuous evolution. Because GraphQL has primitives for deprecating and aliasing fields, the notion of backwards compatibility becomes gray instead of black-and-white. Want to add a field? Great, do it. Want to remove a field? Add `@deprecated` and gradually stop querying them, eventually removing the field altogether.
147
85
148
86
Arguably one of GraphQL's most powerful tool is around continuous evolution. The fact the client declaratively selects the API surface it is interested in means we can track with very high precision how our API is used. This allows us to actually **avoid** breaking changes, and make removals safely on fine grained parts of the schema.
149
87
150
88
Breaking changes and deprecations suck. We all try to avoid them, and yes it's annoying for clients. But if anything GraphQL makes this easier, not harder.
151
89
152
-
Conclusion
153
-
----------
154
-
155
-
Overall, IcanfeelthepainoftheauthorwhenitcomestobuildingpublicGraphQLAPIs. It's not easy. But the post in general never really addresses a very common use-case for GraphQL, which is an internal API for known multiple clients. In this context, using persisted queries is easy, and solves a lot of the problems the author encountered in their journey. There are also a lot of problems mentionned here that are hard problems in general, and I don'talwaysbuythefactthatGraphQLmakesthemanyharder.
Anyway, **Persistyourqueriesandprobablydon't build public GraphQL APIs unless you really know what you'redoing.**
90
+
## Conclusion
160
91
161
-
* [xuorig](https://x.com/__xuorig__)
92
+
After 8 years of GraphQL, I still enjoy the decoupling a GraphQL schema offers between server-side capabilities and client-side requirements, but am aware of the trade-offs when picking it as a technology. Definitely persist your queries. Definitely build a public GraphQL API only if you really know what you're doing. Many critiques of GraphQL apply to any sufficiently complex API, there are no silver bullets in software engineering.
0 commit comments