Updated book for master ***NO_CI***
This commit is contained in:
parent
b906d0ec76
commit
b15f396a56
4 changed files with 24 additions and 20 deletions
|
@ -1230,7 +1230,7 @@ also limited in the data types you can use.</p>
|
||||||
<li>For simple scalars that just wrap a primitive type, you can use the newtype pattern with
|
<li>For simple scalars that just wrap a primitive type, you can use the newtype pattern with
|
||||||
a custom derive.</li>
|
a custom derive.</li>
|
||||||
<li>For more advanced use cases with custom validation, you can use
|
<li>For more advanced use cases with custom validation, you can use
|
||||||
the <code>graphql_scalar!</code> macro.</li>
|
the <code>graphql_scalar</code> proc macro.</li>
|
||||||
</ul>
|
</ul>
|
||||||
<a class="header" href="#built-in-scalars" id="built-in-scalars"><h2>Built-in scalars</h2></a>
|
<a class="header" href="#built-in-scalars" id="built-in-scalars"><h2>Built-in scalars</h2></a>
|
||||||
<p>Juniper has built-in support for:</p>
|
<p>Juniper has built-in support for:</p>
|
||||||
|
@ -1284,7 +1284,7 @@ pub struct UserId(i32);
|
||||||
</code></pre></pre>
|
</code></pre></pre>
|
||||||
<a class="header" href="#custom-scalars" id="custom-scalars"><h2>Custom scalars</h2></a>
|
<a class="header" href="#custom-scalars" id="custom-scalars"><h2>Custom scalars</h2></a>
|
||||||
<p>For more complex situations where you also need custom parsing or validation,
|
<p>For more complex situations where you also need custom parsing or validation,
|
||||||
you can use the <code>graphql_scalar!</code> macro.</p>
|
you can use the <code>graphql_scalar</code> proc macro.</p>
|
||||||
<p>Typically, you represent your custom scalars as strings.</p>
|
<p>Typically, you represent your custom scalars as strings.</p>
|
||||||
<p>The example below implements a custom scalar for a custom <code>Date</code> type.</p>
|
<p>The example below implements a custom scalar for a custom <code>Date</code> type.</p>
|
||||||
<p>Note: juniper already has built-in support for the <code>chrono::DateTime</code> type
|
<p>Note: juniper already has built-in support for the <code>chrono::DateTime</code> type
|
||||||
|
@ -1309,26 +1309,28 @@ purpose.</p>
|
||||||
use juniper::{Value, ParseScalarResult, ParseScalarValue};
|
use juniper::{Value, ParseScalarResult, ParseScalarValue};
|
||||||
use date::Date;
|
use date::Date;
|
||||||
|
|
||||||
juniper::graphql_scalar!(Date where Scalar = <S> {
|
#[juniper::graphql_scalar(description = "Date")]
|
||||||
description: "Date"
|
impl<S> GraphQLScalar for Date
|
||||||
|
where
|
||||||
|
S: ScalarValue
|
||||||
|
{
|
||||||
// Define how to convert your custom scalar into a primitive type.
|
// Define how to convert your custom scalar into a primitive type.
|
||||||
resolve(&self) -> Value {
|
fn resolve(&self) -> Value {
|
||||||
Value::scalar(self.to_string())
|
Value::scalar(self.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define how to parse a primitive type into your custom scalar.
|
// Define how to parse a primitive type into your custom scalar.
|
||||||
from_input_value(v: &InputValue) -> Option<Date> {
|
fn from_input_value(v: &InputValue) -> Option<Date> {
|
||||||
v.as_scalar_value()
|
v.as_scalar_value()
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.and_then(|s| s.parse().ok())
|
.and_then(|s| s.parse().ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define how to parse a string value.
|
// Define how to parse a string value.
|
||||||
from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
|
fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
|
||||||
<String as ParseScalarValue<S>>::from_str(value)
|
<String as ParseScalarValue<S>>::from_str(value)
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
# fn main() {}
|
# fn main() {}
|
||||||
</code></pre></pre>
|
</code></pre></pre>
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -148,7 +148,7 @@ also limited in the data types you can use.</p>
|
||||||
<li>For simple scalars that just wrap a primitive type, you can use the newtype pattern with
|
<li>For simple scalars that just wrap a primitive type, you can use the newtype pattern with
|
||||||
a custom derive.</li>
|
a custom derive.</li>
|
||||||
<li>For more advanced use cases with custom validation, you can use
|
<li>For more advanced use cases with custom validation, you can use
|
||||||
the <code>graphql_scalar!</code> macro.</li>
|
the <code>graphql_scalar</code> proc macro.</li>
|
||||||
</ul>
|
</ul>
|
||||||
<a class="header" href="#built-in-scalars" id="built-in-scalars"><h2>Built-in scalars</h2></a>
|
<a class="header" href="#built-in-scalars" id="built-in-scalars"><h2>Built-in scalars</h2></a>
|
||||||
<p>Juniper has built-in support for:</p>
|
<p>Juniper has built-in support for:</p>
|
||||||
|
@ -202,7 +202,7 @@ pub struct UserId(i32);
|
||||||
</code></pre></pre>
|
</code></pre></pre>
|
||||||
<a class="header" href="#custom-scalars" id="custom-scalars"><h2>Custom scalars</h2></a>
|
<a class="header" href="#custom-scalars" id="custom-scalars"><h2>Custom scalars</h2></a>
|
||||||
<p>For more complex situations where you also need custom parsing or validation,
|
<p>For more complex situations where you also need custom parsing or validation,
|
||||||
you can use the <code>graphql_scalar!</code> macro.</p>
|
you can use the <code>graphql_scalar</code> proc macro.</p>
|
||||||
<p>Typically, you represent your custom scalars as strings.</p>
|
<p>Typically, you represent your custom scalars as strings.</p>
|
||||||
<p>The example below implements a custom scalar for a custom <code>Date</code> type.</p>
|
<p>The example below implements a custom scalar for a custom <code>Date</code> type.</p>
|
||||||
<p>Note: juniper already has built-in support for the <code>chrono::DateTime</code> type
|
<p>Note: juniper already has built-in support for the <code>chrono::DateTime</code> type
|
||||||
|
@ -227,26 +227,28 @@ purpose.</p>
|
||||||
use juniper::{Value, ParseScalarResult, ParseScalarValue};
|
use juniper::{Value, ParseScalarResult, ParseScalarValue};
|
||||||
use date::Date;
|
use date::Date;
|
||||||
|
|
||||||
juniper::graphql_scalar!(Date where Scalar = <S> {
|
#[juniper::graphql_scalar(description = "Date")]
|
||||||
description: "Date"
|
impl<S> GraphQLScalar for Date
|
||||||
|
where
|
||||||
|
S: ScalarValue
|
||||||
|
{
|
||||||
// Define how to convert your custom scalar into a primitive type.
|
// Define how to convert your custom scalar into a primitive type.
|
||||||
resolve(&self) -> Value {
|
fn resolve(&self) -> Value {
|
||||||
Value::scalar(self.to_string())
|
Value::scalar(self.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define how to parse a primitive type into your custom scalar.
|
// Define how to parse a primitive type into your custom scalar.
|
||||||
from_input_value(v: &InputValue) -> Option<Date> {
|
fn from_input_value(v: &InputValue) -> Option<Date> {
|
||||||
v.as_scalar_value()
|
v.as_scalar_value()
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.and_then(|s| s.parse().ok())
|
.and_then(|s| s.parse().ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define how to parse a string value.
|
// Define how to parse a string value.
|
||||||
from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
|
fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
|
||||||
<String as ParseScalarValue<S>>::from_str(value)
|
<String as ParseScalarValue<S>>::from_str(value)
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
# fn main() {}
|
# fn main() {}
|
||||||
</code></pre></pre>
|
</code></pre></pre>
|
||||||
|
|
Loading…
Reference in a new issue