Comments (4)
Usually, you shouldn't have to deal with Value
enums directly. Unless I'm misinterpreting something, your example seems to be creating a complex scalar object, something that's probably better to implement as an actual object since you can't make selections on scalars.
I would rather write your example as:
struct Movie {
release_date: Option<String>,
}
graphql_object!(Movie: () |&self| {
field release_date() -> Option<&String> { self.release_date.as_ref() }
});
from juniper.
Perhaps my example was a bit weak. Consider you have some GUI application that receives n
CSS parameters. In order to construct a completed view, all n
parameters must be queried, so in my opinion, there isn't a lot of value to being able to select the specific fields because there is no situation in this case where I wouldn't need to read all the fields in. The main advantage to doing something like this is that it reduces the body length significantly especially if n
is a large number. The downside would be that there might be more processing and memory usage on the server side.
Also, as a side point, assume that you cannot serialize these parameters and save them locally on the machine (which would be a preferable solution here) in which case you serve a path to the file via graphql and then have nginx or some file server/cdn deliver the content.
To me at least sacrificing server resources for a more easily built query string is worth it. However, a nullable enum would be needed.
from juniper.
I see.
Null values are supported - just use Value::Null
. It seems like you want a convenience method like:
fn optional_value(v: Option<Value>) {
v.unwrap_or(Value::Null)
}
Or something more generic that can take something that implements Into<Value>
?
from juniper.
tl;dr I don't think there needs to be any alteration to the crate.
Hmmm, yeah, perhaps I was overcomplicating things. At worst case, I will have to clone some things due to the &self being enforced in the resolve function. However, generally speaking, the issue can be solved generally like so...
struct Movie {
release_date: OptionString>
}
graphql_scalar!(Movie {
description: "Describes a movie."
resolve(&self) -> Value {
Value::Object(self.into())
}
....
}
// Untested
impl<'a> Into<HashMap<String, Value>> for &'a Movie {
fn into(self) -> HashMap<String, Value> {
let mut object = HashMap::new();
object.insert(
self.release_date.clone().map(|x| Value::String(x))
).unwrap_or(Value::Null);
return object;
}
}
Then again I might be able to just leverage the encode function from rustc_serialize and spit out a string that way, but I would argue it might make more sense for the end user to treat the scalar JSON object instead of a string. Alternatively, if I leveraged serde I could convert the &Movie to a serde::Value and then convert those values into the juniper values.
To be honest, I am starting to like the idea of just making the body longer and using the graphql object macro regardless of the size of n a lot more.
from juniper.
Related Issues (20)
- Generated code fails the `clippy::str_to_string` lint HOT 3
- Interface not working as expected with imlp HOT 3
- Expected query, got subscription. HOT 3
- `juniper_actix` does not compile with the `subscription` feature enabled HOT 2
- Old version of book is still online HOT 1
- operation_name not being set in juniper_hyper library HOT 2
- I'm having issues with App Data configuration
- Looking for Help & Community HOT 2
- `juniper_warp::make_graphql_filter` improperly returns 4xx on some `context_extractor` 5xx errors HOT 5
- Gitter link resolves to 404 HOT 1
- A better way to keep graphiql updated HOT 1
- Example to integrate with Axum HOT 7
- Subscription argumennt Error using juniper_graphql_transport_ws HOT 2
- GraphQLInputObject macro fails if crate has a local type alias named "Result"
- Create new release HOT 7
- expected tuple struct or tuple variant, found function `Ok` HOT 2
- Input type, determine if key set or unset HOT 2
- `#[graphql_object]` hides syntax errors HOT 2
- GraphQL Union types on generic Rust enums
- Cannot get "Foreign For implementing custom scalars on foreign types there is #[graphql_scalar] attribute." working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from juniper.