-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validations for custom scalar arguments
Summary: NOTE: the majority of this diff is test cases, please review carefully to ensure that all relevant cases are captured A future diff will enable this for projects in www. Add a flag `enable_strict_custom_scalars` which will perform more validations when custom scalar types are used. The only validation added here is to check that the user doesn't try to pass literal values (strings, ints, etc) in positions where a custom scalar type is expected, as this can break type safety since Relay can't know whether those literals conform to the underlying custom type. This also fixes some existing issues where you could pass an object or list literal to a place expecting a custom scalar, despite it not being a scalar. Keeping this behind a flag for backwards compatibility, as some users depend on this (e.g. see #3730). Reviewed By: alunyov Differential Revision: D51219598 fbshipit-source-id: 71d9ee85e4428c6b3e958929114ac9e06e8967e3
- Loading branch information
1 parent
adabbb0
commit b683e8c
Showing
46 changed files
with
1,948 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...phql-ir/tests/parse_with_extensions/fixtures/custom_scalar_directive_arg.invalid.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
==================================== INPUT ==================================== | ||
# expected-to-throw | ||
# relay:no_custom_scalar_literals | ||
|
||
query CustomScalarLiteralArgQuery { | ||
extension_field @customScalarDirective(arg: "foo") { | ||
__typename | ||
} | ||
extension_scalar_field @customScalarDirective(arg: "bar") | ||
} | ||
|
||
# %extensions% | ||
|
||
scalar CustomScalarType | ||
|
||
directive @customScalarDirective(arg: CustomScalarType!) on FIELD | ||
|
||
type Obj { | ||
some_key: Int! | ||
} | ||
|
||
extend type Query { | ||
extension_field: Obj | ||
extension_scalar_field: Int | ||
} | ||
==================================== ERROR ==================================== | ||
✖︎ Unexpected scalar literal `"foo"` provided in a position expecting custom scalar type `CustomScalarType`. This value should come from a variable. | ||
|
||
custom_scalar_directive_arg.invalid.graphql:5:49 | ||
4 │ query CustomScalarLiteralArgQuery { | ||
5 │ extension_field @customScalarDirective(arg: "foo") { | ||
│ ^^^^^ | ||
6 │ __typename | ||
|
||
|
||
✖︎ Unexpected scalar literal `"bar"` provided in a position expecting custom scalar type `CustomScalarType`. This value should come from a variable. | ||
|
||
custom_scalar_directive_arg.invalid.graphql:8:56 | ||
7 │ } | ||
8 │ extension_scalar_field @customScalarDirective(arg: "bar") | ||
│ ^^^^^ | ||
9 │ } |
24 changes: 24 additions & 0 deletions
24
...aphql-ir/tests/parse_with_extensions/fixtures/custom_scalar_directive_arg.invalid.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# expected-to-throw | ||
# relay:no_custom_scalar_literals | ||
|
||
query CustomScalarLiteralArgQuery { | ||
extension_field @customScalarDirective(arg: "foo") { | ||
__typename | ||
} | ||
extension_scalar_field @customScalarDirective(arg: "bar") | ||
} | ||
|
||
# %extensions% | ||
|
||
scalar CustomScalarType | ||
|
||
directive @customScalarDirective(arg: CustomScalarType!) on FIELD | ||
|
||
type Obj { | ||
some_key: Int! | ||
} | ||
|
||
extend type Query { | ||
extension_field: Obj | ||
extension_scalar_field: Int | ||
} |
164 changes: 164 additions & 0 deletions
164
...hql-ir/tests/parse_with_extensions/fixtures/custom_scalar_directive_arg_variable.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
==================================== INPUT ==================================== | ||
# relay:no_custom_scalar_literals | ||
|
||
query CustomScalarLiteralArgQuery($var: CustomScalarType!) { | ||
extension_field @customScalarDirective(arg: $var) { | ||
__typename | ||
} | ||
extension_scalar_field @customScalarDirective(arg: $var) | ||
} | ||
|
||
# %extensions% | ||
|
||
scalar CustomScalarType | ||
|
||
directive @customScalarDirective(arg: CustomScalarType!) on FIELD | ||
|
||
type Obj { | ||
some_key: Int! | ||
} | ||
|
||
extend type Query { | ||
extension_field: Obj | ||
extension_scalar_field: Int | ||
} | ||
==================================== OUTPUT =================================== | ||
[ | ||
Operation( | ||
OperationDefinition { | ||
kind: Query, | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:41:68, | ||
item: OperationDefinitionName( | ||
"CustomScalarLiteralArgQuery", | ||
), | ||
}, | ||
type_: Object(0), | ||
variable_definitions: [ | ||
VariableDefinition { | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:69:73, | ||
item: VariableName( | ||
"var", | ||
), | ||
}, | ||
type_: NonNull( | ||
Named( | ||
Scalar(8), | ||
), | ||
), | ||
default_value: None, | ||
directives: [], | ||
}, | ||
], | ||
directives: [], | ||
selections: [ | ||
LinkedField { | ||
alias: None, | ||
definition: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:100:115, | ||
item: FieldID(517), | ||
}, | ||
arguments: [], | ||
directives: [ | ||
Directive { | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:116:138, | ||
item: DirectiveName( | ||
"customScalarDirective", | ||
), | ||
}, | ||
arguments: [ | ||
Argument { | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:139:142, | ||
item: ArgumentName( | ||
"arg", | ||
), | ||
}, | ||
value: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:144:148, | ||
item: Variable( | ||
Variable { | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:144:148, | ||
item: VariableName( | ||
"var", | ||
), | ||
}, | ||
type_: NonNull( | ||
Named( | ||
Scalar(8), | ||
), | ||
), | ||
}, | ||
), | ||
}, | ||
}, | ||
], | ||
data: None, | ||
}, | ||
], | ||
selections: [ | ||
ScalarField { | ||
alias: None, | ||
definition: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:160:170, | ||
item: FieldID(519), | ||
}, | ||
arguments: [], | ||
directives: [], | ||
}, | ||
], | ||
}, | ||
ScalarField { | ||
alias: None, | ||
definition: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:181:203, | ||
item: FieldID(518), | ||
}, | ||
arguments: [], | ||
directives: [ | ||
Directive { | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:204:226, | ||
item: DirectiveName( | ||
"customScalarDirective", | ||
), | ||
}, | ||
arguments: [ | ||
Argument { | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:227:230, | ||
item: ArgumentName( | ||
"arg", | ||
), | ||
}, | ||
value: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:232:236, | ||
item: Variable( | ||
Variable { | ||
name: WithLocation { | ||
location: custom_scalar_directive_arg_variable.graphql:232:236, | ||
item: VariableName( | ||
"var", | ||
), | ||
}, | ||
type_: NonNull( | ||
Named( | ||
Scalar(8), | ||
), | ||
), | ||
}, | ||
), | ||
}, | ||
}, | ||
], | ||
data: None, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
), | ||
] |
23 changes: 23 additions & 0 deletions
23
...phql-ir/tests/parse_with_extensions/fixtures/custom_scalar_directive_arg_variable.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# relay:no_custom_scalar_literals | ||
|
||
query CustomScalarLiteralArgQuery($var: CustomScalarType!) { | ||
extension_field @customScalarDirective(arg: $var) { | ||
__typename | ||
} | ||
extension_scalar_field @customScalarDirective(arg: $var) | ||
} | ||
|
||
# %extensions% | ||
|
||
scalar CustomScalarType | ||
|
||
directive @customScalarDirective(arg: CustomScalarType!) on FIELD | ||
|
||
type Obj { | ||
some_key: Int! | ||
} | ||
|
||
extend type Query { | ||
extension_field: Obj | ||
extension_scalar_field: Int | ||
} |
40 changes: 40 additions & 0 deletions
40
...l-ir/tests/parse_with_extensions/fixtures/custom_scalar_list_literal_arg.invalid.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
==================================== INPUT ==================================== | ||
# expected-to-throw | ||
# relay:no_custom_scalar_literals | ||
|
||
query CustomScalarLiteralArgQuery { | ||
extension_field(custom_scalar_arg: ["1234", "5678"]) { | ||
__typename | ||
} | ||
extension_scalar_field(custom_scalar_arg: []) | ||
} | ||
|
||
# %extensions% | ||
|
||
scalar CustomScalarType | ||
|
||
type Obj { | ||
some_key: Int! | ||
} | ||
|
||
extend type Query { | ||
extension_field(custom_scalar_arg: CustomScalarType!): Obj | ||
extension_scalar_field(custom_scalar_arg: CustomScalarType!): Int | ||
} | ||
==================================== ERROR ==================================== | ||
✖︎ Unexpected list literal provided in a position expecting custom scalar type `CustomScalarType`. | ||
|
||
custom_scalar_list_literal_arg.invalid.graphql:5:40 | ||
4 │ query CustomScalarLiteralArgQuery { | ||
5 │ extension_field(custom_scalar_arg: ["1234", "5678"]) { | ||
│ ^^^^^^^^^^^^^^^^ | ||
6 │ __typename | ||
|
||
|
||
✖︎ Unexpected list literal provided in a position expecting custom scalar type `CustomScalarType`. | ||
|
||
custom_scalar_list_literal_arg.invalid.graphql:8:47 | ||
7 │ } | ||
8 │ extension_scalar_field(custom_scalar_arg: []) | ||
│ ^^ | ||
9 │ } |
22 changes: 22 additions & 0 deletions
22
...ql-ir/tests/parse_with_extensions/fixtures/custom_scalar_list_literal_arg.invalid.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# expected-to-throw | ||
# relay:no_custom_scalar_literals | ||
|
||
query CustomScalarLiteralArgQuery { | ||
extension_field(custom_scalar_arg: ["1234", "5678"]) { | ||
__typename | ||
} | ||
extension_scalar_field(custom_scalar_arg: []) | ||
} | ||
|
||
# %extensions% | ||
|
||
scalar CustomScalarType | ||
|
||
type Obj { | ||
some_key: Int! | ||
} | ||
|
||
extend type Query { | ||
extension_field(custom_scalar_arg: CustomScalarType!): Obj | ||
extension_scalar_field(custom_scalar_arg: CustomScalarType!): Int | ||
} |
Oops, something went wrong.