Skip to content

Commit

Permalink
add validations for mutation resolvers
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D50993473

fbshipit-source-id: bc0804753f8668a83f06dff00c259e22620a353b
  • Loading branch information
Alex Danoff authored and facebook-github-bot committed Nov 7, 2023
1 parent ef84550 commit 904924c
Show file tree
Hide file tree
Showing 17 changed files with 598 additions and 12 deletions.
4 changes: 4 additions & 0 deletions compiler/crates/common/src/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub struct FeatureFlags {
/// Allow legacy verbose resolver syntax
#[serde(default)]
pub relay_resolvers_allow_legacy_verbose_syntax: FeatureFlag,

/// Allow relay resolvers to extend the Mutation type
#[serde(default)]
pub enable_relay_resolver_mutations: bool,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ fn extend_schema_with_types(
}

/// Extend the schema with resolver fields
fn extend_schema_with_fields<'a>(
fn extend_schema_with_fields(
schema: &mut SDLSchema,
project_config: &ProjectConfig,
field_asts_and_definitions: FieldAstsAndDefinitions<'a>,
field_asts_and_definitions: FieldAstsAndDefinitions<'_>,
is_base_project: bool,
) -> DiagnosticsResult<()> {
let field_definitions = try_all(field_asts_and_definitions.0.into_iter().map(
Expand Down
7 changes: 6 additions & 1 deletion compiler/crates/relay-compiler/src/docblocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ fn parse_source(
)?;
maybe_ir
.map(|ir| {
ir.to_graphql_schema_ast(project_config.name, schema, &project_config.schema_config)
ir.to_graphql_schema_ast(
project_config.name,
schema,
&project_config.schema_config,
&project_config.feature_flags,
)
})
.transpose()
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String>
enable_schema_resolvers: false,
relay_resolvers_enable_strict_resolver_flavors: FeatureFlag::Disabled,
relay_resolvers_allow_legacy_verbose_syntax: FeatureFlag::Disabled,
enable_relay_resolver_mutations: false,
};

let default_project_config = ProjectConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String>
enable_schema_resolvers: false,
relay_resolvers_enable_strict_resolver_flavors: FeatureFlag::Disabled,
relay_resolvers_allow_legacy_verbose_syntax: FeatureFlag::Disabled,
enable_relay_resolver_mutations: false,
};

let default_schema_config = SchemaConfig::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ graphql`mutation barMutation {
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"enable_relay_resolver_transform": true
"enable_relay_resolver_transform": true,
"enable_relay_resolver_mutations": true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ graphql`mutation barMutation {
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"enable_relay_resolver_transform": true
"enable_relay_resolver_transform": true,
"enable_relay_resolver_mutations": true
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
==================================== INPUT ====================================
//- foo.js
/**
* @RelayResolver Mutation.foo_mutation: Person
*/

/**
* @RelayResolver NotCalledMutation.baz_mutation: Boolean
*/

//- bar.js
graphql`mutation barMutation {
baz_mutation
}`

//- relay.config.json
{
"language": "flow",
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"enable_relay_resolver_transform": true,
"enable_relay_resolver_mutations": true
},
"schemaExtensions": [
"./extensions.graphql"
]
}

//- schema.graphql

type Mutation

type NotCalledMutation

type Query

schema {
query: Query,
mutation: NotCalledMutation,
}

//- extensions.graphql

type Person {
name: String!
age: Int!
}
==================================== OUTPUT ===================================
//- __generated__/Mutation__foo_mutation$normalization.graphql.js
/**
* <auto-generated> SignedSource<<87fb321a30684890a8b77e1bdb2cf79a>>
* @flow
* @lightSyntaxTransform
* @nogrep
*/

/* eslint-disable */

'use strict';

/*::
import type { NormalizationSplitOperation } from 'relay-runtime';

export type Mutation__foo_mutation$normalization = {|
+age: number,
+name: string,
|};

*/

var node/*: NormalizationSplitOperation*/ = {
"kind": "SplitOperation",
"metadata": {},
"name": "Mutation__foo_mutation$normalization",
"selections": [
{
"kind": "ClientExtension",
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "name",
"storageKey": null
},
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "age",
"storageKey": null
}
]
}
]
};

export default node;

//- __generated__/barMutation.graphql.js
/**
* <auto-generated> SignedSource<<6f79ca8d7718498c480388c5b22a5130>>
* @flow
* @lightSyntaxTransform
* @nogrep
*/

/* eslint-disable */

'use strict';

/*::
import type { ClientRequest, Mutation } from 'relay-runtime';
import {baz_mutation as notCalledMutationBazMutationResolverType} from "../foo.js";
// Type assertion validating that `notCalledMutationBazMutationResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(notCalledMutationBazMutationResolverType: () => ?boolean);
export type barMutation$variables = {||};
export type barMutation$data = {|
+baz_mutation: ?boolean,
|};
export type barMutation = {|
response: barMutation$data,
variables: barMutation$variables,
|};
*/

import {baz_mutation as notCalledMutationBazMutationResolver} from './../foo';

var node/*: ClientRequest*/ = {
"fragment": {
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "barMutation",
"selections": [
{
"kind": "ClientExtension",
"selections": [
{
"alias": null,
"args": null,
"fragment": null,
"kind": "RelayResolver",
"name": "baz_mutation",
"resolverModule": notCalledMutationBazMutationResolver,
"path": "baz_mutation"
}
]
}
],
"type": "NotCalledMutation",
"abstractKey": null
},
"kind": "Request",
"operation": {
"argumentDefinitions": [],
"kind": "Operation",
"name": "barMutation",
"selections": [
{
"kind": "ClientExtension",
"selections": [
{
"name": "baz_mutation",
"args": null,
"fragment": null,
"kind": "RelayResolver",
"storageKey": null,
"isOutputType": true
}
]
}
]
},
"params": {
"cacheID": "b7409af7b3a13247f776ad3f9192c84b",
"id": null,
"metadata": {},
"name": "barMutation",
"operationKind": "mutation",
"text": null
}
};

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

export default ((node/*: any*/)/*: Mutation<
barMutation$variables,
barMutation$data,
>*/);
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//- foo.js
/**
* @RelayResolver Mutation.foo_mutation: Person
*/

/**
* @RelayResolver NotCalledMutation.baz_mutation: Boolean
*/

//- bar.js
graphql`mutation barMutation {
baz_mutation
}`

//- relay.config.json
{
"language": "flow",
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"enable_relay_resolver_transform": true,
"enable_relay_resolver_mutations": true
},
"schemaExtensions": [
"./extensions.graphql"
]
}

//- schema.graphql

type Mutation

type NotCalledMutation

type Query

schema {
query: Query,
mutation: NotCalledMutation,
}

//- extensions.graphql

type Person {
name: String!
age: Int!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
==================================== INPUT ====================================
//- foo.js
/**
* @RelayResolver Mutation.foo_mutation: Boolean
*/

//- bar.js
graphql`mutation barMutation {
foo_mutation
}`

//- relay.config.json
{
"language": "flow",
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"enable_relay_resolver_transform": true
}
}

//- schema.graphql

type Mutation
==================================== OUTPUT ===================================
✖︎ Resolvers on the mutation type Mutation are disallowed without the enable_relay_resolver_mutations feature flag

foo.js:1:3
1 │ *
│ ^
2 │ * @RelayResolver Mutation.foo_mutation: Boolean
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//- foo.js
/**
* @RelayResolver Mutation.foo_mutation: Boolean
*/

//- bar.js
graphql`mutation barMutation {
foo_mutation
}`

//- relay.config.json
{
"language": "flow",
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"enable_relay_resolver_transform": true
}
}

//- schema.graphql

type Mutation
Loading

0 comments on commit 904924c

Please sign in to comment.