Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to generic ScalarValue in #[graphql_object] macro #779

Merged
merged 33 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
fa9af3a
Change codegen ScalarValue defaults for #[graphql_object] macro
tyranron Oct 8, 2020
a8a216c
Fix integration tests
tyranron Oct 8, 2020
1173788
Fix codegen failure tests
tyranron Oct 8, 2020
a70031a
Fix 'juniper' crate tests
tyranron Oct 8, 2020
c711a21
Fix integration crates tests
tyranron Oct 8, 2020
f56364c
Fix 'juniper_benchmarks' crate
tyranron Oct 8, 2020
6a9a2c0
Fix examples
tyranron Oct 9, 2020
85be2e9
Fix Book
tyranron Oct 17, 2020
bc992c6
Merge branch 'master' into fix-generic-scalar-in-objects
tyranron Oct 17, 2020
8001961
Merge branch 'master' into fix-generic-scalar-in-objects
tyranron Oct 19, 2020
86d768b
Fix
tyranron Oct 19, 2020
9c245c6
Merge branch 'master' into fix-generic-scalar-in-objects
tyranron Oct 19, 2020
62fb877
Merge branch 'master' into fix-generic-scalar-in-objects
tyranron Oct 20, 2020
efb53c2
Add CHANGELOG entry
tyranron Oct 20, 2020
fa065c7
Some Book corrections
tyranron Oct 20, 2020
365fbf6
Fix
tyranron Oct 20, 2020
4f20e29
Bootstrap coercion machinery
tyranron Oct 21, 2020
c45104c
Reimpl coercion
tyranron Oct 22, 2020
6febfa1
Correct tests, vol.1
tyranron Oct 22, 2020
3c04980
Merge branch 'master' into fix-generic-scalar-in-objects
tyranron Oct 23, 2020
bec36b6
Merge branch 'master' into fix-generic-scalar-in-objects
tyranron Oct 28, 2020
b050a2f
Correct tests, vol.2
LegNeato Oct 28, 2020
abb045e
Correct tests, vol.3
LegNeato Oct 29, 2020
c3075c8
Correct tests, vol.4
tyranron Oct 29, 2020
110542c
Merge remote-tracking branch 'upstream/fix-generic-scalar-in-objects'…
tyranron Oct 29, 2020
17ed26b
Correct tests, vol.5
tyranron Oct 29, 2020
c0b771a
Fix coercion for subscriptions
tyranron Oct 29, 2020
d7c6b6f
Merge branch 'master' into fix-generic-scalar-in-objects
LegNeato Oct 29, 2020
f4740cf
Merge branch 'master' into fix-generic-scalar-in-objects
tyranron Oct 30, 2020
0bc0efe
Merge remote-tracking branch 'upstream/fix-generic-scalar-in-objects'…
tyranron Oct 30, 2020
a0f5256
README fixes
tyranron Oct 30, 2020
41b55c7
Merge branch 'master' into fix-generic-scalar-in-objects
LegNeato Nov 4, 2020
5c3c0c0
Merge branch 'master' into fix-generic-scalar-in-objects
LegNeato Nov 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/book/content/advanced/introspection.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ result can then be converted to JSON for use with tools and libraries such as
# #![allow(unused_variables)]
# extern crate juniper;
# extern crate serde_json;
use juniper::{EmptyMutation, EmptySubscription, FieldResult, IntrospectionFormat};
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
GraphQLObject, IntrospectionFormat,
};

// Define our schema.

#[derive(juniper::GraphQLObject)]
#[derive(GraphQLObject)]
struct Example {
id: String,
}
Expand All @@ -47,9 +50,7 @@ impl juniper::Context for Context {}

struct Query;

#[juniper::graphql_object(
Context = Context,
)]
#[graphql_object(context = Context)]
impl Query {
fn example(id: String) -> FieldResult<Example> {
unimplemented!()
Expand Down
9 changes: 5 additions & 4 deletions docs/book/content/advanced/non_struct_objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ errors from a mutation:

```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject};
# #[derive(juniper::GraphQLObject)] struct User { name: String }

#[derive(juniper::GraphQLObject)]
#
#[derive(GraphQLObject)]
struct ValidationError {
field: String,
message: String,
Expand All @@ -24,7 +25,7 @@ enum SignUpResult {
Error(Vec<ValidationError>),
}

#[juniper::graphql_object]
#[graphql_object]
impl SignUpResult {
fn user(&self) -> Option<&User> {
match *self {
Expand All @@ -40,7 +41,7 @@ impl SignUpResult {
}
}
}

#
# fn main() {}
```

Expand Down
29 changes: 16 additions & 13 deletions docs/book/content/advanced/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sequentially:
# extern crate juniper;
# extern crate juniper_subscriptions;
# extern crate tokio;
# use juniper::FieldError;
# use juniper::{graphql_object, graphql_subscription, FieldError};
# use futures::Stream;
# use std::pin::Pin;
#
Expand All @@ -38,7 +38,7 @@ sequentially:
# impl juniper::Context for Database {}

# pub struct Query;
# #[juniper::graphql_object(Context = Database)]
# #[graphql_object(context = Database)]
# impl Query {
# fn hello_world() -> &str {
# "Hello World!"
Expand All @@ -48,7 +48,7 @@ pub struct Subscription;

type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;

#[juniper::graphql_subscription(Context = Database)]
#[graphql_subscription(context = Database)]
impl Subscription {
async fn hello_world() -> StringStream {
let stream = tokio::stream::iter(vec![
Expand All @@ -58,8 +58,9 @@ impl Subscription {
Box::pin(stream)
}
}
#
# fn main () {}
```
```



Expand All @@ -84,8 +85,12 @@ where [`Connection`][Connection] is a `Stream` of values returned by the operati
# extern crate juniper_subscriptions;
# extern crate serde_json;
# extern crate tokio;
# use juniper::http::GraphQLRequest;
# use juniper::{DefaultScalarValue, EmptyMutation, FieldError, RootNode, SubscriptionCoordinator};
# use juniper::{
# http::GraphQLRequest,
# graphql_object, graphql_subscription,
# DefaultScalarValue, EmptyMutation, FieldError,
# RootNode, SubscriptionCoordinator,
# };
# use juniper_subscriptions::Coordinator;
# use futures::{Stream, StreamExt};
# use std::pin::Pin;
Expand All @@ -103,7 +108,7 @@ where [`Connection`][Connection] is a `Stream` of values returned by the operati
#
# pub struct Query;
#
# #[juniper::graphql_object(Context = Database)]
# #[graphql_object(context = Database)]
# impl Query {
# fn hello_world() -> &str {
# "Hello World!"
Expand All @@ -114,7 +119,7 @@ where [`Connection`][Connection] is a `Stream` of values returned by the operati
#
# type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
#
# #[juniper::graphql_subscription(Context = Database)]
# #[graphql_subscription(context = Database)]
# impl Subscription {
# async fn hello_world() -> StringStream {
# let stream =
Expand All @@ -132,11 +137,9 @@ async fn run_subscription() {
let schema = schema();
let coordinator = Coordinator::new(schema);
let req: GraphQLRequest<DefaultScalarValue> = serde_json::from_str(
r#"
{
r#"{
"query": "subscription { helloWorld }"
}
"#,
}"#,
)
.unwrap();
let ctx = Database::new();
Expand All @@ -145,7 +148,7 @@ async fn run_subscription() {
println!("{}", serde_json::to_string(&result).unwrap());
}
}

#
# fn main() { }
```

Expand Down
56 changes: 31 additions & 25 deletions docs/book/content/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,28 @@ resolvers, which you will use for the `Query` and `Mutation` roots.
```rust
# #![allow(unused_variables)]
# extern crate juniper;
use juniper::{FieldResult, EmptySubscription};

# use std::fmt::Display;
use juniper::{
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
GraphQLInputObject, GraphQLObject, ScalarValue,
};
#
# struct DatabasePool;
# impl DatabasePool {
# fn get_connection(&self) -> FieldResult<DatabasePool> { Ok(DatabasePool) }
# fn find_human(&self, _id: &str) -> FieldResult<Human> { Err("")? }
# fn insert_human(&self, _human: &NewHuman) -> FieldResult<Human> { Err("")? }
# }

#[derive(juniper::GraphQLEnum)]
#[derive(GraphQLEnum)]
enum Episode {
NewHope,
Empire,
Jedi,
}

#[derive(juniper::GraphQLObject)]
#[graphql(description="A humanoid creature in the Star Wars universe")]
#[derive(GraphQLObject)]
#[graphql(description = "A humanoid creature in the Star Wars universe")]
struct Human {
id: String,
name: String,
Expand All @@ -55,8 +59,8 @@ struct Human {

// There is also a custom derive for mapping GraphQL input objects.

#[derive(juniper::GraphQLInputObject)]
#[graphql(description="A humanoid creature in the Star Wars universe")]
#[derive(GraphQLInputObject)]
#[graphql(description = "A humanoid creature in the Star Wars universe")]
struct NewHuman {
name: String,
appears_in: Vec<Episode>,
Expand All @@ -78,14 +82,13 @@ impl juniper::Context for Context {}

struct Query;

#[juniper::graphql_object(
#[graphql_object(
// Here we specify the context type for the object.
// We need to do this in every type that
// needs access to the context.
Context = Context,
context = Context,
)]
impl Query {

fn apiVersion() -> &str {
"1.0"
}
Expand All @@ -109,22 +112,26 @@ impl Query {

struct Mutation;

#[juniper::graphql_object(
Context = Context,
)]
impl Mutation {
#[graphql_object(
context = Context,

fn createHuman(context: &Context, new_human: NewHuman) -> FieldResult<Human> {
let db = context.pool.get_connection()?;
let human: Human = db.insert_human(&new_human)?;
// If we need to use `ScalarValue` parametrization explicitly somewhere
// in the object definition (like here in `FieldResult`), we should
// declare an explicit type parameter for that, and specify it.
scalar = S,
)]
impl<S: ScalarValue + Display> Mutation {
fn createHuman(context: &Context, new_human: NewHuman) -> FieldResult<Human, S> {
let db = context.pool.get_connection().map_err(|e| e.map_scalar_value())?;
let human: Human = db.insert_human(&new_human).map_err(|e| e.map_scalar_value())?;
Ok(human)
}
}

// A root schema consists of a query, a mutation, and a subscription.
// Request queries can be executed against a RootNode.
type Schema = juniper::RootNode<'static, Query, Mutation, EmptySubscription<Context>>;

#
# fn main() {
# let _ = Schema::new(Query, Mutation{}, EmptySubscription::new());
# }
Expand All @@ -143,10 +150,12 @@ You can invoke `juniper::execute` directly to run a GraphQL query:
```rust
# // Only needed due to 2018 edition because the macro is not accessible.
# #[macro_use] extern crate juniper;
use juniper::{FieldResult, Variables, EmptyMutation, EmptySubscription};

use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
GraphQLEnum, Variables,
};

#[derive(juniper::GraphQLEnum, Clone, Copy)]
#[derive(GraphQLEnum, Clone, Copy)]
enum Episode {
NewHope,
Empire,
Expand All @@ -160,16 +169,13 @@ impl juniper::Context for Ctx {}

struct Query;

#[juniper::graphql_object(
Context = Ctx,
)]
#[graphql_object(context = Ctx)]
impl Query {
fn favoriteEpisode(context: &Ctx) -> FieldResult<Episode> {
Ok(context.0)
}
}


// A root schema consists of a query, a mutation, and a subscription.
// Request queries can be executed against a RootNode.
type Schema = juniper::RootNode<'static, Query, EmptyMutation<Ctx>, EmptySubscription<Ctx>>;
Expand Down
26 changes: 14 additions & 12 deletions docs/book/content/schema/schemas_and_mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ object in Juniper, most commonly using the `graphql_object` proc macro:
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# use juniper::FieldResult;
# #[derive(juniper::GraphQLObject)] struct User { name: String }
# use juniper::{graphql_object, FieldResult, GraphQLObject};
# #[derive(GraphQLObject)] struct User { name: String }
struct Root;

#[juniper::graphql_object]
#[graphql_object]
impl Root {
fn userWithUsername(username: String) -> FieldResult<Option<User>> {
// Look up user in database...
# unimplemented!()
# unimplemented!()
}
}

#
# fn main() { }
```

Expand All @@ -47,18 +47,18 @@ that performs some mutating side-effect such as updating a database.
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# use juniper::FieldResult;
# #[derive(juniper::GraphQLObject)] struct User { name: String }
# use juniper::{graphql_object, FieldResult, GraphQLObject};
# #[derive(GraphQLObject)] struct User { name: String }
struct Mutations;

#[juniper::graphql_object]
#[graphql_object]
impl Mutations {
fn signUpUser(name: String, email: String) -> FieldResult<User> {
// Validate inputs and save user in database...
# unimplemented!()
# unimplemented!()
}
}

#
# fn main() { }
```

Expand All @@ -68,11 +68,13 @@ Many tools in the GraphQL ecosystem require the schema to be defined in the [Gra

```rust
# extern crate juniper;
use juniper::{FieldResult, EmptyMutation, EmptySubscription, RootNode};
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult, RootNode,
};

struct Query;

#[juniper::graphql_object]
#[graphql_object]
impl Query {
fn hello(&self) -> FieldResult<&str> {
Ok("hello world")
Expand Down
Loading