The Ultimate API Showdown: Server Actions, tRPC, GraphQL, and REST Compared!

Jack Herrington
20 Nov 202310:49

Summary

TLDR本视频深入探讨了服务器操作与tRPC、GraphQL和REST的比较,旨在帮助观众了解这些技术在项目中的最佳选择。从易于设置、使用便捷性、执行变更操作和查询能力,到类型安全性和兼容性,每种技术的优势和劣势都进行了详尽分析。特别提到,服务器操作在Next.js 14中的引入极大简化了开发流程,但每种技术都有其独特适用场景。视频最后,根据不同需求推荐了适用的技术选择,为开发者提供了宝贵的指导意见。

Takeaways

  • 🤖 服务器操作(Server Actions)是Next.js 14内置的,用于在服务器端执行操作的函数。
  • 🔑 服务器操作、tRPC、GraphQL和REST是四种不同的范式,各有优缺点。
  • ✅ 服务器操作在设置和使用方面非常简单,只需调用一个函数。
  • 📝 服务器操作适合执行变更操作,但不太适合查询操作。
  • 🦺 tRPC和GraphQL都非常擅长查询,但GraphQL存在潜在的拒绝服务攻击风险。
  • 🔐 服务器操作和tRPC具有很好的类型安全性,而GraphQL则通过内省获得类型信息。
  • 📱 tRPC和GraphQL在不同平台之间有较好的互操作性,而服务器操作则受框架限制。
  • 🌐 只有服务器操作能够在没有JavaScript的情况下工作。
  • 💡 对于简单的Web应用程序,结合服务器渲染和服务器操作是最简单的选择。
  • 📌 如果需要支持移动端或桌面端,建议使用tRPC或GraphQL(内部使用)。

Q & A

  • 什么是服务器操作(Server Actions)?

    -服务器操作是在Next.js 14中引入的一种新功能。它们是在服务器端定义的函数,可以通过客户端直接调用,框架会自动处理发送请求到服务器并获取数据的过程。

  • tRPC(TypeScript Remote Procedure Call)是什么?

    -tRPC是一种类型安全的远程过程调用方式。你可以在服务器端定义要暴露的远程过程调用,它会自动为你创建查询钩子,并处理API端点和函数之间的数据传输。

  • GraphQL是什么?

    -GraphQL是一种由Meta(Facebook)创建的查询语言。它基于HTTP和JSON,但与传统的REST API不同的是,它使用一种特殊的查询语法来指定查询或修改操作。

  • 什么是REST?

    -REST代表representational state transfer(表现层状态转移)。它是一种架构风格,定义了一组约束条件和原则,但没有严格的标准。REST APIs可以有不同的形状和风格。

  • 从设置难易程度来看,四种范例有何区别?

    -服务器操作最容易设置,因为它内置在Next.js 14中。tRPC文档较好,相对简单。GraphQL设置较困难,需要配置端点和代码生成器。REST设置通常比较简单。

  • 在易用性方面,四种范例有何区别?

    -服务器操作最容易使用,因为就像调用函数一样。tRPC和GraphQL使用查询与端点交互,类似于React Query。REST则需要自定义API端点。

  • 在执行数据修改(Mutation)时,四种范例有何优缺点?

    -服务器操作旨在处理数据修改,表现出色。tRPC和GraphQL也做得不错。REST虽然也容易修改数据,但需要自定义非标准的API,存在一些问题。

  • 在执行数据查询时,四种范例有何优缺点?

    -tRPC和GraphQL在查询方面表现出色,但GraphQL存在潜在的拒绝服务风险。REST也很适合查询。服务器操作并不是为查询而设计的,在Next.js中可能会遇到队列等待的问题。

  • 从类型安全性来看,四种范例有何优缺点?

    -服务器操作和GraphQL天生就是类型安全的。GraphQL还可以通过内省从服务器获取模式并生成类型。REST通常缺乏类型安全性,需要一些额外的设置。

  • 在与其他客户端(如移动端、桌面端)的兼容性方面,四种范例有何区别?

    -由于API由框架定义,服务器操作在跨端兼容性方面表现不佳。tRPC在JavaScript/TypeScript之间可以很好地共享类型。GraphQL作为标准,在各种语言和平台间有很好的兼容性。REST被广泛支持,但缺乏标准化。

Outlines

00:00

🗣️ 介绍服务器操作与其他技术的比较

这一段介绍了将在本视频中比较的四种技术范例:服务器操作、typesafe远程过程调用(trpc)、GraphQL 和 REST。并概述了它们的基本工作原理和特点。同时指出,将比较这些技术在设置、使用、变更、查询、类型安全性、兼容性和无 JavaScript 支持等方面的优缺点,以帮助确定哪种技术最适合您的项目。

05:00

🚀 比较服务器操作、trpc、GraphQL 和 REST

这一段详细比较了服务器操作、trpc、GraphQL 和 REST 在各个方面的优缺点。在设置和变更(例如执行变更)方面,服务器操作相当简单;在查询方面,服务器操作则存在一些限制。类型安全性方面,服务器操作和 GraphQL 更出色。兼容性方面,GraphQL 和 REST 表现较好,但服务器操作则受限于框架的实现。在无需 JavaScript 的情况下,只有服务器操作可以工作。最后,作者根据不同的场景,分别推荐了服务器操作、trpc 和 GraphQL 作为首选。

10:02

🏆 总结与建议

在这一段中,作者总结并给出了自己的建议。对于简单的 Next.js Web 应用程序,使用服务器操作和服务器组件查询是最简单的方式。如果需要支持移动客户端或桌面客户端,推荐使用 trpc。对于需要在服务器和客户端上进行定制查询且不会受到拒绝服务攻击的内部应用程序,则推荐使用 GraphQL。作者还鼓励观众就这些评估发表意见,并呼吁观众点赞和订阅。

Mindmap

Keywords

💡服务器行为

服务器行为是在服务器上定义的函数,可以直接从客户端调用。在视频中,服务器行为是通过使用XJS 14中的use server pragma定义的,并且可以无需额外操作即由客户端调用,这使得它们非常易于使用。这一概念强调了服务器和客户端之间交互的简化,尤其在进行数据请求和更新时。

💡tRPC

tRPC代表类型安全的远程过程调用,允许开发者定义服务器暴露的远程过程调用,并自动创建查询钩子。视频中指出,tRPC基于HTTP和JSON,强调了其对类型安全的支持,并提出即使不使用TypeScript也可以使用tRPC进行RPC请求。

💡GraphQL

GraphQL是由Meta创建的一种查询语言,它基于HTTP和JSON。视频中提到,GraphQL的独特之处在于其请求格式,允许用户定义查询或变更,并且有完整的标准支持。这使得GraphQL在数据查询和更新时非常灵活,尽管设置起来可能相对复杂。

💡REST

REST代表表现层状态转移,是一种架构风格,通过定义层次结构中的实体来交互。视频强调了REST相对缺乏标准化和格式多样性,以及开发者通过OpenAPI和Swagger等工具来规范REST API的尝试。

💡易于设置

视频中讨论了服务器行为、tRPC、GraphQL和REST的设置难易度。服务器行为因为内建于NEX JS 14而得到最高评价,而GraphQL由于需要额外的工具和代码生成器而被认为设置相对困难。这一比较突出了不同技术对开发者友好度的影响。

💡易用性

易用性指的是开发者在实际应用中调用和利用这些技术的难易程度。视频中指出,服务器行为的易用性非常高,因为它们就像在客户端直接调用函数一样简单。这一点在选择技术栈时是一个重要的考量因素。

💡变更操作

变更操作是指对服务器数据的更新或修改。视频中提到,服务器行为、tRPC和GraphQL都很擅长执行变更操作,而REST的变更操作则因其非标准化而显得灵活但可能带来问题。这一讨论强调了在实现数据更新时不同技术的适用性。

💡查询操作

查询操作是从服务器获取数据的过程。视频指出,服务器行为不太适合进行查询操作,因为它们主要设计用于执行服务器端的动作,而tRPC和GraphQL则在查询方面表现出色。这表明不同的技术适用于不同类型的数据交互需求。

💡类型安全

类型安全是指代码中变量的类型是固定的,这有助于防止类型错误。视频中提到,服务器行为和GraphQL在类型安全方面表现优异,特别是GraphQL通过服务器端的自省能力支持强大的类型系统。这强调了类型安全在确保应用稳定性和减少运行时错误方面的重要性。

💡兼容性

兼容性讨论了不同技术与其他客户端(如移动和桌面应用)的交互能力。视频强调了服务器行为在与非框架客户端交互时可能遇到的限制,而tRPC和GraphQL则在不同环境中提供了更好的兼容性。这一点对于跨平台应用的开发尤其重要。

💡无JavaScript环境下的工作能力

这一概念讨论了在客户端禁用JavaScript时,不同技术的工作能力。视频中提到,服务器行为能够以表单提交的形式工作,而这是无需JavaScript支持的。这对于提高网站的可访问性和兼容性具有特别的意义。

Highlights

Server actions are functions that you define on the server, and you can invoke them from the client by just calling that function.

tRPC (typesafe remote procedure calls) allows you to define remote procedure calls that the server exposes, and it automatically creates query hooks for you.

GraphQL is a query language created by Meta, sitting on top of HTTP and JSON, with a standardized request format for queries and mutations.

REST (Representational State Transfer) is an architectural style with defined entities in a hierarchical structure, but without strict formality or standards.

Server actions are incredibly easy to set up and use, as they're built into Next.js 14 and you just call functions on the client.

tRPC is fairly easy to set up, with good documentation, while GraphQL is more difficult due to the need for code generation and type safety setup.

REST is easy to set up in most environments, as they usually have a way to create API endpoint routes.

Server actions are designed for mutations, making them shine in that aspect, while tRPC and GraphQL are also good for mutations.

For queries, server actions are not as great, as requests are queued, while tRPC and GraphQL excel, but GraphQL has a potential denial of service issue.

Server actions and tRPC are highly type-safe out of the box, while GraphQL has an advantage with introspection and generating types from the server schema.

Server actions have compatibility issues with mobile and desktop clients, as the API is defined by the framework, while tRPC and GraphQL are more compatible across platforms.

REST has good compatibility with most clients, as they can make GET or POST requests against the server.

Server actions are the only option that can work without JavaScript being enabled on the client, as they can be accomplished through form posts.

For simple web-only Next.js applications, using server components with queries and server actions is recommended.

For applications with mobile or desktop clients, tRPC or GraphQL (for internal apps) are recommended, with consideration for the potential denial of service issue with GraphQL on the open internet.

Transcripts

play00:00

whenever the topic of server actions

play00:01

comes up inevitably folks ask well now

play00:03

that we have server actions and

play00:04

something like nextjs do we even need

play00:07

stuff like trpc graphql or rest and

play00:11

that's actually a complex question so

play00:13

what we're going to do in this video is

play00:15

compare server actions to trpc graphql

play00:19

and rest and give you some insights into

play00:22

which might be the best choice for your

play00:24

project let's get right into

play00:28

it

play00:33

all right let's introduce our four

play00:34

different paradigms starting off we've

play00:37

got server actions server actions are

play00:39

functions that you define on the server

play00:42

in the case of xjs 14 you define them

play00:45

using the use server pragma and then you

play00:47

can invoke them from the client by just

play00:50

calling that function and nextjs or

play00:53

solid start or quick or whatever

play00:55

framework you're using manages that

play00:58

entire flow of making that call for you

play01:01

next up is trpc or typesafe remote

play01:03

procedure calls you define the remote

play01:06

procedure calls that you want to have

play01:08

exposed by the server it then

play01:10

automatically creates some handy query

play01:13

hooks for you and does all of that

play01:16

marshalling for you between the API

play01:18

endpoint and your function which is

play01:20

really cool it sits on top of HTTP and

play01:22

Json and despite what people may think

play01:25

you don't have to be using typescript to

play01:27

use trpc you can make RPC requests from

play01:32

any language next up is graphql it is a

play01:34

query language that was created by meta

play01:36

it sits again on top of HTP and Json

play01:38

there is nothing exotic about the return

play01:41

values those are simple Json what is

play01:43

interesting is the request format which

play01:46

is a query language and you can specify

play01:49

either queries or mutations and the

play01:51

Syntax for that query is well understood

play01:55

and there's a full standard around that

play01:57

and then finally there is rest rest

play01:59

stand stands for representational State

play02:01

transfer it is an architectural style

play02:04

where there are entities defined in a

play02:07

hierarchic structure but beyond that it

play02:10

it really doesn't have a lot of

play02:11

formality to it or standards to it and

play02:14

so you see things like open API and

play02:17

Swagger as ways to kind of lock down

play02:20

rest apis but rest apis can have all

play02:24

kinds of different shapes and flavors

play02:26

and styles so now let's get into

play02:28

comparing the various advantag and

play02:30

disadvantages of using server actions

play02:32

trpc graphql and rest we'll start off

play02:37

without easy each one of these is to set

play02:39

up in terms of setup server actions is

play02:41

incredibly easy to set up it's actually

play02:43

built into NEX js14 now so you don't

play02:46

even have to enable anything all you

play02:48

have to do is just start using them and

play02:50

they work out of the box so that's why

play02:52

I'm giving it three up arrows if it's

play02:55

three down arrows that would be the

play02:56

lowest ranking three up arrows is our

play02:58

highest ranking so how does that compare

play03:00

to something like trpc well trpc is

play03:03

fairly easy to set up the docs are

play03:04

pretty good about how to set this up in

play03:06

your environment so I'm going to give

play03:07

that two up arrows graphql is actually

play03:10

pretty difficult to set up not only you

play03:11

have to go and set up the API end points

play03:13

and get those going but you also usually

play03:16

have to do something like a graphql code

play03:18

generator to just go and create all of

play03:21

the stuff like the query Hooks and

play03:23

everything else to keep it type safe it

play03:25

is kind of a pain to set up and then

play03:27

rest is really easy to set up in most of

play03:29

these environments

play03:30

nextjs remix they all have a way to

play03:32

create API endpoint routes all right

play03:35

what's next ease of use all right well

play03:38

server actions again are incredibly easy

play03:39

to use there are just functions all you

play03:42

got to do is just call these functions

play03:44

on the client and the client does all of

play03:47

the work of actually making the request

play03:49

against the server and getting the data

play03:51

back it couldn't be easier it looks just

play03:53

like a function and if you're familiar

play03:54

with react query trpc is pretty easy to

play03:57

use as well as our graphql and rest

play04:00

they're basically just queries against

play04:03

that endpoint and with react query you

play04:05

get all those status values and all of

play04:06

that but let's face it come on use Query

play04:09

is never going to be as convenient as

play04:11

just calling a function which is what

play04:12

you do with server actions our next

play04:15

category is mutations how easy is it to

play04:17

do a mutation against a server server

play04:20

actions were designed to do mutations so

play04:23

that's one of the places where server

play04:26

actions are really going to shine of

play04:27

course they're pretty good across trp C

play04:29

and graphql as well graphql has specific

play04:33

types of queries known as mutations

play04:37

that's all type safe so that's really

play04:38

easy to use now rest is also really easy

play04:41

to do mutations with but the thing with

play04:44

rest is when it comes to these endpoints

play04:45

you're basically kind of making it up as

play04:47

you go along you get to define the API

play04:50

you could use a get to make a mutation

play04:53

which is not something you'd want to do

play04:54

you could use a post or a put you get to

play04:57

define the API that you want and so in

play05:00

essence you get to define a non-standard

play05:03

API and that can be a problem all right

play05:06

what's next well queries so the flip

play05:08

side of doing a mutation is to do a

play05:10

query this is actually where server

play05:12

actions are not so great so server

play05:15

actions aren't really designed to do

play05:17

queries they're server actions after all

play05:20

you're acting against the server you're

play05:21

not querying against it you're trying to

play05:23

make some sort of change on the server

play05:25

and one of the things I've heard

play05:27

recently about nextjs in particular is

play05:29

that server action requests are cued so

play05:31

if you want to go and make multiple

play05:32

query requests against a server you're

play05:34

going to have to wait for those to cue

play05:36

and that's not great when you want to do

play05:37

queries you're generally going to want

play05:39

to do multiple queries simultaneously so

play05:41

that's something to watch out for when

play05:42

it comes to server actions on nextjs

play05:45

queries really shine of course on trpc

play05:47

and graphql but there's a big hidden

play05:50

gotcha on graphql and that's the fact

play05:53

that a client can make up any query that

play05:56

they want against the server and that

play05:58

can be deadly you can get a denial of

play06:00

service against your API when someone

play06:02

makes an AP a malicious API request that

play06:06

ends up eating a lot of either database

play06:09

time or server time to accomplish that

play06:11

request so that's something to watch out

play06:13

for when it comes to graphql now there

play06:15

are productss like vunder graph that

play06:16

allow you to do build time graphql but

play06:19

runtime rest that avoid that issue so

play06:22

that's simply something you want to

play06:23

think about when you think about putting

play06:24

graphql into production particularly

play06:27

when you're thinking about high traffic

play06:30

and then of course rest is phenomenal

play06:32

for queries all you've got to do is just

play06:33

create a get endpoint and it's really

play06:35

easy so another big question is how

play06:37

types safe are these so server actions

play06:39

are fantastic they're typees safe right

play06:40

out of the box you're just calling a

play06:42

function and so typescript is able to

play06:44

track those types just as a function now

play06:48

type safety is one where I'm going to

play06:49

give an advantage to graphql because

play06:51

graphql allows for introspection against

play06:53

the server and that means that a client

play06:55

whether it be on JavaScript or

play06:57

typescript or go or rust or whatever

play06:59

have you you can go and request against

play07:02

the server to get the schema from the

play07:04

server and create types off of that so

play07:07

that is very very handy and it's a

play07:09

unique advantage of

play07:11

graphql and then rest is a problem when

play07:13

it comes to type safety because again

play07:15

you can make up anything you want when

play07:17

it comes to rest now you could follow

play07:19

open API or you could export Swagger and

play07:24

potentially get types out of rest but

play07:25

that's a lot of setup and unless you're

play07:28

in a larger setup most of the time you

play07:31

are just going and creating your own

play07:33

types around your own queries and that's

play07:35

not great for type safety all right

play07:37

another Vector of comparison is

play07:38

compatibility so if you are going to

play07:41

have these endpoints how compatible are

play07:43

they with other things making those

play07:45

calls for example you have a mobile

play07:47

client or you got a desktop client are

play07:49

those going to interface with these

play07:51

Technologies well and that's actually

play07:53

one of the areas where server actions

play07:55

fall down and that's because server

play07:58

actions the API is a effectively

play08:00

designed by the framework so you don't

play08:02

control what type of query it is if it's

play08:05

a form post what the actions are the

play08:09

format of the request you control none

play08:11

of that so when it comes to a mobile

play08:14

client if all you have a server actions

play08:16

they're basically going to have to look

play08:17

at the wire and see what's going across

play08:19

the wire and then imitate that to go and

play08:22

do the equivalent of the server action

play08:25

which is not a great API trpc is really

play08:28

good for this you can have the types

play08:29

that are then imported by a client if

play08:32

you got JavaScript or typescript on the

play08:34

other client that's going to be

play08:36

fantastic when it comes to trpc when it

play08:38

comes to something like go or rust or

play08:40

another framework that's not going to be

play08:43

as good but yeah if you're doing trpc

play08:46

between two different typescript

play08:48

applications be it react native and the

play08:51

web that's going to be great graphql is

play08:53

fantastic when it comes to

play08:54

interoperability that's because graphql

play08:57

is a standard and so you can make

play08:58

Library liaries that comport to that

play09:00

standard and therefore you can have you

play09:03

know IOS and Android libraries that are

play09:05

fantastic for doing graphql as well as

play09:07

rust and go it's great rust is pretty

play09:11

good when it comes to compatibility

play09:12

pretty much everything you're going to

play09:13

find is going to be able to do a get or

play09:15

a post against some server but you're

play09:17

not going to get that really nice

play09:19

standardized request like you would with

play09:21

like a graphql one last area of

play09:23

comparison that's specific to the web is

play09:25

whether it can work without JavaScript

play09:27

being enabled on the client the only on

play09:29

that's going to be able to do that are

play09:31

server actions server actions are often

play09:33

done as a form post that's a basic

play09:35

function of the web and therefore can be

play09:37

accomplished without JavaScript and the

play09:39

only thing that's going to do that of

play09:40

course is server actions in this case so

play09:42

if you need that it's going to be server

play09:44

actions or nothing so my recommendations

play09:46

as of late 2023 after nextjs 14 has come

play09:49

out are that for very simple nextjs

play09:52

applications that are webon using a

play09:55

combination of queries on the server in

play09:58

react server components and server

play10:00

actions is going to be the easiest way

play10:01

to go if you're looking to support

play10:04

mobile if you're looking if you also

play10:06

have mobile clients or desktop clients

play10:08

then I personally would recommend

play10:10

probably

play10:11

trpc if it's an internal app or an

play10:14

internet app I might also look at

play10:15

graphql as a possibility nice thing

play10:18

about graphql is it's really good on the

play10:20

server and the client and it's really

play10:22

easy to make customized

play10:24

queries but on the other hand it's got

play10:27

that denial of service issue so I

play10:29

wouldn't want to put it out on the open

play10:31

internet because it could potentially be

play10:33

denial of serviced well I hope this

play10:35

helps you figure out what you want to

play10:36

use in your applications of course let

play10:38

me know in the comments do you agree

play10:40

with my assessments or not in the

play10:41

meantime of course hit that like button

play10:43

if you like the video hit the Subscribe

play10:44

button if you really like the video and

play10:46

I'll see you on the next Blue Collar

play10:47

coder