Skip to content

Commit

Permalink
chore: add fuse in valid (tailcallhq#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath authored Feb 1, 2024
1 parent f759d2c commit 3d80c9e
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 127 deletions.
2 changes: 1 addition & 1 deletion src/blueprint/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::config::{Config, Field, GraphQLOperationType, Union};
use crate::directive::DirectiveCodec;
use crate::lambda::{Expression, Lambda};
use crate::try_fold::TryFold;
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

pub fn to_scalar_type_definition(name: &str) -> Valid<Definition, String> {
Valid::succeed(Definition::ScalarTypeDefinition(ScalarTypeDefinition {
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/from_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::config::{Arg, Batch, Config, ConfigSet, Field};
use crate::json::JsonSchema;
use crate::lambda::{Expression, IO};
use crate::try_fold::TryFold;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

pub fn config_blueprint<'a>() -> TryFold<'a, ConfigSet, Blueprint, String> {
let server = TryFoldConfig::<Blueprint>::new(|config_set, blueprint| {
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/mustache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{is_scalar, to_type, FieldDefinition, Type};
use crate::config::{self, Config};
use crate::lambda::{Expression, IO};
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

struct MustachePartsValidator<'a> {
type_of: &'a config::Type,
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Write;
use async_graphql::dynamic::Schema;

use super::{Blueprint, SchemaModifiers};
use crate::valid::{Cause, Valid};
use crate::valid::{Cause, Valid, Validator};

#[derive(Debug)]
pub struct OperationQuery {
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/operators/const_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::config::Field;
use crate::lambda::Expression;
use crate::lambda::Expression::Literal;
use crate::try_fold::TryFold;
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

fn validate_data_with_schema(
config: &config::Config,
Expand Down
10 changes: 6 additions & 4 deletions src/blueprint/operators/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::config;
use crate::config::{ExprBody, Field, If};
use crate::lambda::{Expression, List, Logic, Math, Relation};
use crate::try_fold::TryFold;
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

struct CompilationContext<'a> {
config_field: &'a config::Field,
Expand Down Expand Up @@ -78,9 +78,10 @@ fn compile(ctx: &CompilationContext, expr: ExprBody) -> Valid<Expression, String
ExprBody::If(If { ref cond, on_true: ref then, on_false: ref els }) => {
compile(ctx, *cond.clone())
.map(Box::new)
.zip(compile(ctx, *then.clone()).map(Box::new))
.zip(compile(ctx, *els.clone()).map(Box::new))
.map(|((cond, then), els)| {
.fuse(compile(ctx, *then.clone()).map(Box::new))
.fuse(compile(ctx, *els.clone()).map(Box::new))
.collect()
.map(|(cond, then, els)| {
Expression::Logic(Logic::If { cond, then, els }).parallel_when(expr.has_io())
})
}
Expand Down Expand Up @@ -181,6 +182,7 @@ mod tests {
use crate::config::{ConfigSet, Expr, Field, GraphQLOperationType};
use crate::http::RequestContext;
use crate::lambda::{Concurrent, Eval, EvaluationContext, ResolverContextLike};
use crate::valid::Validator;

#[derive(Default)]
struct Context<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/operators/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::graphql::RequestTemplate;
use crate::helpers;
use crate::lambda::{Expression, Lambda};
use crate::try_fold::TryFold;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

pub fn compile_graphql(
config: &config::Config,
Expand Down
11 changes: 6 additions & 5 deletions src/blueprint/operators/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::json::JsonSchema;
use crate::lambda::{Expression, Lambda, IO};
use crate::mustache::Mustache;
use crate::try_fold::TryFold;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};
use crate::{config, helpers};

fn to_url(grpc: &Grpc, config: &Config) -> Valid<Mustache, String> {
Expand Down Expand Up @@ -127,13 +127,14 @@ pub fn compile_grpc(inputs: CompileGrpc) -> Valid<Expression, String> {
let validate_with_schema = inputs.validate_with_schema;

to_url(grpc, config_set)
.zip(to_operation(
.fuse(to_operation(
grpc,
&config_set.extensions.grpc_file_descriptor,
))
.zip(helpers::headers::to_mustache_headers(&grpc.headers))
.zip(helpers::body::to_body(grpc.body.as_deref()))
.and_then(|(((url, operation), headers), body)| {
.fuse(helpers::headers::to_mustache_headers(&grpc.headers))
.fuse(helpers::body::to_body(grpc.body.as_deref()))
.collect()
.and_then(|(url, operation, headers, body)| {
let validation = if validate_with_schema {
let field_schema = json_schema_from_field(config_set, field);
if grpc.group_by.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/operators/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::endpoint::Endpoint;
use crate::http::{Method, RequestTemplate};
use crate::lambda::{Expression, Lambda, IO};
use crate::try_fold::TryFold;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};
use crate::{config, helpers};

pub fn compile_http(
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_graphql::parser::types::ConstDirective;
use crate::blueprint::*;
use crate::config::{Config, Field, Type};
use crate::directive::DirectiveCodec;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

fn validate_query(config: &Config) -> Valid<(), String> {
Valid::from_option(
Expand Down
11 changes: 6 additions & 5 deletions src/blueprint/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hyper::header::{HeaderName, HeaderValue};
use hyper::HeaderMap;

use crate::config::{self, HttpVersion};
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

#[derive(Clone, Debug, Setters)]
pub struct Server {
Expand Down Expand Up @@ -87,12 +87,13 @@ impl TryFrom<crate::config::Server> for Server {
};

validate_hostname((config_server).get_hostname().to_lowercase())
.zip(http_server)
.zip(handle_response_headers(
.fuse(http_server)
.fuse(handle_response_headers(
(config_server).get_response_headers().0,
))
.zip(to_script(&config_server))
.map(|(((hostname, http), response_headers), script)| Server {
.fuse(to_script(&config_server))
.collect()
.map(|(hostname, http, response_headers, script)| Server {
enable_apollo_tracing: (config_server).enable_apollo_tracing(),
enable_cache_control_header: (config_server).enable_cache_control(),
enable_graphiql: (config_server).enable_graphiql(),
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Deref;
use super::TryFoldConfig;
use crate::config::{ConfigSet, Upstream};
use crate::try_fold::TryFold;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

pub fn to_upstream<'a>() -> TryFold<'a, ConfigSet, Upstream, String> {
TryFoldConfig::<Upstream>::new(|config_set, up| {
Expand Down
1 change: 1 addition & 0 deletions src/cli/tc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::cli::server::Server;
use crate::cli::{init_file, init_http, CLIError};
use crate::config::reader::ConfigReader;
use crate::config::{Config, Upstream};
use crate::valid::Validator;
use crate::{print_schema, FileIO};

const FILE_NAME: &str = ".tailcallrc.graphql";
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::directive::DirectiveCodec;
use crate::http::Method;
use crate::is_default;
use crate::json::JsonSchema;
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

#[derive(
Serialize, Deserialize, Clone, Debug, Default, Setters, PartialEq, Eq, schemars::JsonSchema,
Expand Down
71 changes: 35 additions & 36 deletions src/config/from_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::config::{
self, Cache, Config, Expr, GraphQL, Grpc, Modify, Omit, RootSchema, Server, Union, Upstream,
};
use crate::directive::DirectiveCodec;
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

const DEFAULT_SCHEMA_DEFINITION: &SchemaDefinition = &SchemaDefinition {
extend: false,
Expand All @@ -36,16 +36,16 @@ pub fn from_document(doc: ServiceDocument) -> Valid<Config, String> {
let types = to_types(&type_definitions);
let unions = to_union_types(&type_definitions);
let schema = schema_definition(&doc).map(to_root_schema);

schema_definition(&doc)
.and_then(|sd| {
server(sd)
.zip(upstream(sd))
.zip(types)
.zip(unions)
.zip(schema)
.fuse(upstream(sd))
.fuse(types)
.fuse(unions)
.fuse(schema)
.collect()
})
.map(|((((server, upstream), types), unions), schema)| Config {
.map(|(server, upstream, types, unions, schema)| Config {
server,
upstream,
types,
Expand Down Expand Up @@ -248,35 +248,34 @@ where
let list_type_required = matches!(&base, BaseType::List(type_of) if !type_of.nullable);
let doc = description.to_owned().map(|pos| pos.node);
config::Http::from_directives(directives.iter())
.zip(GraphQL::from_directives(directives.iter()))
.zip(Cache::from_directives(directives.iter()))
.zip(Grpc::from_directives(directives.iter()))
.zip(Expr::from_directives(directives.iter()))
.zip(Omit::from_directives(directives.iter()))
.zip(Modify::from_directives(directives.iter()))
.zip(JS::from_directives(directives.iter()))
.map(
|(((((((http, graphql), cache), grpc), expr), omit), modify), script)| {
let const_field = to_const_field(directives);
config::Field {
type_of,
list,
required: !nullable,
list_type_required,
args,
doc,
modify,
omit,
http,
grpc,
script,
const_field,
graphql,
expr,
cache,
}
},
)
.fuse(GraphQL::from_directives(directives.iter()))
.fuse(Cache::from_directives(directives.iter()))
.fuse(Grpc::from_directives(directives.iter()))
.fuse(Expr::from_directives(directives.iter()))
.fuse(Omit::from_directives(directives.iter()))
.fuse(Modify::from_directives(directives.iter()))
.fuse(JS::from_directives(directives.iter()))
.collect()
.map(|(http, graphql, cache, grpc, expr, omit, modify, script)| {
let const_field = to_const_field(directives);
config::Field {
type_of,
list,
required: !nullable,
list_type_required,
args,
doc,
modify,
omit,
http,
grpc,
script,
const_field,
graphql,
expr,
cache,
}
})
}

fn to_type_of(type_: &Type) -> String {
Expand Down
3 changes: 2 additions & 1 deletion src/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_json::{Map, Value};
use serde_path_to_error::deserialize;

use crate::blueprint;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

fn pos<A>(a: A) -> Positioned<A> {
Positioned::new(a, Pos::default())
Expand Down Expand Up @@ -115,6 +115,7 @@ mod tests {

use crate::blueprint::Directive;
use crate::directive::{pos, to_const_directive};
use crate::valid::Validator;

#[test]
fn test_to_const_directive() {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use hyper::header::HeaderName;

use crate::config::KeyValues;
use crate::mustache::Mustache;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

pub type MustacheHeaders = Vec<(HeaderName, Mustache)>;

Expand Down Expand Up @@ -30,6 +30,7 @@ mod tests {
use super::to_mustache_headers;
use crate::config::KeyValues;
use crate::mustache::Mustache;
use crate::valid::Validator;

#[test]
fn valid_headers() -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/json/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use prost_reflect::{FieldDescriptor, Kind, MessageDescriptor};
use serde::{Deserialize, Serialize};

use crate::valid::Valid;
use crate::valid::{Valid, Validator};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)]
#[serde(rename = "schema")]
Expand Down Expand Up @@ -214,7 +214,7 @@ mod tests {
use indexmap::IndexMap;

use crate::json::JsonSchema;
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

#[test]
fn test_validate_string() {
Expand Down
1 change: 1 addition & 0 deletions src/lambda/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::grpc::request_template::RenderedRequestTemplate;
use crate::http::{cache_policy, DataLoaderRequest, HttpDataLoader, Response};
use crate::json::JsonLike;
use crate::lambda::EvaluationError;
use crate::valid::Validator;
use crate::{grpc, http};

#[derive(Clone, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions src/try_fold.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::valid::Valid;
use crate::valid::{Valid, Validator};

/// Trait for types that support a "try fold" operation.
///
Expand Down Expand Up @@ -164,7 +164,7 @@ mod tests {
use std::cell::RefCell;

use super::TryFold;
use crate::valid::{Valid, ValidationError};
use crate::valid::{Valid, ValidationError, Validator};

#[test]
fn test_and() {
Expand Down
Loading

0 comments on commit 3d80c9e

Please sign in to comment.