GraphQL
- 主要让 consumer/client 方使用便捷
- 核心特性/卖点 - 推荐开启
- GlobalID - ID 包含类型消息,能实现 ID ->
__typename
- Graph Cache - 以
__typename
+ id 来缓存 - 类似一个内存的 KV 数据库,可以直接查询- 配合自定义 resolver 将查询指向缓存 能大量减少查询
- 需要暴露
__typename
, 但是一般影响不大
- Batch
- Subscription - EventSource, WebSocket
- 代码生成
- GlobalID - ID 包含类型消息,能实现 ID ->
- 推荐特性
- partial query - 只返回需要的字段
- Document Cache - 以查询维度来缓存
- persisted query - 通过 hash 来缓存查询, 避免暴露整个查询内容
- schema aware - 直接告诉查询引擎 schema 内容能让查询引擎更加智能
- 例如 判断 nullable 可以先返回缓存数据再发起查询
- federation - 多个 schema 合并
- schema stitching - 多个 schema 合并
- 特点
- 面向终端
- 接口数据格式由客户端定义
- 强类型
- GraphQL 规范
- Wikipedia GraphQL
- 实现
- graphql/graphql-js
- A reference implementation of GraphQL for JavaScript
- graphql-java/graphql-java
- GraphQL Java implementation
- neelance/graphql-go
- GraphQL server with a focus on ease of use
- 强类型, 基于反射
- 直接基于 schema 解析
- vektah/gqlgen
- go generate based graphql server library
- HN
- graphql-go/graphql
- An implementation of GraphQL for Go / Golang
- 弱类型, Resolver 返回 interface{}
- 解析的 schema 不能转为代码中的对象
- graphql-python/graphene
- GraphQL framework for Python
- graphene-python.org
- graphql-js/graphene
- graphql/graphql-js
- 工具
- postgraphql/postgraphql
- A GraphQL API created by reflection over a PostgreSQL schema.
- 2fd/graphdoc
- apollographql/graphql-syntax
- A catalog of different packages and syntaxes to generate a GraphQL-JS schema
- graphql/graphiql
- An in-browser IDE for exploring GraphQL
- google/rejoiner
- postgraphql/postgraphql
- 接口参考
- Facebook Graph API
- GitHubGraphQL API v4
- 接口地址 https://api.github.com/graphql
- github 文档
- shopify 文档
- https://graphcms.com/docs/api_simple/
- 参考
- Improve GraphQL Performance with Automatic Persisted Queries
- 优化查询大小
- 使用一个 Hash 值作为查询标识
- The future of state management
- 使用 GraphQL 来管理应用状态
- GraphQL.g4
- 语法参考
- Designing GraphQL Mutations
- 设计修改操作
- GraphQL schema stitching
- 合并多个 Schema
- A guide to authentication in GraphQL
- 认证
- 识别用户身份
- Authorization in GraphQL
- 鉴权
- 判断用户是否进行相关操作
- Reusable GraphQL schema directives
- Principled GraphQL
- Improve GraphQL Performance with Automatic Persisted Queries
- Relay
- facebook/relay
- Learn Relay
- What is a Query?
- The node and viewer fields
- Pagination
- 直接返回数组
- 返回数组接收翻页参数
- 返回链接
collections(first: Int!, after: String, reverse: Boolean): CollectionConnection!
followers(first: Int, after: String, last: Int, before: String): FollowerConnection!
- 客户端
- Apollo React
- prisma-labs/graphql-request
- 非常简单的客户端 - 配合 react-query 使用
- react-query vs apollo client
# 获取 Schema
npm install -g graphqurl
gq https://my-graphql-engine.com/v1/graphql --introspect > schema.graphql
gq https://my-graphql-engine.com/v1/graphql -H 'X-Hasura-Admin-Secret: adminsecretkey' --introspect > schema.graphql
gq https://my-graphql-engine.com/v1/graphql --introspect --format json > schema.json
# https://github.com/apollographql/apollo-tooling
# schema.json
npm install -g apollo
apollo schema:download --endpoint https://my-graphql-engine.com/v1/graphql
apollo schema:download --endpoint https://my-graphql-engine.com/v1/graphql --header 'X-Hasura-Admin-Secret: adminsecretkey'
Apollo
FAQ
N + 1
DateTime
vs REST
TBD
https://github.com/skevy/graphiql-app Light, Electron-based Wrapper around GraphiQL
https://github.com/apollographql/apollo-server
https://github.com/graphcool/graphql-playground GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration)
docker run -p 5000:5000 postgraphql/postgraphql --connection postgres://POSTGRES_USER:POSTGRES_PASSWORD@POSTGRES_HOST:POSTGRES_PORT/POSTGRES_SCHEMA
https://github.com/chentsulin/awesome-graphql Awesome list of GraphQL & Relay
https://www.graphql.com/ http://graphql.org/learn/best-practices/ GraphQL Best Practices
https://www.apollographql.com/ Apollo provides a universal GraphQL API on top of your existing services, so you can build new application features fast without waiting on backend changes.
https://github.com/facebook/dataloader DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.
https://blog.datank.ai/graphql-grpc-part-1-54d92a109619 GraphQL & gRPC
relay
global id
- https://docs.github.com/cn/graphql/guides/using-global-node-ids
- github ID 格式 -
04:User583231
- New global ID format coming to GraphQL
- github ID 格式 -
- 支持用于接口处理 -
labelableId: ID! @possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType: "Labelable")
- Global Object Identification
- graphile/global-ids