Skip to main content

GraphQL

tip
  • 主要让 consumer/client 方使用便捷
  • 核心特性/卖点 - 推荐开启
    • GlobalID - ID 包含类型消息,能实现 ID -> __typename
    • Graph Cache - 以 __typename + id 来缓存 - 类似一个内存的 KV 数据库,可以直接查询
      • 配合自定义 resolver 将查询指向缓存 能大量减少查询
      • 需要暴露 __typename, 但是一般影响不大
    • Batch
    • Subscription - EventSource, WebSocket
    • 代码生成
  • 推荐特性
    • partial query - 只返回需要的字段
    • Document Cache - 以查询维度来缓存
    • persisted query - 通过 hash 来缓存查询, 避免暴露整个查询内容
    • schema aware - 直接告诉查询引擎 schema 内容能让查询引擎更加智能
      • 例如 判断 nullable 可以先返回缓存数据再发起查询
    • federation - 多个 schema 合并
    • schema stitching - 多个 schema 合并
# 获取 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

http://graphql.org/code/

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