24 个稳定版本
3.7.3 | 2024 年 6 月 4 日 |
---|---|
3.7.2 | 2024 年 5 月 22 日 |
3.7.0 | 2024 年 4 月 28 日 |
3.5.0 | 2024 年 3 月 25 日 |
3.0.0-beta.3 | 2023 年 5 月 13 日 |
#579 in HTTP 服务器
4,273 每月下载量
在 cynic-cli 中使用
555KB
13K SLoC
Cynic Introspection
cynic-introspection
定义了一个 GraphQL 自省查询,可以使用 cynic
(一个 Rust GraphQL 客户端)来运行。
这可以用于任何您想要自省 GraphQL 服务器的原因——包括您在自己的项目中作为库使用 cynic 时。
特性
- 支持自省同时支持 GraphQL 2021 和 GraphQL 2018 的服务器。
- 包含一个功能检测查询,可以在查询之前确定服务器支持 GraphQL 规范的哪个版本。
用法
lib.rs
:
cynic-introspection
定义了一个 GraphQL 自省查询,可以使用 cynic
(一个 Rust GraphQL 客户端)来运行。
这可以用于任何您想要自省 GraphQL 服务器的原因——包括您在自己的项目中作为库使用 cynic 时。
它还提供了一个基于自省查询结果的 [Schema] 抽象,这比直接使用自省结果提供了更强的类型。
use cynic::{QueryBuilder, http::ReqwestBlockingExt};
use cynic_introspection::IntrospectionQuery;
// We can run an introspection query and unwrap the data contained within
let introspection_data = reqwest::blocking::Client::new()
.post("https://swapi-graphql.netlify.app/.netlify/functions/index")
.run_graphql(IntrospectionQuery::build(()))
.unwrap()
.data
.unwrap();
// And then convert it into a schema for easier use.
let schema = introspection_data.into_schema().unwrap();
assert_eq!(schema.query_type, "Root");
GraphQL 版本
当前 GraphQL 服务器通常支持两种不同的 GraphQL 规范版本
这两个版本的自省字段不同。默认情况下,我们只查询支持 2018 年 6 月规范的字段。您可以使用 InstrospectionQuery::with_capabilities 请求不同的查询版本。
use cynic::http::ReqwestBlockingExt;
use cynic_introspection::{IntrospectionQuery, SpecificationVersion};
// We can run an introspection query and unwrap the data contained within
let introspection_data = reqwest::blocking::Client::new()
.post("https://spacex-production.up.railway.app/")
.run_graphql(
IntrospectionQuery::with_capabilities(
SpecificationVersion::October2021.capabilities()
)
)
.unwrap()
.data
.unwrap();
// And then convert it into a schema for easier use.
let schema = introspection_data.into_schema().unwrap();
assert_eq!(schema.query_type, "Query");
检测功能
cynic-introspection
还提供了 [CapabilitiesQuery],这是一个可以确定远程 GraphQL 服务器功能的查询。它可以与 Introspection::with_capabilities
配对。
use cynic::{QueryBuilder, http::ReqwestBlockingExt};
use cynic_introspection::{CapabilitiesQuery, IntrospectionQuery};
// First we run a capabilites query to check what the server supports
let capabilities = reqwest::blocking::Client::new()
.post("https://swapi-graphql.netlify.app/.netlify/functions/index")
.run_graphql(CapabilitiesQuery::build(()))
.unwrap()
.data
.unwrap()
.capabilities();
// Now we can safely run introspection, only querying for what the server supports.
let introspection_data = reqwest::blocking::Client::new()
.post("https://swapi-graphql.netlify.app/.netlify/functions/index")
.run_graphql(IntrospectionQuery::with_capabilities(capabilities))
.unwrap()
.data
.unwrap();
// And then convert it into a schema for easier use.
let schema = introspection_data.into_schema().unwrap();
assert_eq!(schema.query_type, "Root");
依赖项
~5.5MB
~84K SLoC