Skip to content

Commit

Permalink
Apply fragment renaming to resolver fields
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D50847113

fbshipit-source-id: 7471f235d5beb436de56eaf0aa25dd5971df2e73
  • Loading branch information
captbaritone authored and facebook-github-bot committed Oct 31, 2023
1 parent 22a3c89 commit 8e0949d
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
==================================== INPUT ====================================
query fragmentArgPassedToResolverRutimeArg_MeQuery($globalArg: String!) {
...fragmentArgPassedToResolverRutimeArgFragment
@arguments(fragmentArg: $globalArg)
}

fragment fragmentArgPassedToResolverRutimeArgFragment on Query
@argumentDefinitions(fragmentArg: {type: "String!"}) {
resolver_field(resolverArg: $fragmentArg)
}

# %extensions%

extend type Query {
resolver_field(resolverArg: String!): String
@relay_resolver(import_path: "./path/to/Resolver.js")
}
==================================== OUTPUT ===================================
{
"fragment": {
"argumentDefinitions": [
{
"defaultValue": null,
"kind": "LocalArgument",
"name": "globalArg"
}
],
"kind": "Fragment",
"metadata": null,
"name": "fragmentArgPassedToResolverRutimeArg_MeQuery",
"selections": [
{
"args": [
{
"kind": "Variable",
"name": "fragmentArg",
"variableName": "globalArg"
}
],
"kind": "FragmentSpread",
"name": "fragmentArgPassedToResolverRutimeArgFragment"
}
],
"type": "Query",
"abstractKey": null
},
"kind": "Request",
"operation": {
"argumentDefinitions": [
{
"defaultValue": null,
"kind": "LocalArgument",
"name": "globalArg"
}
],
"kind": "Operation",
"name": "fragmentArgPassedToResolverRutimeArg_MeQuery",
"selections": [
{
"kind": "ClientExtension",
"selections": [
{
"name": "resolver_field",
"args": [
{
"kind": "Variable",
"name": "resolverArg",
"variableName": "globalArg"
}
],
"fragment": null,
"kind": "RelayResolver",
"storageKey": null,
"isOutputType": false
}
]
}
]
},
"params": {
"cacheID": "e6fab15386443f5a4c89366a5d006c7b",
"id": null,
"metadata": {},
"name": "fragmentArgPassedToResolverRutimeArg_MeQuery",
"operationKind": "query",
"text": null
}
}

QUERY:

Query Text is Empty.

{
"argumentDefinitions": [
{
"defaultValue": null,
"kind": "LocalArgument",
"name": "fragmentArg"
}
],
"kind": "Fragment",
"metadata": null,
"name": "fragmentArgPassedToResolverRutimeArgFragment",
"selections": [
{
"kind": "ClientExtension",
"selections": [
{
"alias": null,
"args": [
{
"kind": "Variable",
"name": "resolverArg",
"variableName": "fragmentArg"
}
],
"fragment": null,
"kind": "RelayResolver",
"name": "resolver_field",
"resolverModule": require('Resolver'),
"path": "resolver_field"
}
]
}
],
"type": "Query",
"abstractKey": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query fragmentArgPassedToResolverRutimeArg_MeQuery($globalArg: String!) {
...fragmentArgPassedToResolverRutimeArgFragment
@arguments(fragmentArg: $globalArg)
}

fragment fragmentArgPassedToResolverRutimeArgFragment on Query
@argumentDefinitions(fragmentArg: {type: "String!"}) {
resolver_field(resolverArg: $fragmentArg)
}

# %extensions%

extend type Query {
resolver_field(resolverArg: String!): String
@relay_resolver(import_path: "./path/to/Resolver.js")
}
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<<0dc87fb7f406a49cc9b3c8ab491d6815>>
* @generated SignedSource<<4a85869bb6831356ca25ffc4ee7af4fb>>
*/

mod compile_relay_artifacts;
Expand Down Expand Up @@ -488,6 +488,13 @@ async fn fragment_alias_on_inline_fragment_does_not_get_flattened() {
test_fixture(transform_fixture, "fragment-alias-on-inline-fragment-does-not-get-flattened.graphql", "compile_relay_artifacts/fixtures/fragment-alias-on-inline-fragment-does-not-get-flattened.expected", input, expected).await;
}

#[tokio::test]
async fn fragment_arg_passed_to_resolver_rutime_arg() {
let input = include_str!("compile_relay_artifacts/fixtures/fragment-arg-passed-to-resolver-rutime-arg.graphql");
let expected = include_str!("compile_relay_artifacts/fixtures/fragment-arg-passed-to-resolver-rutime-arg.expected");
test_fixture(transform_fixture, "fragment-arg-passed-to-resolver-rutime-arg.graphql", "compile_relay_artifacts/fixtures/fragment-arg-passed-to-resolver-rutime-arg.expected", input, expected).await;
}

#[tokio::test]
async fn fragment_on_node_interface() {
let input = include_str!("compile_relay_artifacts/fixtures/fragment-on-node-interface.graphql");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use crate::no_inline::NO_INLINE_DIRECTIVE_NAME;
use crate::no_inline::PARENT_DOCUMENTS_ARG;
use crate::util::get_normalization_operation_name;
use crate::RawResponseGenerationMode;
use crate::RelayResolverMetadata;

/// A transform that converts a set of documents containing fragments/fragment
/// spreads *with* arguments to one where all arguments have been inlined. This
Expand Down Expand Up @@ -300,7 +301,7 @@ impl Transformer for ApplyFragmentArgumentsTransform<'_, '_, '_> {
}));
// If the fragment type is abstract, we need to ensure that it's only evaluated at runtime if the
// type of the object matches the fragment's type condition. Rather than reimplement type refinement
// for fragment spreads, we wrap the fragment spread in an inlinefragment (which may be inlined away)
// for fragment spreads, we wrap the fragment spread in an inline fragment (which may be inlined away)
// that ensures it will go through type-refinement at runtime.
return if fragment.type_condition.is_abstract_type() {
Transformed::Replace(Selection::InlineFragment(Arc::new(InlineFragment {
Expand Down Expand Up @@ -338,6 +339,24 @@ impl Transformer for ApplyFragmentArgumentsTransform<'_, '_, '_> {
})
}

fn transform_directive(&mut self, directive: &Directive) -> Transformed<Directive> {
if directive.name.item == RelayResolverMetadata::directive_name() {
if let Some(resolver_metadata) = RelayResolverMetadata::from(directive) {
return self
.transform_arguments(&resolver_metadata.field_arguments)
.map(|new_args| {
RelayResolverMetadata {
field_arguments: new_args,
..resolver_metadata.clone()
}
.into()
})
.into();
}
}
self.default_transform_directive(directive)
}

fn transform_value(&mut self, value: &Value) -> TransformedValue<Value> {
match value {
Value::Variable(prev_variable) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-transforms/src/relay_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ associated_data_impl!(RelayResolverFieldMetadata);

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct RelayResolverMetadata {
field_id: FieldID,
pub field_id: FieldID,
pub import_path: StringKey,
pub import_name: Option<StringKey>,
pub field_alias: Option<StringKey>,
Expand Down

0 comments on commit 8e0949d

Please sign in to comment.