跳到主要内容

GraphQL Awesome

学习

决策

  • Code first or GraphQL Schema first
    • Code - Code to Schema
      • 可读性好,但是需要手动维护,可以复用现有代码
    • Schema - Schema to Code
      • 生成很多代码,可读性交较差,但是可以快速搭建原型
  • 动态Schema vs 静态Schema
    • 动态
      • 可以根据租户自定义 - 类似动态 CMS
      • 可以根据角色自定义 - conditional schema
    • 静态
      • 可以手写
      • 可以一次性生成
  • DB Schema first or Entity first
    • DB Schema
      • 更好的掌控数据库结构
      • 更好的利用数据库特性
    • Entity
      • 可能考虑支持多数据库
  • Prototype 阶段 还是 Production 阶段
    • Prototype
      • DB -> GraphQL - 只是做简单的接口封装
    • Production
      • 要求 DB 设计合理
      • DB Schema first
      • 预留扩展
  • 是否需要 类型安全
    • 影响怎么做 schema builder
    • 什么程度的 类型安全
      • 全局
      • 输入输出校验
    • 一定程度增加 维护
    • 一定程度增加 写代码 的难度
  • 是否需要 subscriptions
    • 要求感知 修改
  • 是否需要 federation/gateway
  • 是否需要 dataloader
  • 是否需要 relay
    • Node, Connection, clientMutationId

设计

  • GraphQL 最佳实践
    1. 通过 HTTP 提供服务
    2. JSON+Gzip 返回
    3. 对接口进行版本控制 - 利用 deprecated 和 额外字段能
    4. 字段默认 null - 利用 非 null 设计更好的接口
    5. 分页使用 Connection - 通过 first, last, before, after 实现基于 cursor 分页
    6. 服务端批处理和缓存 - 避免 N+1 问题 - 利用 graphql/dataloader 处理
  • Principled GraphQL
    1. One Graph
    2. Federated Implementation
    3. Track the Schema in a Registry
    4. Abstract, Demand-Oriented Schema
    5. Use an Agile Approach to Schema Development
    6. Iteratively Improve Performance
    7. Use Graph Metadata to Empower Developers
    8. Access and Demand Control
    9. Structured Logging
    10. Separate the GraphQL Layer from the Service Layer
  • Gitlab GraphQL API 规范文档

Gateway

Library


Server

Server, Federation, Gateway, Adapter

DevTools

Client

Public GraphQL

GitLab

# 生成 schema
bundle exec rake gitlab:graphql:schema:dump

Github

参考