Skip to content

Commit

Permalink
Set schema depth and complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jan 8, 2025
1 parent 66053f7 commit e472f3d
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 125 deletions.
18 changes: 6 additions & 12 deletions examples/mysql/src/query_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,10 @@ pub fn schema(
]
);
builder.register_enumeration::<crate::entities::sea_orm_active_enums::Rating>();
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}
18 changes: 6 additions & 12 deletions examples/mysql/tests/guard_mutation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,12 @@ pub fn schema(
]
);
builder.register_enumeration::<sea_orm_active_enums::Rating>();
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}

pub async fn get_schema() -> Schema {
Expand Down
18 changes: 6 additions & 12 deletions examples/mysql/tests/guard_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,12 @@ pub fn schema(
]);
builder
.register_enumeration::<seaography_mysql_example::entities::sea_orm_active_enums::Rating>();
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}

pub async fn get_schema() -> Schema {
Expand Down
18 changes: 6 additions & 12 deletions examples/postgres/src/query_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ pub fn schema(
]
);
builder.register_enumeration::<crate::entities::sea_orm_active_enums::MpaaRating>();
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}
18 changes: 6 additions & 12 deletions examples/postgres/tests/guard_mutation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,12 @@ pub fn schema(
]
);
builder.register_enumeration::<sea_orm_active_enums::MpaaRating>();
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}

pub async fn get_schema() -> Schema {
Expand Down
18 changes: 6 additions & 12 deletions examples/postgres/tests/guard_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,12 @@ pub fn schema(
),
]);
builder.register_enumeration::<seaography_postgres_example::entities::sea_orm_active_enums::MpaaRating>();
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}

pub async fn get_schema() -> Schema {
Expand Down
18 changes: 6 additions & 12 deletions examples/sqlite/src/query_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ pub fn schema(
store,
]
);
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}
18 changes: 6 additions & 12 deletions examples/sqlite/tests/guard_mutation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,12 @@ pub fn schema(
store,
]
);
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}

pub async fn get_schema() -> Schema {
Expand Down
18 changes: 6 additions & 12 deletions examples/sqlite/tests/guard_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,12 @@ pub fn schema(
seaography_sqlite_example::entities::address::Relation::Store.def(),
),
]);
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}

pub async fn get_schema() -> Schema {
Expand Down
21 changes: 6 additions & 15 deletions generator/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,12 @@ pub fn generate_query_root<P: AsRef<Path>>(entities_path: &P) -> TokenStream {

#(#enumerations)*

let schema = builder.schema_builder();

let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};

let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};

schema.data(database).finish()
builder
.set_depth_limit(depth)
.set_complexity_limit(complexity)
.schema_builder()
.data(database)
.finish()
}
}
}
Expand Down
33 changes: 31 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ pub struct Builder {

/// configuration for builder
pub context: &'static BuilderContext,

/// Set the maximum depth a query can have
pub depth: Option<usize>,

/// Set the maximum complexity a query can have
pub complexity: Option<usize>,
}

impl Builder {
Expand All @@ -66,6 +72,8 @@ impl Builder {
mutations: Vec::new(),
connection,
context,
depth: None,
complexity: None,
}
}

Expand Down Expand Up @@ -219,6 +227,16 @@ impl Builder {
.push(filter_types_map_helper.generate_filter_input(&filter_info));
}

pub fn set_depth_limit(mut self, depth: Option<usize>) -> Self {
self.depth = depth;
self
}

pub fn set_complexity_limit(mut self, complexity: Option<usize>) -> Self {
self.complexity = complexity;
self
}

/// used to consume the builder context and generate a ready to be completed GraphQL schema
pub fn schema_builder(self) -> SchemaBuilder {
let query = self.query;
Expand Down Expand Up @@ -264,7 +282,7 @@ impl Builder {
.into_iter()
.fold(schema, |schema, cur| schema.register(cur));

schema
let schema = schema
.register(
OrderByEnumBuilder {
context: self.context,
Expand Down Expand Up @@ -314,7 +332,18 @@ impl Builder {
.to_object(),
)
.register(query)
.register(mutation)
.register(mutation);

let schema = if let Some(depth) = self.depth {
schema.limit_depth(depth)
} else {
schema
};
if let Some(complexity) = self.complexity {
schema.limit_complexity(complexity)
} else {
schema
}
}
}

Expand Down

0 comments on commit e472f3d

Please sign in to comment.