Updated book for master ***NO_CI***
This commit is contained in:
parent
963a0b4bff
commit
cb69da35ea
4 changed files with 128 additions and 16 deletions
|
@ -585,22 +585,78 @@ chapter: <a href="using_contexts.html">Using contexts</a>.</p>
|
|||
<p>Like with the derive attribute, field names will be converted from <code>snake_case</code>
|
||||
to <code>camelCase</code>. If you need to override the conversion, you can simply rename
|
||||
the field. Also, the type name can be changed with an alias:</p>
|
||||
<pre><pre class="playpen"><code class="language-rust">struct Person {
|
||||
name: String,
|
||||
website_url: String,
|
||||
<pre><pre class="playpen"><code class="language-rust">
|
||||
struct Person {
|
||||
}
|
||||
|
||||
/// Doc comments are used as descriptions for GraphQL.
|
||||
#[juniper::object(
|
||||
// With this attribtue you can change the public GraphQL name of the type.
|
||||
name = "PersonObject",
|
||||
// You can also specify a description here, which will overwrite
|
||||
// a doc comment description.
|
||||
description = "...",
|
||||
)]
|
||||
impl Person {
|
||||
fn name(&self) -> &str {
|
||||
self.name.as_str()
|
||||
|
||||
/// A doc comment on the field will also be used for GraphQL.
|
||||
#[graphql(
|
||||
// Or provide a description here.
|
||||
description = "...",
|
||||
)]
|
||||
fn doc_comment(&self) -> &str {
|
||||
""
|
||||
}
|
||||
|
||||
fn websiteURL(&self) -> &str {
|
||||
self.website_url.as_str()
|
||||
// Fields can also be renamed if required.
|
||||
#[graphql(
|
||||
name = "myCustomFieldName",
|
||||
)]
|
||||
fn renamed_field() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
// Deprecations also work as you'd expect.
|
||||
// Both the standard Rust syntax and a custom attribute is accepted.
|
||||
#[deprecated(note = "...")]
|
||||
fn deprecated_standard() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[graphql(deprecated = "...")]
|
||||
fn deprecated_graphql() -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
# fn main() { }
|
||||
</code></pre></pre>
|
||||
<a class="header" href="#customizing-arguments" id="customizing-arguments"><h2>Customizing arguments</h2></a>
|
||||
<p>Method field arguments can also be customized.</p>
|
||||
<p>They can have custom descriptions and default values.</p>
|
||||
<p><strong>Note</strong>: The syntax for this is currently a little awkward.
|
||||
This will become better once the <a href="https://github.com/rust-lang/rust/issues/60406">Rust RFC 2565</a> is implemented.</p>
|
||||
<pre><pre class="playpen"><code class="language-rust">
|
||||
struct Person {}
|
||||
|
||||
#[juniper::object]
|
||||
impl Person {
|
||||
#[graphql(
|
||||
arguments(
|
||||
arg1(
|
||||
// Set a default value which will be injected if not present.
|
||||
// The default can be any valid Rust expression, including a function call, etc.
|
||||
default = true,
|
||||
// Set a description.
|
||||
description = "The first argument..."
|
||||
),
|
||||
arg2(
|
||||
default = 0,
|
||||
)
|
||||
)
|
||||
)]
|
||||
fn field1(&self, arg1: bool, arg2: i32) -> String {
|
||||
format!("{} {}", arg1, arg2)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -191,22 +191,78 @@ chapter: <a href="using_contexts.html">Using contexts</a>.</p>
|
|||
<p>Like with the derive attribute, field names will be converted from <code>snake_case</code>
|
||||
to <code>camelCase</code>. If you need to override the conversion, you can simply rename
|
||||
the field. Also, the type name can be changed with an alias:</p>
|
||||
<pre><pre class="playpen"><code class="language-rust">struct Person {
|
||||
name: String,
|
||||
website_url: String,
|
||||
<pre><pre class="playpen"><code class="language-rust">
|
||||
struct Person {
|
||||
}
|
||||
|
||||
/// Doc comments are used as descriptions for GraphQL.
|
||||
#[juniper::object(
|
||||
// With this attribtue you can change the public GraphQL name of the type.
|
||||
name = "PersonObject",
|
||||
// You can also specify a description here, which will overwrite
|
||||
// a doc comment description.
|
||||
description = "...",
|
||||
)]
|
||||
impl Person {
|
||||
fn name(&self) -> &str {
|
||||
self.name.as_str()
|
||||
|
||||
/// A doc comment on the field will also be used for GraphQL.
|
||||
#[graphql(
|
||||
// Or provide a description here.
|
||||
description = "...",
|
||||
)]
|
||||
fn doc_comment(&self) -> &str {
|
||||
""
|
||||
}
|
||||
|
||||
fn websiteURL(&self) -> &str {
|
||||
self.website_url.as_str()
|
||||
// Fields can also be renamed if required.
|
||||
#[graphql(
|
||||
name = "myCustomFieldName",
|
||||
)]
|
||||
fn renamed_field() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
// Deprecations also work as you'd expect.
|
||||
// Both the standard Rust syntax and a custom attribute is accepted.
|
||||
#[deprecated(note = "...")]
|
||||
fn deprecated_standard() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[graphql(deprecated = "...")]
|
||||
fn deprecated_graphql() -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
# fn main() { }
|
||||
</code></pre></pre>
|
||||
<a class="header" href="#customizing-arguments" id="customizing-arguments"><h2>Customizing arguments</h2></a>
|
||||
<p>Method field arguments can also be customized.</p>
|
||||
<p>They can have custom descriptions and default values.</p>
|
||||
<p><strong>Note</strong>: The syntax for this is currently a little awkward.
|
||||
This will become better once the <a href="https://github.com/rust-lang/rust/issues/60406">Rust RFC 2565</a> is implemented.</p>
|
||||
<pre><pre class="playpen"><code class="language-rust">
|
||||
struct Person {}
|
||||
|
||||
#[juniper::object]
|
||||
impl Person {
|
||||
#[graphql(
|
||||
arguments(
|
||||
arg1(
|
||||
// Set a default value which will be injected if not present.
|
||||
// The default can be any valid Rust expression, including a function call, etc.
|
||||
default = true,
|
||||
// Set a description.
|
||||
description = "The first argument..."
|
||||
),
|
||||
arg2(
|
||||
default = 0,
|
||||
)
|
||||
)
|
||||
)]
|
||||
fn field1(&self, arg1: bool, arg2: i32) -> String {
|
||||
format!("{} {}", arg1, arg2)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue