Skip to content

Commit

Permalink
Document current behavior of conditional + requried
Browse files Browse the repository at this point in the history
Reviewed By: tyao1

Differential Revision: D58797795

fbshipit-source-id: a07e3125af00d064d6d73cc7ff98ddfe09c6c4bd
  • Loading branch information
captbaritone authored and facebook-github-bot committed Jun 21, 2024
1 parent 7bd521e commit fc5894a
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
==================================== INPUT ====================================
fragment requiredDirectiveOnConditionalField_Foo_user on User {
id
name @required(action: THROW) @include(if: $condition)
}
==================================== OUTPUT ===================================
{
"argumentDefinitions": [
{
"kind": "RootArgument",
"name": "condition"
}
],
"kind": "Fragment",
"metadata": null,
"name": "requiredDirectiveOnConditionalField_Foo_user",
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "id",
"storageKey": null
},
{
"condition": "condition",
"kind": "Condition",
"passingValue": true,
"selections": [
{
"kind": "RequiredField",
"field": {
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "name",
"storageKey": null
},
"action": "THROW",
"path": "name"
}
]
}
],
"type": "User",
"abstractKey": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fragment requiredDirectiveOnConditionalField_Foo_user on User {
id
name @required(action: THROW) @include(if: $condition)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<a462cf5b6b402f99514438fe2a6c56fe>>
* @generated SignedSource<<1cbf5b06f3aa5678cb1b1d37cea9c275>>
*/

mod compile_relay_artifacts;
Expand Down Expand Up @@ -1510,6 +1510,13 @@ async fn required_directive() {
test_fixture(transform_fixture, file!(), "required-directive.graphql", "compile_relay_artifacts/fixtures/required-directive.expected", input, expected).await;
}

#[tokio::test]
async fn required_directive_on_conditional_field() {
let input = include_str!("compile_relay_artifacts/fixtures/required-directive-on-conditional-field.graphql");
let expected = include_str!("compile_relay_artifacts/fixtures/required-directive-on-conditional-field.expected");
test_fixture(transform_fixture, file!(), "required-directive-on-conditional-field.graphql", "compile_relay_artifacts/fixtures/required-directive-on-conditional-field.expected", input, expected).await;
}

#[tokio::test]
async fn resolver_field_with_all_fragment_args_omitted() {
let input = include_str!("compile_relay_artifacts/fixtures/resolver-field-with-all-fragment-args-omitted.graphql");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
==================================== INPUT ====================================
//- foo.js
graphql`
fragment foo on User {
name @required(action: THROW) @skip(if: $condition)
}`;

//- relay.config.json
{
"language": "flow",
"schema": "./schema.graphql"
}

//- schema.graphql
type Query { me: User }
type User { name: String }
==================================== OUTPUT ===================================
//- __generated__/foo.graphql.js
/**
* <auto-generated> SignedSource<<e856535e023f8ce3347fff54074089de>>
* @flow
* @lightSyntaxTransform
* @nogrep
*/

/* eslint-disable */

'use strict';

/*::
import type { Fragment, ReaderFragment } from 'relay-runtime';
import type { FragmentType } from "relay-runtime";
declare export opaque type foo$fragmentType: FragmentType;
export type foo$data = {|
+name?: string,
+$fragmentType: foo$fragmentType,
|};
export type foo$key = {
+$data?: foo$data,
+$fragmentSpreads: foo$fragmentType,
...
};
*/

var node/*: ReaderFragment*/ = {
"argumentDefinitions": [
{
"kind": "RootArgument",
"name": "condition"
}
],
"kind": "Fragment",
"metadata": null,
"name": "foo",
"selections": [
{
"condition": "condition",
"kind": "Condition",
"passingValue": false,
"selections": [
{
"kind": "RequiredField",
"field": {
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "name",
"storageKey": null
},
"action": "THROW",
"path": "name"
}
]
}
],
"type": "User",
"abstractKey": null
};

(node/*: any*/).hash = "9315d3c00a7787d5cc3f0930cf1ee1a5";

module.exports = ((node/*: any*/)/*: Fragment<
foo$fragmentType,
foo$data,
>*/);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//- foo.js
graphql`
fragment foo on User {
name @required(action: THROW) @skip(if: $condition)
}`;

//- relay.config.json
{
"language": "flow",
"schema": "./schema.graphql"
}

//- schema.graphql
type Query { me: User }
type User { name: String }
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<4ed46c8869542d0837a520ae742aa619>>
* @generated SignedSource<<2d11c317125ba80603db0a7c52cf523e>>
*/

mod relay_compiler_integration;
Expand Down Expand Up @@ -131,6 +131,13 @@ async fn preloadable_query_typescript() {
test_fixture(transform_fixture, file!(), "preloadable_query_typescript.input", "relay_compiler_integration/fixtures/preloadable_query_typescript.expected", input, expected).await;
}

#[tokio::test]
async fn required_conditional_field() {
let input = include_str!("relay_compiler_integration/fixtures/required_conditional_field.input");
let expected = include_str!("relay_compiler_integration/fixtures/required_conditional_field.expected");
test_fixture(transform_fixture, file!(), "required_conditional_field.input", "relay_compiler_integration/fixtures/required_conditional_field.expected", input, expected).await;
}

#[tokio::test]
async fn resolver_on_interface() {
let input = include_str!("relay_compiler_integration/fixtures/resolver_on_interface.input");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
==================================== INPUT ====================================
query FooQuery($condition: Boolean!) {
me {
firstName
lastName @required(action: LOG) @skip(if: $condition)
}
}
==================================== OUTPUT ===================================
export type FooQuery$variables = {|
condition: CustomBoolean,
|};
export type FooQuery$data = {|
+me: ?{|
+firstName: ?string,
+lastName?: string,
|},
|};
export type FooQuery = {|
response: FooQuery$data,
variables: FooQuery$variables,
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query FooQuery($condition: Boolean!) {
me {
firstName
lastName @required(action: LOG) @skip(if: $condition)
}
}
9 changes: 8 additions & 1 deletion compiler/crates/relay-typegen/tests/generate_flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<fbad3b8f1e54b6a58a0190aff49f96bc>>
* @generated SignedSource<<1630011fc826a0199de34bf6ba1fc24d>>
*/

mod generate_flow;
Expand Down Expand Up @@ -614,6 +614,13 @@ async fn required_chain_bubbles_to_non_null_linked_field_through_inline_fragment
test_fixture(transform_fixture, file!(), "required-chain-bubbles-to-non-null-linked-field-through-inline-fragment.graphql", "generate_flow/fixtures/required-chain-bubbles-to-non-null-linked-field-through-inline-fragment.expected", input, expected).await;
}

#[tokio::test]
async fn required_conditional() {
let input = include_str!("generate_flow/fixtures/required-conditional.graphql");
let expected = include_str!("generate_flow/fixtures/required-conditional.expected");
test_fixture(transform_fixture, file!(), "required-conditional.graphql", "generate_flow/fixtures/required-conditional.expected", input, expected).await;
}

#[tokio::test]
async fn required_isolates_concrete_inline_fragments() {
let input = include_str!("generate_flow/fixtures/required-isolates-concrete-inline-fragments.graphql");
Expand Down

0 comments on commit fc5894a

Please sign in to comment.