Skip to content

Commit

Permalink
Evaluate custom scalars first (#3671)
Browse files Browse the repository at this point in the history
Summary:
I have a custom scalar type on a private project that replaces the graphal `ID` type with a Flow `newtype`. The JS relay compiler supported the ability to replace built-ins, but the new Rust compiler seems to lack this ability.

With this change, custom scalars are evaluated first, then built-ins, then `any` (or panic). AFAIK this matches the old compiler's behavior.

My `cargo fmt` is not working right now for some reason, so I present this patch unformatted. If GitHub Actions doesn't complain, I assume Meta-internal formatters will catch and fix this if necessary.

Pull Request resolved: #3671

Test Plan:
`cargo test` passes

`cargo run --bin relay -- ../../../myproject/relay.config.json` now replaces `ID` types with `GraphQLID` instead of `string` as it does in the rc0 version

Reviewed By: alunyov

Differential Revision: D32859584

Pulled By: bigfootjon

fbshipit-source-id: 1a1d4b38d14359ecfdbe8f2e2f73d2fc24f519f5
  • Loading branch information
bigfootjon authored and facebook-github-bot committed Dec 6, 2021
1 parent 9157236 commit a0160e8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
8 changes: 3 additions & 5 deletions compiler/crates/relay-typegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,16 +1228,14 @@ impl<'a> TypeGenerator<'a> {

fn transform_graphql_scalar_type(&mut self, scalar: ScalarID) -> AST {
let scalar_name = self.schema.scalar(scalar).name;
if scalar_name == *TYPE_ID || scalar_name == *TYPE_STRING {
if let Some(&custom_scalar) = self.typegen_config.custom_scalar_types.get(&scalar_name) {
AST::RawType(custom_scalar)
} else if scalar_name == *TYPE_ID || scalar_name == *TYPE_STRING {
AST::String
} else if scalar_name == *TYPE_FLOAT || scalar_name == *TYPE_INT {
AST::Number
} else if scalar_name == *TYPE_BOOLEAN {
AST::Boolean
} else if let Some(&custom_scalar) =
self.typegen_config.custom_scalar_types.get(&scalar_name)
{
AST::RawType(custom_scalar)
} else {
if self.typegen_config.require_custom_scalar_types {
panic!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fragment FriendFragment on User {
import type { FriendFragment$fragmentType } from "FriendFragment.graphql";
export type ExampleQuery$variables = {|
id: string,
condition: boolean,
condition: CustomBoolean,
|};
export type ExampleQuery$data = {|
+node: ?{|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type TestDefer$rawResponse = {|
|}>,
+pageInfo: ?{|
+endCursor: ?string,
+hasNextPage: ?boolean,
+hasNextPage: ?CustomBoolean,
|},
|},
|} | {|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type TestStream$rawResponse = {|
|}>,
+pageInfo: ?{|
+endCursor: ?string,
+hasNextPage: ?boolean,
+hasNextPage: ?CustomBoolean,
|},
|},
|} | {|
Expand Down
8 changes: 7 additions & 1 deletion compiler/crates/relay-typegen/tests/generate_flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

use common::{ConsoleLogger, FeatureFlag, FeatureFlags, SourceLocationKey};
use fixture_tests::Fixture;
use fnv::FnvHashMap;
use fnv::{FnvBuildHasher, FnvHashMap};
use graphql_ir::{build, Program};
use graphql_syntax::parse_executable;
use indexmap::IndexMap;
use intern::string_key::Intern;
use relay_codegen::JsModuleFormat;
use relay_test_schema::{get_test_schema, get_test_schema_with_extensions};
use relay_transforms::{apply_transforms, ConnectionInterface};
use relay_typegen::{self, TypegenConfig, TypegenLanguage};
use std::sync::Arc;

type FnvIndexMap<K, V> = IndexMap<K, V, FnvBuildHasher>;

pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let parts = fixture.content.split("%extensions%").collect::<Vec<_>>();
let (source, schema) = match parts.as_slice() {
Expand Down Expand Up @@ -56,8 +59,11 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {

let js_module_format = JsModuleFormat::Haste;
let has_unified_output = false;
let mut custom_scalar_types = FnvIndexMap::default();
custom_scalar_types.insert("Boolean".intern(), "CustomBoolean".intern());
let typegen_config = TypegenConfig {
language: TypegenLanguage::Flow,
custom_scalar_types,
..Default::default()
};

Expand Down

0 comments on commit a0160e8

Please sign in to comment.