Cloudflare GraphQL API 具有动态 schema,在 zone 和 account 范围内公开 70 多个数据集。我们不断扩展列表,并用功能更强的替代方案替换现有数据集。
为解决 schema 问题,GraphQL 提供 introspection ↗ 机制。它是 GraphQL 规范的一部分,允许你探索数据集和 field 的图。
Introspection 结果提供所有可用 node 和 field 的概览、描述和弃用状态。
虽然 GraphQL 有 query、subscription 和 mutation 操作,Cloudflare GraphQL API 仅支持 query 操作。
描述中包含给定 node 或 field 暴露的数据详情,还指示是否处于 Beta 模式。Beta node(或 field)用于测试和探索,通常对更高级计划的客户可用。请不要依赖 beta 数据 node,因为它们可能在没有通知的情况下更改或移除。
Introspection 提供弃用状态信息。Cloudflare 使用它通知替换计划。如果提供了 sunset 日期,请在该日期之前迁移到替换 node,以避免任何中断。
某些 node 可能仅对部分用户可用。有关给定 node 的可用性和个人限制,请参阅 settings node。
探索 schema 最便捷的方式是使用文档 explorer,通常是 GraphQL 客户端(如 GraphiQL、Altair 等)的一部分。
或者,你也可以使用带有所需指令的 __schema node 手动执行。
{
__schema {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
fragment InputValue on __InputValue {
name
description
type {
...TypeRef
}
defaultValue
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}有关如何使用 curl 发送 GraphQL 请求的更多详情,请参阅 Execute a GraphQL query with curl。