Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(config): represent field resolver as separate enum #2675

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 74 additions & 66 deletions generated/.tailcallrc.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,80 @@
"Field": {
"description": "A field definition containing all the metadata information about resolving a field.",
"type": "object",
"oneOf": [
{
"type": "object",
"required": [
"http"
],
"properties": {
"http": {
"$ref": "#/definitions/Http"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"grpc"
],
"properties": {
"grpc": {
"$ref": "#/definitions/Grpc"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"graphql"
],
"properties": {
"graphql": {
"$ref": "#/definitions/GraphQL"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"call"
],
"properties": {
"call": {
"$ref": "#/definitions/Call"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"js"
],
"properties": {
"js": {
"$ref": "#/definitions/JS"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"expr"
],
"properties": {
"expr": {
"$ref": "#/definitions/Expr"
}
},
"additionalProperties": false
}
],
"properties": {
"args": {
"description": "Map of argument name and its definition.",
Expand All @@ -391,17 +465,6 @@
}
]
},
"call": {
"description": "Inserts a call resolver for the field.",
"anyOf": [
{
"$ref": "#/definitions/Call"
},
{
"type": "null"
}
]
},
"default_value": {
"description": "Stores the default value for the field"
},
Expand All @@ -412,50 +475,6 @@
"null"
]
},
"expr": {
"description": "Inserts a constant resolver for the field.",
"anyOf": [
{
"$ref": "#/definitions/Expr"
},
{
"type": "null"
}
]
},
"graphql": {
"description": "Inserts a GraphQL resolver for the field.",
"anyOf": [
{
"$ref": "#/definitions/GraphQL"
},
{
"type": "null"
}
]
},
"grpc": {
"description": "Inserts a GRPC resolver for the field.",
"anyOf": [
{
"$ref": "#/definitions/Grpc"
},
{
"type": "null"
}
]
},
"http": {
"description": "Inserts an HTTP resolver for the field.",
"anyOf": [
{
"$ref": "#/definitions/Http"
},
{
"type": "null"
}
]
},
"list": {
"description": "Flag to indicate the type is a list.",
"type": "boolean"
Expand Down Expand Up @@ -502,17 +521,6 @@
"description": "Flag to indicate the type is required.",
"type": "boolean"
},
"script": {
"description": "Inserts a Javascript resolver for the field.",
"anyOf": [
{
"$ref": "#/definitions/JS"
},
{
"type": "null"
}
]
},
"type": {
"description": "Refers to the type of the value the field can be resolved to.",
"type": "string"
Expand Down
4 changes: 2 additions & 2 deletions src/cli/tc/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;

use super::helpers::{FILE_NAME, JSON_FILE_NAME, YML_FILE_NAME};
use crate::cli::runtime::{confirm_and_write, create_directory, select_prompt};
use crate::core::config::{Config, Expr, Field, RootSchema, Source, Type};
use crate::core::config::{Config, Expr, Field, Resolver, RootSchema, Source, Type};
use crate::core::merge_right::MergeRight;
use crate::core::runtime::TargetRuntime;

Expand Down Expand Up @@ -75,7 +75,7 @@ fn main_config() -> Config {
let field = Field {
type_of: "String".to_string(),
required: true,
const_field: Some(Expr { body: "Hello, World!".into() }),
resolver: Some(Resolver::Expr(Expr { body: "Hello, World!".into() })),
..Default::default()
};

Expand Down
26 changes: 3 additions & 23 deletions src/core/blueprint/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,9 @@ fn process_field_within_type(context: ProcessFieldWithinTypeContext) -> Valid<Ty
let path_resolver_error_handler = context.path_resolver_error_handler;

if let Some(next_field) = type_info.fields.get(field_name) {
if next_field.has_resolver() {
let next_dir_http = next_field
.http
.as_ref()
.map(|_| config::Http::directive_name());
let next_dir_const = next_field
.const_field
.as_ref()
.map(|_| config::Expr::directive_name());
if let Some(resolver) = &next_field.resolver {
return path_resolver_error_handler(
next_dir_http
.or(next_dir_const)
.unwrap_or(config::JS::directive_name())
.as_str(),
&resolver.directive_name(),
&field.type_of,
field_name,
context.original_path,
Expand Down Expand Up @@ -423,7 +412,7 @@ fn to_fields(
&add_field.name,
)
.and_then(|field_definition| {
let added_field_path = match source_field.http {
let added_field_path = match source_field.resolver {
Some(_) => add_field.path[1..]
.iter()
.map(|s| s.to_owned())
Expand Down Expand Up @@ -497,15 +486,6 @@ pub fn to_field_definition(
type_of: &config::Type,
name: &String,
) -> Valid<FieldDefinition, String> {
let directives = field.resolvable_directives();

if directives.len() > 1 {
return Valid::fail(format!(
"Multiple resolvers detected [{}]",
directives.join(", ")
));
}

update_args()
.and(update_http().trace(config::Http::trace_name().as_str()))
.and(update_grpc(operation_type).trace(config::Grpc::trace_name().as_str()))
Expand Down
2 changes: 1 addition & 1 deletion src/core/blueprint/from_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ where
let schema = if let Some(type_) = type_ {
let mut schema_fields = HashMap::new();
for (name, field) in type_.fields.iter() {
if field.script.is_none() && field.http.is_none() {
if field.resolver.is_none() {
schema_fields.insert(name.clone(), to_json_schema_for_field(field, config));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/blueprint/operators/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde_json::Value;

use crate::core::blueprint::*;
use crate::core::config;
use crate::core::config::{Field, GraphQLOperationType};
use crate::core::config::{Field, GraphQLOperationType, Resolver};
use crate::core::ir::model::IR;
use crate::core::try_fold::TryFold;
use crate::core::valid::{Valid, ValidationError, Validator};
Expand All @@ -14,11 +14,11 @@ pub fn update_call<'a>(
{
TryFold::<(&ConfigModule, &Field, &config::Type, &str), FieldDefinition, String>::new(
move |(config, field, _, name), b_field| {
let Some(ref calls) = field.call else {
let Some(Resolver::Call(call)) = &field.resolver else {
return Valid::succeed(b_field);
};

compile_call(config, calls, operation_type, object_name)
compile_call(config, call, operation_type, object_name)
.map(|field| b_field.resolver(field.resolver).name(name.to_string()))
},
)
Expand Down
13 changes: 4 additions & 9 deletions src/core/blueprint/operators/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_graphql_value::ConstValue;

use crate::core::blueprint::*;
use crate::core::config;
use crate::core::config::Field;
use crate::core::config::{Field, Resolver};
use crate::core::ir::model::IR;
use crate::core::ir::model::IR::Dynamic;
use crate::core::try_fold::TryFold;
Expand Down Expand Up @@ -64,17 +64,12 @@ pub fn update_const_field<'a>(
{
TryFold::<(&ConfigModule, &Field, &config::Type, &str), FieldDefinition, String>::new(
|(config_module, field, _, _), b_field| {
let Some(const_field) = &field.const_field else {
let Some(Resolver::Expr(expr)) = &field.resolver else {
return Valid::succeed(b_field);
};

compile_expr(CompileExpr {
config_module,
field,
value: &const_field.body,
validate: true,
})
.map(|resolver| b_field.resolver(Some(resolver)))
compile_expr(CompileExpr { config_module, field, value: &expr.body, validate: true })
.map(|resolver| b_field.resolver(Some(resolver)))
},
)
}
6 changes: 4 additions & 2 deletions src/core/blueprint/operators/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::{HashMap, HashSet};

use crate::core::blueprint::FieldDefinition;
use crate::core::config::{Config, ConfigModule, Field, GraphQL, GraphQLOperationType, Type};
use crate::core::config::{
Config, ConfigModule, Field, GraphQL, GraphQLOperationType, Resolver, Type,
};
use crate::core::graphql::RequestTemplate;
use crate::core::helpers;
use crate::core::ir::model::{IO, IR};
Expand Down Expand Up @@ -78,7 +80,7 @@ pub fn update_graphql<'a>(
) -> TryFold<'a, (&'a ConfigModule, &'a Field, &'a Type, &'a str), FieldDefinition, String> {
TryFold::<(&ConfigModule, &Field, &Type, &'a str), FieldDefinition, String>::new(
|(config, field, type_of, _), b_field| {
let Some(graphql) = &field.graphql else {
let Some(Resolver::Graphql(graphql)) = &field.resolver else {
return Valid::succeed(b_field);
};

Expand Down
4 changes: 2 additions & 2 deletions src/core/blueprint/operators/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use prost_reflect::FieldDescriptor;

use crate::core::blueprint::{FieldDefinition, TypeLike};
use crate::core::config::group_by::GroupBy;
use crate::core::config::{Config, ConfigModule, Field, GraphQLOperationType, Grpc};
use crate::core::config::{Config, ConfigModule, Field, GraphQLOperationType, Grpc, Resolver};
use crate::core::grpc::protobuf::{ProtobufOperation, ProtobufSet};
use crate::core::grpc::request_template::RequestTemplate;
use crate::core::ir::model::{IO, IR};
Expand Down Expand Up @@ -216,7 +216,7 @@ pub fn update_grpc<'a>(
{
TryFold::<(&ConfigModule, &Field, &config::Type, &'a str), FieldDefinition, String>::new(
|(config_module, field, type_of, _name), b_field| {
let Some(grpc) = &field.grpc else {
let Some(Resolver::Grpc(grpc)) = &field.resolver else {
return Valid::succeed(b_field);
};

Expand Down
4 changes: 2 additions & 2 deletions src/core/blueprint/operators/http.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::core::blueprint::*;
use crate::core::config::group_by::GroupBy;
use crate::core::config::Field;
use crate::core::config::{Field, Resolver};
use crate::core::endpoint::Endpoint;
use crate::core::http::{HttpFilter, Method, RequestTemplate};
use crate::core::ir::model::{IO, IR};
Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn update_http<'a>(
{
TryFold::<(&ConfigModule, &Field, &config::Type, &'a str), FieldDefinition, String>::new(
|(config_module, field, type_of, _), b_field| {
let Some(http) = &field.http else {
let Some(Resolver::Http(http)) = &field.resolver else {
return Valid::succeed(b_field);
};

Expand Down
4 changes: 2 additions & 2 deletions src/core/blueprint/operators/js.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::core::blueprint::FieldDefinition;
use crate::core::config;
use crate::core::config::{ConfigModule, Field};
use crate::core::config::{ConfigModule, Field, Resolver};
use crate::core::ir::model::{IO, IR};
use crate::core::try_fold::TryFold;
use crate::core::valid::{Valid, Validator};
Expand All @@ -21,7 +21,7 @@ pub fn update_js_field<'a>(
{
TryFold::<(&ConfigModule, &Field, &config::Type, &str), FieldDefinition, String>::new(
|(module, field, _, _), b_field| {
let Some(js) = &field.script else {
let Some(Resolver::Js(js)) = &field.resolver else {
return Valid::succeed(b_field);
};

Expand Down
Loading
Loading