Skip to content

Commit

Permalink
feat: use ArcStr for storing strings in Schema
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Schema types are not lifetime-generic anymore
  • Loading branch information
audunhalland committed Apr 7, 2024
1 parent 9c4ea32 commit ae83c65
Show file tree
Hide file tree
Showing 100 changed files with 1,112 additions and 1,091 deletions.
3 changes: 1 addition & 2 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ impl Query {
}
}

pub fn new_schema() -> RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>
{
pub fn new_schema() -> RootNode<Query, EmptyMutation<Context>, EmptySubscription<Context>> {
RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new())
}

Expand Down
4 changes: 2 additions & 2 deletions book/src/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Mutation {

// 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>>;
type Schema = juniper::RootNode<Query, Mutation, EmptySubscription<Context>>;
#
# fn main() {
# _ = Schema::new(Query, Mutation, EmptySubscription::new());
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Query {
}
}

type Schema = juniper::RootNode<'static, Query, EmptyMutation<Ctx>, EmptySubscription<Ctx>>;
type Schema = juniper::RootNode<Query, EmptyMutation<Ctx>, EmptySubscription<Ctx>>;

fn main() {
// Create a context.
Expand Down
4 changes: 2 additions & 2 deletions book/src/schema/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Mutation {
}
}

type Schema = RootNode<'static, Query, Mutation, EmptySubscription>;
type Schema = RootNode<Query, Mutation, EmptySubscription>;
#
# fn main() {}
```
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Query {
}
}

type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>;
type Schema = RootNode<Query, EmptyMutation, EmptySubscription>;

fn main() {
// Run the built-in introspection query.
Expand Down
2 changes: 1 addition & 1 deletion book/src/schema/introspection.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Query {
}
}

type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>;
type Schema = RootNode<Query, EmptyMutation, EmptySubscription>;

fn main() {
let schema = Schema::new(Query, EmptyMutation::new(), EmptySubscription::new())
Expand Down
2 changes: 1 addition & 1 deletion book/src/schema/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ While we can implement [`SubscriptionCoordinator`] ourselves, [Juniper] contains
# }
# }
#
type Schema = RootNode<'static, Query, EmptyMutation<Database>, Subscription>;
type Schema = RootNode<Query, EmptyMutation<Database>, Subscription>;

fn schema() -> Schema {
Schema::new(Query, EmptyMutation::new(), Subscription)
Expand Down
2 changes: 1 addition & 1 deletion juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ uuid = ["dep:uuid"]

[dependencies]
anyhow = { version = "1.0.47", optional = true }
arcstr = "1"
async-trait = "0.1.39"
auto_enums = "0.8"
bigdecimal = { version = "0.4", optional = true }
Expand All @@ -57,7 +58,6 @@ rust_decimal = { version = "1.20", default-features = false, optional = true }
ryu = { version = "1.0", optional = true }
serde = { version = "1.0.122", features = ["derive"] }
serde_json = { version = "1.0.18", features = ["std"], default-features = false, optional = true }
smartstring = "1.0"
static_assertions = "1.1"
time = { version = "0.3", features = ["formatting", "macros", "parsing"], optional = true }
url = { version = "2.0", optional = true }
Expand Down
21 changes: 11 additions & 10 deletions juniper/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ use crate::{
executor::Variables,
parser::Spanning,
value::{DefaultScalarValue, ScalarValue},
ArcStr,
};

/// A type literal in the syntax tree
///
/// This enum carries no semantic information and might refer to types that do
/// not exist.
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Type<'a> {
pub enum Type<N = ArcStr> {
/// A nullable named type, e.g. `String`
Named(Cow<'a, str>),
Named(N),
/// A nullable list type, e.g. `[String]`
///
/// The list itself is what's nullable, the containing type might be non-null.
List(Box<Type<'a>>, Option<usize>),
List(Box<Type<N>>, Option<usize>),
/// A non-null named type, e.g. `String!`
NonNullNamed(Cow<'a, str>),
NonNullNamed(N),
/// A non-null list type, e.g. `[String]!`.
///
/// The list itself is what's non-null, the containing type might be null.
NonNullList(Box<Type<'a>>, Option<usize>),
NonNullList(Box<Type<N>>, Option<usize>),
}

/// A JSON-like value that can be passed into the query execution, either
Expand All @@ -47,7 +48,7 @@ pub enum InputValue<S = DefaultScalarValue> {

#[derive(Clone, PartialEq, Debug)]
pub struct VariableDefinition<'a, S> {
pub var_type: Spanning<Type<'a>>,
pub var_type: Spanning<Type<&'a str>>,
pub default_value: Option<Spanning<InputValue<S>>>,
pub directives: Option<Vec<Spanning<Directive<'a, S>>>>,
}
Expand Down Expand Up @@ -194,13 +195,13 @@ pub trait ToInputValue<S = DefaultScalarValue>: Sized {
fn to_input_value(&self) -> InputValue<S>;
}

impl<'a> Type<'a> {
impl<N: AsRef<str>> Type<N> {
/// Get the name of a named type.
///
/// Only applies to named types; lists will return `None`.
pub fn name(&self) -> Option<&str> {
match *self {
Type::Named(ref n) | Type::NonNullNamed(ref n) => Some(n),
Type::Named(ref n) | Type::NonNullNamed(ref n) => Some(n.as_ref()),
_ => None,
}
}
Expand All @@ -210,7 +211,7 @@ impl<'a> Type<'a> {
/// All type literals contain exactly one named type.
pub fn innermost_name(&self) -> &str {
match *self {
Type::Named(ref n) | Type::NonNullNamed(ref n) => n,
Type::Named(ref n) | Type::NonNullNamed(ref n) => n.as_ref(),
Type::List(ref l, _) | Type::NonNullList(ref l, _) => l.innermost_name(),
}
}
Expand All @@ -221,7 +222,7 @@ impl<'a> Type<'a> {
}
}

impl<'a> fmt::Display for Type<'a> {
impl<N: fmt::Display> fmt::Display for Type<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Named(n) => write!(f, "{n}"),
Expand Down
Loading

0 comments on commit ae83c65

Please sign in to comment.