WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 0f3721b

Browse files
authored
refactor: use links only from the root config (#3190)
1 parent 198b8ad commit 0f3721b

37 files changed

+128
-660
lines changed

src/core/config/config_module/fixtures/router.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
schema {
1+
schema @server(port: 8000) @upstream(httpCache: 42, batch: {delay: 100}) {
22
# @link(src: "http://localhost:4000", type: SubGraph, meta: {name: "Users"})
33
# @link(src: "http://localhost:5000", type: SubGraph, meta: {name: "Posts"})
44
query: Query

src/core/config/config_module/fixtures/subgraph-posts.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
schema @server(port: 8000) @upstream(httpCache: 42, batch: {delay: 100}) {
1+
schema {
22
query: Query
33
}
44

src/core/config/config_module/fixtures/subgraph-users.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
schema @server(port: 8000) @upstream(httpCache: 42, batch: {delay: 100}) {
1+
schema {
22
query: Query
33
}
44

src/core/config/config_module/merge.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use indexmap::IndexMap;
44
use tailcall_valid::{Valid, Validator};
55

66
use super::{Cache, ConfigModule};
7-
use crate::core;
8-
use crate::core::config::{Arg, Config, Enum, Field, Type};
7+
use crate::core::config::{Arg, Config, Enum, Field, RootSchema, Type};
98
use crate::core::merge_right::MergeRight;
109
use crate::core::variance::{Contravariant, Covariant, Invariant};
10+
use crate::core::{self};
1111

1212
impl core::Type {
1313
fn merge(self, other: Self, non_null_merge: fn(bool, bool) -> bool) -> Valid<Self, String> {
@@ -183,6 +183,24 @@ impl Covariant for Enum {
183183
}
184184
}
185185

186+
impl Invariant for RootSchema {
187+
fn unify(self, other: Self) -> Valid<Self, String> {
188+
fn unify_option<T>(left: Option<T>, right: Option<T>) -> Option<T> {
189+
match (left, right) {
190+
(None, None) => None,
191+
(None, Some(that)) => Some(that),
192+
(Some(this), _) => Some(this),
193+
}
194+
}
195+
196+
Valid::succeed(Self {
197+
query: unify_option(self.query, other.query),
198+
mutation: unify_option(self.mutation, other.mutation),
199+
subscription: unify_option(self.subscription, other.subscription),
200+
})
201+
}
202+
}
203+
186204
impl Invariant for Cache {
187205
fn unify(self, other: Self) -> Valid<Self, String> {
188206
let mut types = self.config.types;
@@ -270,12 +288,18 @@ impl Invariant for Cache {
270288
.map(|en| (name, en))
271289
.trace(&trace_name)
272290
}))
273-
.map( |(merged_types, merged_enums)| {
291+
.fuse(self.config.schema.unify(other.config.schema))
292+
.map( |(merged_types, merged_enums, schema)| {
274293
types.extend(merged_types);
275294
enums.extend(merged_enums);
276295

277296
let config = Config {
278-
types, enums, unions: self.config.unions.merge_right(other.config.unions), server: self.config.server.merge_right(other.config.server), upstream: self.config.upstream.merge_right(other.config.upstream), schema: self.config.schema.merge_right(other.config.schema), links: self.config.links.merge_right(other.config.links), telemetry: self.config.telemetry.merge_right(other.config.telemetry) };
297+
types,
298+
enums,
299+
unions: self.config.unions.merge_right(other.config.unions),
300+
schema,
301+
..self.config
302+
};
279303

280304
Cache {
281305
config,

src/core/config/reader.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::path::Path;
22

3+
use futures_util::future::join_all;
34
use rustls_pemfile;
45
use rustls_pki_types::{
56
CertificateDer, PrivateKeyDer, PrivatePkcs1KeyDer, PrivatePkcs8KeyDer, PrivateSec1KeyDer,
67
};
7-
use tailcall_valid::{Valid, Validator};
8+
use tailcall_valid::{Valid, ValidationError, Validator};
89
use url::Url;
910

1011
use super::{ConfigModule, Content, Link, LinkType, PrivateKey};
@@ -34,11 +35,10 @@ impl ConfigReader {
3435
}
3536

3637
/// Reads the links in a Config and fill the content
37-
#[async_recursion::async_recursion]
3838
async fn ext_links(
3939
&self,
4040
config_module: ConfigModule,
41-
parent_dir: Option<&'async_recursion Path>,
41+
parent_dir: Option<&Path>,
4242
) -> anyhow::Result<ConfigModule> {
4343
let reader_ctx = ConfigReaderContext::new(&self.runtime);
4444

@@ -77,14 +77,6 @@ impl ConfigReader {
7777
config_module = config_module.and_then(|config_module| {
7878
config_module.unify(ConfigModule::from(config.clone()))
7979
});
80-
81-
if !config.links.is_empty() {
82-
let cfg_module = self
83-
.ext_links(ConfigModule::from(config), Path::new(&link.src).parent())
84-
.await?;
85-
config_module =
86-
config_module.and_then(|config_module| config_module.unify(cfg_module));
87-
}
8880
}
8981
LinkType::Protobuf => {
9082
let meta = self.proto_reader.read(path, None).await?;
@@ -200,24 +192,32 @@ impl ConfigReader {
200192
.map(|file| file.render(&reader_ctx))
201193
.collect::<Vec<_>>();
202194

203-
let mut config_module = Valid::succeed(ConfigModule::default());
204-
205-
for file in files.iter() {
195+
let mut config_modules = join_all(files.iter().map(|file| async {
206196
let source = Source::detect(&file.path)?;
207197
let schema = &file.content;
208198

209199
// Create initial config module
210-
let new_config_module = self
211-
.resolve(
212-
Config::from_source(source, schema)?,
213-
Path::new(&file.path).parent(),
214-
)
215-
.await?;
216-
217-
// Merge it with the original config set
218-
config_module =
219-
config_module.and_then(|config_module| config_module.unify(new_config_module));
220-
}
200+
self.resolve(
201+
Config::from_source(source, schema)?,
202+
Path::new(&file.path).parent(),
203+
)
204+
.await
205+
}))
206+
.await
207+
.into_iter();
208+
209+
let config_module = Valid::from(
210+
config_modules
211+
.next()
212+
.ok_or(anyhow::anyhow!("At least one config should be defined"))?
213+
.map_err(to_validation_error),
214+
);
215+
216+
let config_module = config_modules.fold(config_module, |acc, other| {
217+
acc.and_then(|cfg| {
218+
Valid::from(other.map_err(to_validation_error)).and_then(|other| cfg.unify(other))
219+
})
220+
});
221221

222222
Ok(config_module.to_result()?)
223223
}
@@ -256,6 +256,13 @@ impl ConfigReader {
256256
}
257257
}
258258

259+
fn to_validation_error(error: anyhow::Error) -> ValidationError<String> {
260+
match error.downcast::<ValidationError<String>>() {
261+
Ok(err) => err,
262+
Err(err) => ValidationError::new(err.to_string()),
263+
}
264+
}
265+
259266
#[cfg(test)]
260267
mod reader_tests {
261268
use std::path::{Path, PathBuf};

tests/core/snapshots/https.md_0.snap

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/core/snapshots/https.md_client.snap

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/core/snapshots/https.md_merged.snap

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/core/snapshots/test-nested-link.md_client.snap renamed to tests/core/snapshots/merge-linked-config.md_client.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
source: tests/core/spec.rs
33
expression: formatted
4+
snapshot_kind: text
45
---
5-
enum Foo {
6-
BAR
7-
BAZ
6+
type Foo {
7+
foo: String
88
}
99

1010
type Post {

tests/core/snapshots/test-nested-link.md_merged.snap renamed to tests/core/snapshots/merge-linked-config.md_merged.snap

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
---
22
source: tests/core/spec.rs
33
expression: formatter
4+
snapshot_kind: text
45
---
56
schema
6-
@server
7-
@upstream
8-
@link(src: "graphql-with-link.graphql", type: Config)
9-
@link(src: "link-enum.graphql", type: Config)
10-
@link(src: "link-enum.graphql", type: Config) {
7+
@server(port: 8000)
8+
@upstream(batch: {delay: 10, headers: []}, httpCache: 10)
9+
@link(src: "link-1.graphql", type: Config)
10+
@link(src: "link-2.graphql", type: Config) {
1111
query: Query
1212
}
1313

14-
enum Foo {
15-
BAR
16-
BAZ
14+
type Foo {
15+
foo: String
1716
}
1817

1918
type Post {

0 commit comments

Comments
 (0)