@@ -30,6 +30,8 @@ import {
30
30
import { GraphQLBoolean , GraphQLString } from '../../type/scalars.js' ;
31
31
import { GraphQLSchema } from '../../type/schema.js' ;
32
32
33
+ import { valueFromASTUntyped } from '../../utilities/valueFromASTUntyped.js' ;
34
+
33
35
import { executeSync , experimentalExecuteIncrementally } from '../execute.js' ;
34
36
import { getVariableValues } from '../values.js' ;
35
37
@@ -64,6 +66,16 @@ const TestComplexScalar = new GraphQLScalarType({
64
66
} ,
65
67
} ) ;
66
68
69
+ const TestJSONScalar = new GraphQLScalarType ( {
70
+ name : 'JSONScalar' ,
71
+ coerceInputValue ( value ) {
72
+ return value ;
73
+ } ,
74
+ coerceInputLiteral ( value ) {
75
+ return valueFromASTUntyped ( value ) ;
76
+ } ,
77
+ } ) ;
78
+
67
79
const NestedType : GraphQLObjectType = new GraphQLObjectType ( {
68
80
name : 'NestedType' ,
69
81
fields : {
@@ -151,6 +163,7 @@ const TestType = new GraphQLObjectType({
151
163
fieldWithNestedInputObject : fieldWithInputArg ( {
152
164
type : TestNestedInputObject ,
153
165
} ) ,
166
+ fieldWithJSONScalarInput : fieldWithInputArg ( { type : TestJSONScalar } ) ,
154
167
list : fieldWithInputArg ( { type : new GraphQLList ( GraphQLString ) } ) ,
155
168
nested : {
156
169
type : NestedType ,
@@ -859,6 +872,74 @@ describe('Execute: Handles inputs', () => {
859
872
} ) ;
860
873
} ) ;
861
874
875
+ // Note: the below is non-specified custom graphql-js behavior.
876
+ describe ( 'Handles custom scalars with embedded variables' , ( ) => {
877
+ it ( 'allows custom scalars' , ( ) => {
878
+ const result = executeQuery ( `
879
+ {
880
+ fieldWithJSONScalarInput(input: { a: "foo", b: ["bar"], c: "baz" })
881
+ }
882
+ ` ) ;
883
+
884
+ expectJSON ( result ) . toDeepEqual ( {
885
+ data : {
886
+ fieldWithJSONScalarInput : '{ a: "foo", b: ["bar"], c: "baz" }' ,
887
+ } ,
888
+ } ) ;
889
+ } ) ;
890
+
891
+ it ( 'allows custom scalars with non-embedded variables' , ( ) => {
892
+ const result = executeQuery (
893
+ `
894
+ query ($input: JSONScalar) {
895
+ fieldWithJSONScalarInput(input: $input)
896
+ }
897
+ ` ,
898
+ { input : { a : 'foo' , b : [ 'bar' ] , c : 'baz' } } ,
899
+ ) ;
900
+
901
+ expectJSON ( result ) . toDeepEqual ( {
902
+ data : {
903
+ fieldWithJSONScalarInput : '{ a: "foo", b: ["bar"], c: "baz" }' ,
904
+ } ,
905
+ } ) ;
906
+ } ) ;
907
+
908
+ it ( 'allows custom scalars with embedded operation variables' , ( ) => {
909
+ const result = executeQuery (
910
+ `
911
+ query ($input: String) {
912
+ fieldWithJSONScalarInput(input: { a: $input, b: ["bar"], c: "baz" })
913
+ }
914
+ ` ,
915
+ { input : 'foo' } ,
916
+ ) ;
917
+
918
+ expectJSON ( result ) . toDeepEqual ( {
919
+ data : {
920
+ fieldWithJSONScalarInput : '{ a: "foo", b: ["bar"], c: "baz" }' ,
921
+ } ,
922
+ } ) ;
923
+ } ) ;
924
+
925
+ it ( 'allows custom scalars with embedded fragment variables' , ( ) => {
926
+ const result = executeQueryWithFragmentArguments ( `
927
+ {
928
+ ...JSONFragment(input: "foo")
929
+ }
930
+ fragment JSONFragment($input: String) on TestType {
931
+ fieldWithJSONScalarInput(input: { a: $input, b: ["bar"], c: "baz" })
932
+ }
933
+ ` ) ;
934
+
935
+ expectJSON ( result ) . toDeepEqual ( {
936
+ data : {
937
+ fieldWithJSONScalarInput : '{ a: "foo", b: ["bar"], c: "baz" }' ,
938
+ } ,
939
+ } ) ;
940
+ } ) ;
941
+ } ) ;
942
+
862
943
describe ( 'Handles lists and nullability' , ( ) => {
863
944
it ( 'allows lists to be null' , ( ) => {
864
945
const doc = `
0 commit comments