Skip to content

Commit 904924c

Browse files
Alex Danofffacebook-github-bot
authored andcommitted
add validations for mutation resolvers
Reviewed By: alunyov Differential Revision: D50993473 fbshipit-source-id: bc0804753f8668a83f06dff00c259e22620a353b
1 parent ef84550 commit 904924c

17 files changed

+598
-12
lines changed

compiler/crates/common/src/feature_flags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ pub struct FeatureFlags {
8080
/// Allow legacy verbose resolver syntax
8181
#[serde(default)]
8282
pub relay_resolvers_allow_legacy_verbose_syntax: FeatureFlag,
83+
84+
/// Allow relay resolvers to extend the Mutation type
85+
#[serde(default)]
86+
pub enable_relay_resolver_mutations: bool,
8387
}
8488

8589
#[derive(Debug, Deserialize, Clone, Serialize)]

compiler/crates/relay-compiler/src/build_project/build_resolvers_schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ fn extend_schema_with_types(
9999
}
100100

101101
/// Extend the schema with resolver fields
102-
fn extend_schema_with_fields<'a>(
102+
fn extend_schema_with_fields(
103103
schema: &mut SDLSchema,
104104
project_config: &ProjectConfig,
105-
field_asts_and_definitions: FieldAstsAndDefinitions<'a>,
105+
field_asts_and_definitions: FieldAstsAndDefinitions<'_>,
106106
is_base_project: bool,
107107
) -> DiagnosticsResult<()> {
108108
let field_definitions = try_all(field_asts_and_definitions.0.into_iter().map(

compiler/crates/relay-compiler/src/docblocks.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ fn parse_source(
8989
)?;
9090
maybe_ir
9191
.map(|ir| {
92-
ir.to_graphql_schema_ast(project_config.name, schema, &project_config.schema_config)
92+
ir.to_graphql_schema_ast(
93+
project_config.name,
94+
schema,
95+
&project_config.schema_config,
96+
&project_config.feature_flags,
97+
)
9398
})
9499
.transpose()
95100
}

compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String>
121121
enable_schema_resolvers: false,
122122
relay_resolvers_enable_strict_resolver_flavors: FeatureFlag::Disabled,
123123
relay_resolvers_allow_legacy_verbose_syntax: FeatureFlag::Disabled,
124+
enable_relay_resolver_mutations: false,
124125
};
125126

126127
let default_project_config = ProjectConfig {

compiler/crates/relay-compiler/tests/compile_relay_artifacts_with_custom_id/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String>
9999
enable_schema_resolvers: false,
100100
relay_resolvers_enable_strict_resolver_flavors: FeatureFlag::Disabled,
101101
relay_resolvers_allow_legacy_verbose_syntax: FeatureFlag::Disabled,
102+
enable_relay_resolver_mutations: false,
102103
};
103104

104105
let default_schema_config = SchemaConfig::default();

compiler/crates/relay-compiler/tests/relay_compiler_integration/fixtures/client_mutation_resolver.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ graphql`mutation barMutation {
1515
"schema": "./schema.graphql",
1616
"eagerEsModules": true,
1717
"featureFlags": {
18-
"enable_relay_resolver_transform": true
18+
"enable_relay_resolver_transform": true,
19+
"enable_relay_resolver_mutations": true
1920
}
2021
}
2122

compiler/crates/relay-compiler/tests/relay_compiler_integration/fixtures/client_mutation_resolver.input

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ graphql`mutation barMutation {
1414
"schema": "./schema.graphql",
1515
"eagerEsModules": true,
1616
"featureFlags": {
17-
"enable_relay_resolver_transform": true
17+
"enable_relay_resolver_transform": true,
18+
"enable_relay_resolver_mutations": true
1819
}
1920
}
2021

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
==================================== INPUT ====================================
2+
//- foo.js
3+
/**
4+
* @RelayResolver Mutation.foo_mutation: Person
5+
*/
6+
7+
/**
8+
* @RelayResolver NotCalledMutation.baz_mutation: Boolean
9+
*/
10+
11+
//- bar.js
12+
graphql`mutation barMutation {
13+
baz_mutation
14+
}`
15+
16+
//- relay.config.json
17+
{
18+
"language": "flow",
19+
"schema": "./schema.graphql",
20+
"eagerEsModules": true,
21+
"featureFlags": {
22+
"enable_relay_resolver_transform": true,
23+
"enable_relay_resolver_mutations": true
24+
},
25+
"schemaExtensions": [
26+
"./extensions.graphql"
27+
]
28+
}
29+
30+
//- schema.graphql
31+
32+
type Mutation
33+
34+
type NotCalledMutation
35+
36+
type Query
37+
38+
schema {
39+
query: Query,
40+
mutation: NotCalledMutation,
41+
}
42+
43+
//- extensions.graphql
44+
45+
type Person {
46+
name: String!
47+
age: Int!
48+
}
49+
==================================== OUTPUT ===================================
50+
//- __generated__/Mutation__foo_mutation$normalization.graphql.js
51+
/**
52+
* <auto-generated> SignedSource<<87fb321a30684890a8b77e1bdb2cf79a>>
53+
* @flow
54+
* @lightSyntaxTransform
55+
* @nogrep
56+
*/
57+
58+
/* eslint-disable */
59+
60+
'use strict';
61+
62+
/*::
63+
import type { NormalizationSplitOperation } from 'relay-runtime';
64+
65+
export type Mutation__foo_mutation$normalization = {|
66+
+age: number,
67+
+name: string,
68+
|};
69+
70+
*/
71+
72+
var node/*: NormalizationSplitOperation*/ = {
73+
"kind": "SplitOperation",
74+
"metadata": {},
75+
"name": "Mutation__foo_mutation$normalization",
76+
"selections": [
77+
{
78+
"kind": "ClientExtension",
79+
"selections": [
80+
{
81+
"alias": null,
82+
"args": null,
83+
"kind": "ScalarField",
84+
"name": "name",
85+
"storageKey": null
86+
},
87+
{
88+
"alias": null,
89+
"args": null,
90+
"kind": "ScalarField",
91+
"name": "age",
92+
"storageKey": null
93+
}
94+
]
95+
}
96+
]
97+
};
98+
99+
export default node;
100+
101+
//- __generated__/barMutation.graphql.js
102+
/**
103+
* <auto-generated> SignedSource<<6f79ca8d7718498c480388c5b22a5130>>
104+
* @flow
105+
* @lightSyntaxTransform
106+
* @nogrep
107+
*/
108+
109+
/* eslint-disable */
110+
111+
'use strict';
112+
113+
/*::
114+
import type { ClientRequest, Mutation } from 'relay-runtime';
115+
import {baz_mutation as notCalledMutationBazMutationResolverType} from "../foo.js";
116+
// Type assertion validating that `notCalledMutationBazMutationResolverType` resolver is correctly implemented.
117+
// A type error here indicates that the type signature of the resolver module is incorrect.
118+
(notCalledMutationBazMutationResolverType: () => ?boolean);
119+
export type barMutation$variables = {||};
120+
export type barMutation$data = {|
121+
+baz_mutation: ?boolean,
122+
|};
123+
export type barMutation = {|
124+
response: barMutation$data,
125+
variables: barMutation$variables,
126+
|};
127+
*/
128+
129+
import {baz_mutation as notCalledMutationBazMutationResolver} from './../foo';
130+
131+
var node/*: ClientRequest*/ = {
132+
"fragment": {
133+
"argumentDefinitions": [],
134+
"kind": "Fragment",
135+
"metadata": null,
136+
"name": "barMutation",
137+
"selections": [
138+
{
139+
"kind": "ClientExtension",
140+
"selections": [
141+
{
142+
"alias": null,
143+
"args": null,
144+
"fragment": null,
145+
"kind": "RelayResolver",
146+
"name": "baz_mutation",
147+
"resolverModule": notCalledMutationBazMutationResolver,
148+
"path": "baz_mutation"
149+
}
150+
]
151+
}
152+
],
153+
"type": "NotCalledMutation",
154+
"abstractKey": null
155+
},
156+
"kind": "Request",
157+
"operation": {
158+
"argumentDefinitions": [],
159+
"kind": "Operation",
160+
"name": "barMutation",
161+
"selections": [
162+
{
163+
"kind": "ClientExtension",
164+
"selections": [
165+
{
166+
"name": "baz_mutation",
167+
"args": null,
168+
"fragment": null,
169+
"kind": "RelayResolver",
170+
"storageKey": null,
171+
"isOutputType": true
172+
}
173+
]
174+
}
175+
]
176+
},
177+
"params": {
178+
"cacheID": "b7409af7b3a13247f776ad3f9192c84b",
179+
"id": null,
180+
"metadata": {},
181+
"name": "barMutation",
182+
"operationKind": "mutation",
183+
"text": null
184+
}
185+
};
186+
187+
(node/*: any*/).hash = "bbe24bc0c4cab0597abfafef11f07b88";
188+
189+
export default ((node/*: any*/)/*: Mutation<
190+
barMutation$variables,
191+
barMutation$data,
192+
>*/);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//- foo.js
2+
/**
3+
* @RelayResolver Mutation.foo_mutation: Person
4+
*/
5+
6+
/**
7+
* @RelayResolver NotCalledMutation.baz_mutation: Boolean
8+
*/
9+
10+
//- bar.js
11+
graphql`mutation barMutation {
12+
baz_mutation
13+
}`
14+
15+
//- relay.config.json
16+
{
17+
"language": "flow",
18+
"schema": "./schema.graphql",
19+
"eagerEsModules": true,
20+
"featureFlags": {
21+
"enable_relay_resolver_transform": true,
22+
"enable_relay_resolver_mutations": true
23+
},
24+
"schemaExtensions": [
25+
"./extensions.graphql"
26+
]
27+
}
28+
29+
//- schema.graphql
30+
31+
type Mutation
32+
33+
type NotCalledMutation
34+
35+
type Query
36+
37+
schema {
38+
query: Query,
39+
mutation: NotCalledMutation,
40+
}
41+
42+
//- extensions.graphql
43+
44+
type Person {
45+
name: String!
46+
age: Int!
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
==================================== INPUT ====================================
2+
//- foo.js
3+
/**
4+
* @RelayResolver Mutation.foo_mutation: Boolean
5+
*/
6+
7+
//- bar.js
8+
graphql`mutation barMutation {
9+
foo_mutation
10+
}`
11+
12+
//- relay.config.json
13+
{
14+
"language": "flow",
15+
"schema": "./schema.graphql",
16+
"eagerEsModules": true,
17+
"featureFlags": {
18+
"enable_relay_resolver_transform": true
19+
}
20+
}
21+
22+
//- schema.graphql
23+
24+
type Mutation
25+
==================================== OUTPUT ===================================
26+
✖︎ Resolvers on the mutation type Mutation are disallowed without the enable_relay_resolver_mutations feature flag
27+
28+
foo.js:1:3
29+
1 │ *
30+
│ ^
31+
2 │ * @RelayResolver Mutation.foo_mutation: Boolean
32+
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
3 │

0 commit comments

Comments
 (0)