CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
GraphQL 入门/课时/综合练习
RawBlame
课时目录12 节

GraphQL 入门

  1. 01GraphQL 是什么
  2. 02Schema 与类型
  3. 03Query 与字段选择
  4. 04Mutation
  5. 05Resolver 模型
  6. 06Spring for GraphQL
  7. 07N+1 与 DataLoader
  8. 08Apollo Client
  9. 09GraphQL vs REST 选型
  10. 10GraphiQL 与 introspection
  11. 11安全与限流
  12. 12综合练习

综合练习

tracks 迷你 Schema + Spring GraphQL 或 Apollo 只读页。

45 分钟

综合练习

动手搭建 tracks 迷你 GraphQL 读 API,与现有 REST /tracks 并存对照。巩固 Schema、Spring GraphQL、可选 Apollo 只读页;不替换 CodeRoadMap 生产主路径。

学习目标

  • 完成从 schema.graphqls 到 @QueryMapping 的闭环
  • 用 GraphiQL 验证与 REST 数据一致
  • (可选)在 Next.js lab 页用 Apollo 展示列表

核心内容

任务 A:Schema(后端 lab profile)

graphql/track.graphqls:

type Track {
  slug: String!
  title: String!
  level: String
  lessonCount: Int
}

type Query {
  tracks: [Track!]!
  track(slug: String!): Track
}

lessonCount 由 @SchemaMapping 或 Service 聚合 cf_lesson 计数——数据源仍 MySQL(spring-boot-essentials JPA)。

任务 B:Controller

@Profile("lab-graphql")
@Controller
public class TrackGraphqlController {
    @QueryMapping
    public List<Track> tracks() { return trackService.findAll(); }

    @QueryMapping
    public Track track(@Argument String slug) {
        return trackService.findBySlug(slug).orElse(null);
    }
}

启动:spring.profiles.active=lab-graphql,访问 GraphiQL。对照:

curl -s http://localhost:8080/tracks | jq length

任务 C:DataLoader(进阶)

为 Track.lessons 增加字段,实现 findByTrackSlugIn 批查,证明无 N+1(gql-nplus1)。

任务 D:前端(可选)

nextjs-essentials 新建 app/lab/graphql-tracks/page.tsx,gql-apollo 只读列表;页脚注明「实验接口,主站用 REST /tracks」。

验收标准

  • GraphQL tracks { slug title } 与 REST GET /tracks 条数一致
  • 生产 profile 未 暴露 /graphiql
  • README 一段说明:为何全站仍 REST

练习

  • 完成 A+B 并截图 GraphiQL 结果
  • 写对比表:本练习 3 个 endpoint 的 REST vs GraphQL 写法
  • 在 /tracks/graphql-essentials 通读本 track 12 课

小结

GraphQL 技能 = 读懂 + 实验 + 理性选型;CodeRoadMap 权威数据仍经 REST /tracks 与 MySQL 交付。 本 track 完结;深入 Spring 见 spring-boot-essentials,前端见 react-essentials 与 nextjs-essentials。

加载本节练习…
← 上一课安全与限流