Skip to content

Commit

Permalink
Update Juniper to 0.16 (#1298)
Browse files Browse the repository at this point in the history
Closes #1198 

See commit messages. `schema.graphql` certainly shouldn't be hand
reviewed anymore. I hope the remaining diff isn't too terrible to
review.
  • Loading branch information
owi92 authored Dec 12, 2024
2 parents fffda02 + ad5c98a commit cfbce9f
Show file tree
Hide file tree
Showing 16 changed files with 734 additions and 946 deletions.
123 changes: 34 additions & 89 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ hyper-rustls = { version = "0.26.0", features = ["http2"] }
hyper-util = { version = "0.1.3", features = ["client", "server", "http1", "http2"] }
isahc = { version = "1", features = ["static-ssl"] }
iso8601 = "0.6.1"
juniper = { version = "0.15.10", default-features = false, features = ["chrono", "schema-language"] }
juniper = { version = "0.16.1", default-features = false, features = ["chrono", "schema-language", "anyhow", "backtrace"] }
libz-sys = { version = "1", features = ["static"] }
meilisearch-sdk = "0.24.3"
mime_guess = { version = "2", default-features = false }
Expand Down
54 changes: 23 additions & 31 deletions backend/src/api/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bincode::Options;
use juniper::{GraphQLScalar, InputValue, ScalarValue};
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -59,7 +60,11 @@ super::util::impl_object_with_dummy_field!(NotAllowed);
/// serialization format from `bincode`, a compact binary serializer. Of course
/// we could also have serialized it as JSON and base64 encoded it then, but
/// that would be a waste of network bandwidth.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, GraphQLScalar)]
#[graphql(
description = "An opaque cursor used for pagination",
parse_token(String),
)]
pub(crate) struct Cursor(String);

impl Cursor {
Expand All @@ -85,39 +90,32 @@ impl Cursor {
.deserialize_from(b64reader)
.map_err(|e| err::invalid_input!("given cursor is invalid: {}", e))
}
}

#[juniper::graphql_scalar(
name = "Cursor",
description = "An opaque cursor used for pagination",
)]
impl<S> GraphQLScalar for Cursor
where
S: juniper::ScalarValue,
{
fn resolve(&self) -> juniper::Value {
fn to_output<S: ScalarValue>(&self) -> juniper::Value<S> {
juniper::Value::scalar(self.0.clone())
}

fn from_input_value(value: &juniper::InputValue) -> Option<Self> {
value.as_string_value().map(|s| Self(s.into()))
}

fn from_str<'a>(value: juniper::ScalarToken<'a>) -> juniper::ParseScalarResult<'a, S> {
<String as juniper::ParseScalarValue<S>>::from_str(value)
fn from_input<S: ScalarValue>(input: &InputValue<S>) -> Result<Self, String> {
let s = input.as_string_value().ok_or("expected string")?;
Ok(Self(s.into()))
}
}


// TODO: This uses `graphql_scalar` instead of `derive(GraphQLScalar)` because
// the type `ExtraMetadata` is defined in the `db` module and adding GraphQL
// code there seems wrong. However, I feel like we should move some types
// around anyway since we encountered problems like this before.
#[juniper::graphql_scalar(
name = "ExtraMetadata",
description = "Arbitrary metadata for events/series. Serialized as JSON object.",
with = Self,
parse_token(String),
)]
impl<S> GraphQLScalar for ExtraMetadata
where
S: juniper::ScalarValue
{
fn resolve(&self) -> juniper::Value {
#[allow(dead_code)]
pub type ApiExtraMetadata = ExtraMetadata;

impl ExtraMetadata {
fn to_output<S: ScalarValue>(&self) -> juniper::Value<S> {
use juniper::Value;

std::iter::once(("dcterms", &self.dcterms))
Expand All @@ -138,16 +136,10 @@ where
.pipe(Value::Object)
}

fn from_input_value(value: &juniper::InputValue) -> Option<Self> {
fn from_input<S: ScalarValue>(input: &InputValue<S>) -> Result<Self, String> {
// I did not want to waste time implementing this now, given that we
// likely never use it.
let _ = value;
let _ = input;
todo!("ExtraMetadata cannot be used as input value yet")
}

fn from_str<'a>(value: juniper::ScalarToken<'a>) -> juniper::ParseScalarResult<'a, S> {
// See `from_input_value`
let _ = value;
todo!()
}
}
Loading

0 comments on commit cfbce9f

Please sign in to comment.