Add some docs

This commit is contained in:
sunli 2020-04-19 10:28:52 +08:00
parent c233192545
commit 776719743c
7 changed files with 30 additions and 8 deletions

View File

@ -25,7 +25,6 @@
- [Actix-web](integrations_to_actix_web.md)
- [Advanced topics](advanced_topics.md)
- [Custom scalars](custom_scalars.md)
- [Custom input value validators](custom_input_value_validators.md)
- [Custom extensions](custom_extensions.md)
- [Custom subscription transport](custom_subscription_transport.md)
- [Apollo Federation](apollo_federation.md)

View File

@ -1 +0,0 @@
# Custom input value validators

View File

@ -24,7 +24,6 @@
- [Actix-web](integrations_to_actix_web.md)
- [高级主题](advanced_topics.md)
- [自定义标量](custom_scalars.md)
- [自定义输入值校验器](custom_input_value_validators.md)
- [自定义扩展](custom_extensions.md)
- [自定义订阅传输协议](custom_subscription_transport.md)
- [Apollo Federation集成](apollo_federation.md)

View File

@ -1 +0,0 @@
# 自定义输入值校验器

View File

@ -37,4 +37,30 @@ impl Query {
} {
}
}
```
```
## 自定义校验器
```rust
struct MustBeZero {}
impl InputValueValidator for InputValueValidator {
fn is_valid(&self, value: &Value) -> Option<String> {
if let Value::Int(n) = value {
if n.as_i64().unwrap() != 0 {
// 校验失败
Some(format!(
"the value is {}, but must be zero",
n.as_i64().unwrap(),
))
} else {
// 校验通过
None
}
} else {
// 类型不匹配直接返回None内置校验器会发现这个错误
None
}
}
}
```

View File

@ -72,7 +72,7 @@ impl InputValueValidator for IntGreaterThan {
None
}
} else {
Some("expected type \"Int\"".to_string())
None
}
}
}
@ -92,7 +92,7 @@ impl InputValueValidator for IntNonZero {
None
}
} else {
Some("expected type \"Int\"".to_string())
None
}
}
}

View File

@ -20,7 +20,7 @@ impl InputValueValidator for ListMinLength {
None
}
} else {
Some("expected type \"List\"".to_string())
None
}
}
}