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

Automatically Generate JSON Schema #4227

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 31 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,31 @@ jobs:
run: grep -r --include "*.rs" --files-without-match "@generated" crates | xargs rustfmt --check --config="skip_children=true"
working-directory: ./compiler/

build-config-schema:
name: Build Relay Compiler Config JSON Schema
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.64.0
override: true

- uses: actions-rs/cargo@v1
with:
command: build
args: --features vendored --manifest-path compiler/Cargo.toml --bin relay-config-schema --release

- name: Generate JSON Schema
run: |
./compiler/target/release/relay-config-schema > relay-config-schema.json;
node scripts/fixJsonSchema.js;
- name: Upload JSON Schema
uses: actions/upload-artifact@v2
with:
name: relay-config-schema.json
path: ./relay-config-schema.json

build-compiler:
name: Build Rust Compiler (${{ matrix.target.os }})
strategy:
Expand Down Expand Up @@ -218,7 +243,7 @@ jobs:
name: Publish to NPM
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.repository == 'facebook/relay'
needs: [js-tests, js-lint, typecheck, build-tests, build-compiler]
needs: [js-tests, js-lint, typecheck, build-tests, build-compiler, build-config-schema]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down Expand Up @@ -253,6 +278,11 @@ jobs:
with:
name: relay-bin-win-x64
path: artifacts/win-x64
- name: Download artifact relay-config-schema
uses: actions/download-artifact@v2
with:
name: relay-config-schema.json
path: artifacts/relay-config-schmea.json
- name: Mark binaries as executable
working-directory: artifacts
run: |
Expand Down
68 changes: 68 additions & 0 deletions compiler/Cargo.lock

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

1 change: 1 addition & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ members = [
"crates/schema-print",
"crates/schema-validate",
"crates/signedsource",
"crates/relay-config-schema"
]
1 change: 1 addition & 0 deletions compiler/crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ log = { version = "0.4.17", features = ["kv_unstable", "kv_unstable_std"] }
lsp-types = "0.93.2"
md-5 = "0.10"
rayon = "1.2"
schemars = {version = "0.8.11", features = ["indexmap1"] }
serde = { version = "1.0.136", features = ["derive", "rc"] }
serde_json = { version = "1.0.79", features = ["float_roundtrip", "unbounded_depth"] }
7 changes: 4 additions & 3 deletions compiler/crates/common/src/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use std::fmt::Result as FmtResult;
use indexmap::IndexSet;
use intern::string_key::StringKey;
use intern::Lookup;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use crate::Rollout;
use crate::rollout::Rollout;

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[derive(Default, Debug, Serialize, Deserialize, Clone, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct FeatureFlags {
#[serde(default)]
Expand Down Expand Up @@ -77,7 +78,7 @@ pub struct FeatureFlags {
pub emit_normalization_nodes_for_client_edges: bool,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
#[derive(Debug, Deserialize, Clone, Serialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum FeatureFlag {
/// Fully disabled: developers may not use this feature
Expand Down
4 changes: 3 additions & 1 deletion compiler/crates/common/src/named_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use intern::impl_lookup;
use intern::string_key::Intern;
use intern::string_key::StringKey;
use intern::Lookup;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -45,7 +46,8 @@ pub trait Named {
Ord,
PartialEq,
PartialOrd,
Serialize
Serialize,
JsonSchema
)]
pub struct DirectiveName(pub StringKey);

Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/common/src/rollout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

use md5::Digest;
use md5::Md5;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

/// A utility to enable gradual rollout of large codegen changes.
/// Can be constructed as the Default which passes or a percentage between 0 and
/// 100.
#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy)]
#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, JsonSchema)]
pub struct Rollout(Option<u8>);

impl Rollout {
Expand Down
1 change: 1 addition & 0 deletions compiler/crates/intern/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = { version = "1.0.136", features = ["derive", "rc"] }
serde_bytes = "0.11"
serde_derive = "1.0"
smallvec = { version = "1.6.1", features = ["serde", "union"] }
schemars = {version = "0.8.11" }

[dev-dependencies]
bincode = "1.3.3"
Expand Down
17 changes: 17 additions & 0 deletions compiler/crates/intern/src/string_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::fmt::Formatter;
use std::str::FromStr;

use indexmap::IndexMap;
use schemars::schema::InstanceType;
use schemars::schema::SchemaObject;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
Expand Down Expand Up @@ -55,6 +58,20 @@ impl StringKey {
}
}

impl JsonSchema for StringKey {
fn schema_name() -> String {
"StringKey".to_owned()
}

fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
..Default::default()
}
.into()
}
}

impl Lookup for StringKey {
fn lookup(self) -> &'static str {
self.0.as_str()
Expand Down
1 change: 1 addition & 0 deletions compiler/crates/relay-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ relay-transforms = { path = "../relay-transforms" }
relay-typegen = { path = "../relay-typegen" }
schema = { path = "../schema" }
schema-diff = { path = "../schema-diff" }
schemars = { version = "0.8.11", features = ["indexmap1"]}
serde = { version = "1.0.136", features = ["derive", "rc"] }
serde_bser = "0.3"
serde_json = { version = "1.0.79", features = ["float_roundtrip", "unbounded_depth"] }
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/relay-compiler/src/compiler_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use relay_config::SchemaConfig;
use schema::SDLSchema;
use schema_diff::definitions::SchemaChange;
use schema_diff::detect_changes;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use zstd::stream::read::Decoder as ZstdDecoder;
Expand All @@ -58,7 +59,7 @@ use crate::file_source::SourceControlUpdateStatus;
pub type ProjectName = StringKey;

/// Set of project names.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, JsonSchema)]
#[serde(from = "DeserializableProjectSet")]
pub struct ProjectSet(Vec<ProjectName>);

Expand Down
Loading