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(compiler): add type declaration with relay config file types #4569

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 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
2 changes: 2 additions & 0 deletions compiler/crates/common/src/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use serde::Serialize;

use crate::Rollout;

/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can make use of something like this: https://github.com/Aleph-Alpha/ts-rs here?

We would generate these typescript definitions based on the actual rust types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some POCs with specta yesterday, it seems to works well. I think that we can bring it to these structs too.

They have just a limitation around some JSDocs comments, like the @default and others, but it's working.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, @noghartt! I think this would be a much better solution to always keep these definitions in sync.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noghartt Just checking in here. Were you able to make any progress with Spectra? Do you think it can offer a viable automated solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noghartt Just checking in here. Were you able to make any progress with Spectra? Do you think it can offer a viable automated solution?

I think that we can bring an automated solution yes. But as I commented, specta doesn't seem to fit all the types for us, as an example, it doesn't fill the JSDocs related fields, like the @default. I couldn't find a workaround for that scenario.

Except that, it seems to works well. Do you think that we can bring this improvement in this PR or maybe moving to another?

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct FeatureFlags {
Expand Down Expand Up @@ -86,6 +87,7 @@ pub struct FeatureFlags {
pub enable_strict_custom_scalars: bool,
}

/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum FeatureFlag {
Expand Down
3 changes: 3 additions & 0 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ fn get_default_excludes() -> Vec<String> {
}

/// Schema of the compiler configuration JSON file.
///
/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct MultiProjectConfigFile {
Expand Down Expand Up @@ -662,6 +664,7 @@ struct MultiProjectConfigFile {
is_dev_variable_name: Option<String>,
}

/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase", default)]
pub struct SingleProjectConfigFile {
Expand Down
2 changes: 2 additions & 0 deletions compiler/crates/relay-config/src/js_module_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use serde::Deserialize;
use serde::Serialize;

/// Formatting style for generated files.
///
/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum JsModuleFormat {
Expand Down
2 changes: 2 additions & 0 deletions compiler/crates/relay-config/src/module_import_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use serde::Deserialize;
use serde::Serialize;

/// Configuration for @module.
///
/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Debug, Deserialize, Serialize, Default, Copy, Clone)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct ModuleImportConfig {
Expand Down
2 changes: 2 additions & 0 deletions compiler/crates/relay-config/src/project_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub struct LocalPersistConfig {
pub include_query_text: bool,
}

/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Debug, Serialize, Clone)]
#[serde(untagged)]
pub enum PersistConfig {
Expand Down Expand Up @@ -175,6 +176,7 @@ impl Debug for ExtraArtifactsConfig {
}
}

/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use serde::Deserialize;
use serde::Serialize;

/// Configuration for resolvers_schema_module generation
///
/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Default, Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct ResolversSchemaModuleConfig {
Expand Down
2 changes: 2 additions & 0 deletions compiler/crates/relay-config/src/typegen_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use strum::EnumIter;
use strum::IntoEnumIterator;
type FnvIndexMap<K, V> = IndexMap<K, V, FnvBuildHasher>;

/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(
EnumIter,
strum::Display,
Expand Down Expand Up @@ -50,6 +51,7 @@ impl TypegenLanguage {
}
}

/// **NOTE**: Every change on this struct should be reflected into the `relay-compiler/index.d.ts` file.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum CustomScalarType {
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ const relayCompiler = gulp.parallel(
},
function copyPackageFiles() {
return gulp
.src(['README.md', 'package.json', 'cli.js', 'index.js'], {
.src(['README.md', 'package.json', 'cli.js', 'index.js', 'index.d.ts'], {
cwd: path.join(PACKAGES, 'relay-compiler'),
})
.pipe(gulp.dest(path.join(DIST, 'relay-compiler')));
Expand Down
3 changes: 3 additions & 0 deletions packages/relay-compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ file sources, and "listen" to the file changes in the "watch" mode. If
- `criticalLevel` The severity level of diagnostics that will cause the
compiler to error out on. ["error" | "warning" | "info"]

> [!TIP]
> For more details, and configuration options see: [Relay Compiler Configuration](https://github.com/facebook/relay/tree/main/packages/relay-compiler/index.d.ts)

### CLI Arguments

- `--repersist` Run the persister even if the query has not changed.
Expand Down
Loading
Loading