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

feat: Support mustache on link directives #3176

Merged
merged 18 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ Index {
Mustache(
[
Expression(
[
"args",
"input",
],
Expression(
[
"args",
"input",
],
),
),
],
),
Expand Down Expand Up @@ -107,10 +109,12 @@ Index {
Mustache(
[
Expression(
[
"args",
"input",
],
Expression(
[
"args",
"input",
],
),
),
],
),
Expand Down Expand Up @@ -185,10 +189,12 @@ Index {
Mustache(
[
Expression(
[
"args",
"input",
],
Expression(
[
"args",
"input",
],
),
),
],
),
Expand Down Expand Up @@ -266,10 +272,12 @@ Index {
Mustache(
[
Expression(
[
"args",
"input",
],
Expression(
[
"args",
"input",
],
),
),
],
),
Expand Down Expand Up @@ -653,10 +661,12 @@ Index {
value: Mustache(
[
Expression(
[
"args",
"term",
],
Expression(
[
"args",
"term",
],
),
),
],
),
Expand Down Expand Up @@ -724,10 +734,12 @@ Index {
"http://jsonplaceholder.typicode.com/users/",
),
Expression(
[
"args",
"id",
],
Expression(
[
"args",
"id",
],
),
),
],
),
Expand Down Expand Up @@ -812,10 +824,12 @@ Index {
value: Mustache(
[
Expression(
[
"args",
"term",
],
Expression(
[
"args",
"term",
],
),
),
],
),
Expand Down Expand Up @@ -895,10 +909,12 @@ Index {
"http://jsonplaceholder.typicode.com/users/",
),
Expression(
[
"args",
"id",
],
Expression(
[
"args",
"id",
],
),
),
],
),
Expand Down
16 changes: 13 additions & 3 deletions src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ use tailcall_valid::{Valid, Validator};
use super::directive::Directive;
use super::from_document::from_document;
use super::{
AddField, Alias, Cache, Call, Discriminate, Expr, GraphQL, Grpc, Http, Link, Modify, Omit,
Protected, ResolverSet, Server, Telemetry, Upstream, JS,
AddField, Alias, Cache, Call, ConfigReaderContext, Discriminate, Expr, GraphQL, Grpc, Http,
Link, Modify, Omit, Protected, ResolverSet, Server, Telemetry, Upstream, JS,
};
use crate::core::config::npo::QueryPath;
use crate::core::config::source::Source;
use crate::core::is_default;
use crate::core::macros::MergeRight;
use crate::core::merge_right::MergeRight;
use crate::core::scalar::Scalar;
use crate::core::{is_default, Mustache};

#[derive(
Serialize,
Expand Down Expand Up @@ -447,6 +447,16 @@ impl Config {
}
}

pub fn from_source_resolved(
source: Source,
schema: &str,
reader_ctx: &ConfigReaderContext,
) -> Result<Self> {
let mustache = Mustache::parse(schema);
let schema = mustache.render_strict(reader_ctx);
Self::from_source(source, &schema)
}

pub fn n_plus_one(&self) -> QueryPath {
super::npo::PathTracker::new(self).find()
}
Expand Down
20 changes: 18 additions & 2 deletions src/core/config/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ impl ConfigReader {
config_module: ConfigModule,
parent_dir: Option<&'async_recursion Path>,
) -> anyhow::Result<ConfigModule> {
let reader_ctx = ConfigReaderContext {
runtime: &self.runtime,
vars: &Default::default(),
headers: Default::default(),
};
tusharmath marked this conversation as resolved.
Show resolved Hide resolved

let links: Vec<Link> = config_module
.config()
.links
Expand Down Expand Up @@ -67,7 +73,11 @@ impl ConfigReader {
LinkType::Config => {
let source = self.resource_reader.read_file(path).await?;
let content = source.content;
let config = Config::from_source(Source::detect(&source.path)?, &content)?;
let config = Config::from_source_resolved(
Source::detect(&source.path)?,
&content,
&reader_ctx,
)?;
config_module = config_module.and_then(|config_module| {
config_module.unify(ConfigModule::from(config.clone()))
});
Expand Down Expand Up @@ -184,6 +194,12 @@ impl ConfigReader {
&self,
files: &[T],
) -> anyhow::Result<ConfigModule> {
let reader_ctx = ConfigReaderContext {
runtime: &self.runtime,
vars: &Default::default(),
headers: Default::default(),
};

let files = self.resource_reader.read_files(files).await?;
let mut config_module = Valid::succeed(ConfigModule::default());

Expand All @@ -194,7 +210,7 @@ impl ConfigReader {
// Create initial config module
let new_config_module = self
.resolve(
Config::from_source(source, schema)?,
Config::from_source_resolved(source, schema, &reader_ctx)?,
Path::new(&file.path).parent(),
)
.await?;
Expand Down
4 changes: 4 additions & 0 deletions src/core/http/request_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@
})
.next() // Return the first value that is found
}

fn eval_strict(&'a self, mustache: &'a Mustache, in_value: &'a Self::In) -> Self::Out {
self.eval(mustache, in_value)
}

Check warning on line 306 in src/core/http/request_template.rs

View check run for this annotation

Codecov / codecov/patch

src/core/http/request_template.rs#L304-L306

Added lines #L304 - L306 were not covered by tests
}

#[cfg(test)]
Expand Down
Loading
Loading