Skip to content

Commit

Permalink
refactor: use ArcStr for storing strings in Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
audunhalland committed Mar 6, 2024
1 parent ac6e609 commit 7e3e6b4
Show file tree
Hide file tree
Showing 99 changed files with 1,095 additions and 1,080 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
1 change: 0 additions & 1 deletion book/src/advanced/introspection.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl Query {
}

type Schema = juniper::RootNode<
'static,
Query,
EmptyMutation<Context>,
EmptySubscription<Context>
Expand Down
2 changes: 1 addition & 1 deletion book/src/advanced/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ where [`Connection`][Connection] is a `Stream` of values returned by the operati
# Box::pin(stream)
# }
# }
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
4 changes: 2 additions & 2 deletions book/src/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Mutation {

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

// 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>>;
type Schema = juniper::RootNode<Query, EmptyMutation<Ctx>, EmptySubscription<Ctx>>;

fn main() {
// Create a context object.
Expand Down
2 changes: 1 addition & 1 deletion juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -63,7 +64,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 7e3e6b4

Please sign in to comment.