Apollo Kotlin 中的 GraphQL 文件类型
Apollo Kotlin 支持以下文件类型用于 GraphQL 定义:
要了解更多关于 SDL 模式的信息,请参阅有关 Apollo Kotlin 中的 SDL 支持的 博客文章。
模式
一个 模式 描述了一个图的数据类型以及这些类型之间的关系。Apollo Kotlin 支持通过内省内省获得的 JSON 模式,以及传统的 SDL 模式。
内省模式(.json)
您可以通过一个特殊的 GraphQL 服务器 内省查询(假设服务器启用了 内省)来获得 GraphQL 服务器的模式。与任何其他 GraphQL 服务器响应一样,返回的模式以类似于下面的 JSON 格式提供:
{"data": {"__schema": {"queryType": {"name": "Query"},"mutationType": {"name": "Mutation"},"types": [...]}}}
一些 JSON 架构省略了顶层data 字段。 Apollo Kotlin支持这种省略。
像Apollo 空间站这样的工具可以自动分析 GraphQL 服务器,并允许您以 JSON(或 SDL)格式下载其架构。Apollo Kotlin也可以使用以下 Gradle 任务下载分析过的架构: downloadApolloSchema:
./gradlew :app:downloadApolloSchema \--endpoint "https://example.com/graphql" \--schema schema.json
JSON introspection schemas are verbose and difficult to read or modify. For simplicity, you should consider JSON schemas read-only and convert them to SDL schemas if you need to make changes.
SDL schemas (.graphqls)
SDL (Schema Definition Language) is the canonical language for defining GraphQL schemas as defined in the GraphQL spec. It's much cleaner and more expressive than JSON for understanding a schema's structure. It also supports directives, unlike the JSON representation (see this issue):
type schema {query: Querymutation: Mutation}type Query {field: String @deprecated...}
您无法直接从 GraphQL 端点下载 SDL Schema,但 Apollo Kotlin 可以在指定 --schema 选项中的 .graphqls 扩展名后自动将 introspection schema 转换为 SDL:
./gradlew :app:downloadApolloSchema \--endpoint "https://example.com/graphql" \--schema schema.graphqls
Apollo Kotlin 还支持 .sdl 文件扩展名用于 SDL Schema,但很少有其他工具使用 .sdl。您随后应继续使用 .graphqls。
操作(.graphql)
GraphQL 操作是针对图(graph)来获取或修改数据的。
在 Apollo Kotlin 中,operation 文件使用 .graphql(不带单引号)扩展名。这些文件只包含可执行定义(查询、指令、订阅和/或片段):
query MyQuery {field1field2...}
Apollo Kotlin 将这些操作编译成类型安全的模型,以便您在运行时使用。