Skip to content

Commit

Permalink
Support boolean
Browse files Browse the repository at this point in the history
Reviewed By: captbaritone

Differential Revision: D57081977

fbshipit-source-id: 39d93f228adeda6a96d7fb0fd71747fa43ae6354
  • Loading branch information
tyao1 authored and facebook-github-bot committed May 15, 2024
1 parent 24c9a53 commit 5052062
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 8 deletions.
27 changes: 20 additions & 7 deletions compiler/crates/relay-schema-generation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,7 @@ impl RelayResolverExtractor {
(flow_return_type, None)
}
}
FlowTypeAnnotation::StringTypeAnnotation(_) => (flow_return_type, None),
FlowTypeAnnotation::NumberTypeAnnotation(_) => (flow_return_type, None),
_ => {
// Only support named types in function returns
return self.error_result(SchemaGenerationError::UnsupportedType, flow_return_type);
}
_ => (flow_return_type, None),
};

if node.params.is_empty() {
Expand Down Expand Up @@ -793,9 +788,27 @@ fn return_type_to_type_annotation(
name: string_key_to_identifier(identifier),
})
}
FlowTypeAnnotation::BooleanTypeAnnotation(node) => {
let identifier = WithLocation {
item: intern!("Boolean"),
location: to_location(source_location, node.as_ref()),
};
TypeAnnotation::Named(NamedTypeAnnotation {
name: string_key_to_identifier(identifier),
})
}
FlowTypeAnnotation::BooleanLiteralTypeAnnotation(node) => {
let identifier = WithLocation {
item: intern!("Boolean"),
location: to_location(source_location, node.as_ref()),
};
TypeAnnotation::Named(NamedTypeAnnotation {
name: string_key_to_identifier(identifier),
})
}
_ => {
return Err(vec![Diagnostic::error(
SchemaGenerationError::TODO,
SchemaGenerationError::UnsupportedType,
location,
)]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
==================================== INPUT ====================================
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

//- module.js

import type CatFlowType from 'Cat';

/**
* @RelayResolver
*/
export function Cat(id: DataID): CatFlowType {
return {};
}


/**
* @RelayResolver
*/
export function ages(cat: CatFlowType): ?number {
return 2;
}


/**
* @RelayResolver
*/
export function hungry1(cat: CatFlowType): ?boolean {
return false;
}

/**
* @RelayResolver
*/
export function hungry2(cat: CatFlowType): ?false {
return false;
}

/**
* @RelayResolver
*/
export function hungry3(cat: CatFlowType): true {
return false;
}
==================================== OUTPUT ===================================
StrongObjectResolver(
StrongObjectIr {
type_name: Identifier {
span: 272:275,
token: Token {
span: 272:275,
kind: Identifier,
},
value: "Cat",
},
rhs_location: module.js:272:275,
root_fragment: WithLocation {
location: module.js:272:275,
item: FragmentDefinitionName(
"Cat__id",
),
},
description: None,
deprecated: None,
live: None,
semantic_non_null: None,
location: module.js:272:275,
implements_interfaces: [],
source_hash: ResolverSourceHash(
"5e42d8055bf96703110b4485122e0e0f",
),
},
)
type Cat @__RelayResolverModel {
id: ID!
__relay_model_instance: RelayResolverValue! @relay_resolver(import_path: "module.js", fragment_name: "Cat__id", generated_fragment: true, inject_fragment_data: "id", import_name: "Cat") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f") @unselectable(reason: "This field is intended only for Relay's internal use")
}


TerseRelayResolver(
TerseRelayResolverIr {
field: FieldDefinition {
name: Identifier {
span: 362:366,
token: Token {
span: 362:366,
kind: Identifier,
},
value: "ages",
},
type_: Named(
NamedTypeAnnotation {
name: Identifier {
span: 387:393,
token: Token {
span: 387:393,
kind: Identifier,
},
value: "Float",
},
},
),
arguments: None,
directives: [],
description: None,
hack_source: None,
span: 362:366,
},
type_: WithLocation {
location: <generated>:272:275,
item: "Cat",
},
root_fragment: None,
deprecated: None,
semantic_non_null: None,
live: None,
location: module.js:362:366,
fragment_arguments: None,
source_hash: ResolverSourceHash(
"5e42d8055bf96703110b4485122e0e0f",
),
},
)
extend type Cat {
ages: Float @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "ages") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f")
}


TerseRelayResolver(
TerseRelayResolverIr {
field: FieldDefinition {
name: Identifier {
span: 454:461,
token: Token {
span: 454:461,
kind: Identifier,
},
value: "hungry1",
},
type_: Named(
NamedTypeAnnotation {
name: Identifier {
span: 482:489,
token: Token {
span: 482:489,
kind: Identifier,
},
value: "Boolean",
},
},
),
arguments: None,
directives: [],
description: None,
hack_source: None,
span: 454:461,
},
type_: WithLocation {
location: <generated>:272:275,
item: "Cat",
},
root_fragment: None,
deprecated: None,
semantic_non_null: None,
live: None,
location: module.js:454:461,
fragment_arguments: None,
source_hash: ResolverSourceHash(
"5e42d8055bf96703110b4485122e0e0f",
),
},
)
extend type Cat {
hungry1: Boolean @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "hungry1") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f")
}


TerseRelayResolver(
TerseRelayResolverIr {
field: FieldDefinition {
name: Identifier {
span: 553:560,
token: Token {
span: 553:560,
kind: Identifier,
},
value: "hungry2",
},
type_: Named(
NamedTypeAnnotation {
name: Identifier {
span: 581:586,
token: Token {
span: 581:586,
kind: Identifier,
},
value: "Boolean",
},
},
),
arguments: None,
directives: [],
description: None,
hack_source: None,
span: 553:560,
},
type_: WithLocation {
location: <generated>:272:275,
item: "Cat",
},
root_fragment: None,
deprecated: None,
semantic_non_null: None,
live: None,
location: module.js:553:560,
fragment_arguments: None,
source_hash: ResolverSourceHash(
"5e42d8055bf96703110b4485122e0e0f",
),
},
)
extend type Cat {
hungry2: Boolean @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "hungry2") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f")
}


TerseRelayResolver(
TerseRelayResolverIr {
field: FieldDefinition {
name: Identifier {
span: 650:657,
token: Token {
span: 650:657,
kind: Identifier,
},
value: "hungry3",
},
type_: NonNull(
NonNullTypeAnnotation {
span: 677:681,
type_: Named(
NamedTypeAnnotation {
name: Identifier {
span: 677:681,
token: Token {
span: 677:681,
kind: Identifier,
},
value: "Boolean",
},
},
),
exclamation: Token {
span: 0:0,
kind: Empty,
},
},
),
arguments: None,
directives: [],
description: None,
hack_source: None,
span: 650:657,
},
type_: WithLocation {
location: <generated>:272:275,
item: "Cat",
},
root_fragment: None,
deprecated: None,
semantic_non_null: None,
live: None,
location: module.js:650:657,
fragment_arguments: None,
source_hash: ResolverSourceHash(
"5e42d8055bf96703110b4485122e0e0f",
),
},
)
extend type Cat {
hungry3: Boolean! @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "hungry3") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

//- module.js

import type CatFlowType from 'Cat';

/**
* @RelayResolver
*/
export function Cat(id: DataID): CatFlowType {
return {};
}


/**
* @RelayResolver
*/
export function ages(cat: CatFlowType): ?number {
return 2;
}


/**
* @RelayResolver
*/
export function hungry1(cat: CatFlowType): ?boolean {
return false;
}

/**
* @RelayResolver
*/
export function hungry2(cat: CatFlowType): ?false {
return false;
}

/**
* @RelayResolver
*/
export function hungry3(cat: CatFlowType): true {
return false;
}
Loading

0 comments on commit 5052062

Please sign in to comment.