graphql-engine/v3/crates/lang-graphql/benches/validation.rs
Philip Lykke Carlsen 18efbe9380 Add ability to override interpretations of roles (#660)
This PR refactors the schema crate to enable overriding the
interpretation of roles at runtime.

By providing a `NamespacedGetter` that ignores roles entirely (i.e.
`GDSNamespacedGetterAgnostic`) to schema generation functions it is
possible to derive a GraphQL schema that ignores permissions and
presets.

This PR leaves back a few opportunities to improve code clarity, which I
hope to address in follow-up PRs.
For instance, it seems superfluous to make sense for generic code
parametrised `<S: SchemaContext>` to carry around both a `S::Namespace`
and a `S::NamespaceGetter`, when all that code could ever do with that
`S::Namespace` value anyway is apply it to the `S::NamespaceGetter`.
Taking this line of reasoning to its conclusion suggests that we can
reduce the complexity of `trait SchemaContext` and its associated types
and kit.

---------

Co-authored-by: Samir Talwar <samir.talwar@hasura.io>
V3_GIT_ORIGIN_REV_ID: c84555fd88279582670919faa5a62bbd00b714bd
2024-06-04 13:04:36 +00:00

57 lines
1.9 KiB
Rust

use lang_graphql::http;
use lang_graphql::parser::Parser;
use lang_graphql::schema::sdl;
use lang_graphql::validation;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::collections::HashMap;
use std::fs;
use std::str::FromStr;
pub fn bench_validation(c: &mut Criterion) {
let query_files = fs::read_dir("benches/validation/introspection").unwrap();
let mut group = c.benchmark_group("validation");
for query_file in query_files {
let query_path = query_file.unwrap().path();
let query = fs::read_to_string(&query_path).unwrap();
// benches/queries/<file_name>.graphql -> <file_name>
let query_name = query_path.file_stem().unwrap().to_str().unwrap();
let parsed_query = Parser::new(&query).parse_executable_document().unwrap();
let fake_schema = sdl::SDL::new("type Query {foo: Int}")
.and_then(|v| v.build_schema())
.unwrap();
let request = http::Request {
operation_name: Some(lang_graphql::ast::common::Name::from_str(query_name).unwrap()),
query: parsed_query,
variables: HashMap::new(),
};
validation::normalize_request(
&sdl::SDLNamespacedGetter(),
&sdl::Namespace,
&fake_schema,
&request,
)
.unwrap();
// parse with our parser
group.bench_with_input(
BenchmarkId::new("hasura", query_name),
&(request, fake_schema),
|b, (request, schema)| {
b.iter(|| {
validation::normalize_request(
&sdl::SDLNamespacedGetter(),
&sdl::Namespace,
schema,
request,
)
.unwrap()
})
},
);
}
group.finish();
}
criterion_group!(benches, bench_validation);
criterion_main!(benches);