3
3
import { expect } from 'chai' ;
4
4
import { describe , it } from 'mocha' ;
5
5
6
+ import invariant from '../../jsutils/invariant' ;
7
+ import identityFunc from '../../jsutils/identityFunc' ;
8
+
6
9
import { parseValue } from '../../language/parser' ;
7
10
import {
8
11
GraphQLInt ,
@@ -12,6 +15,7 @@ import {
12
15
GraphQLID ,
13
16
} from '../../type/scalars' ;
14
17
import {
18
+ GraphQLScalarType ,
15
19
GraphQLEnumType ,
16
20
GraphQLInputObjectType ,
17
21
GraphQLList ,
@@ -53,6 +57,39 @@ describe('valueFromAST', () => {
53
57
expectValueFrom ( '123.456' , GraphQLString ) . to . equal ( undefined ) ;
54
58
} ) ;
55
59
60
+ it ( 'convert using parseLiteral from a custom scalar type' , ( ) => {
61
+ const passthroughScalar = new GraphQLScalarType ( {
62
+ name : 'PassthroughScalar' ,
63
+ parseLiteral ( node ) {
64
+ invariant ( node . kind === 'StringValue' ) ;
65
+ return node . value ;
66
+ } ,
67
+ parseValue : identityFunc ,
68
+ } ) ;
69
+
70
+ expectValueFrom ( '"value"' , passthroughScalar ) . to . equal ( 'value' ) ;
71
+
72
+ const throwScalar = new GraphQLScalarType ( {
73
+ name : 'ThrowScalar' ,
74
+ parseLiteral ( ) {
75
+ throw new Error ( 'Test' ) ;
76
+ } ,
77
+ parseValue : identityFunc ,
78
+ } ) ;
79
+
80
+ expectValueFrom ( 'value' , throwScalar ) . to . equal ( undefined ) ;
81
+
82
+ const returnUndefinedScalar = new GraphQLScalarType ( {
83
+ name : 'ReturnUndefinedScalar' ,
84
+ parseLiteral ( ) {
85
+ return undefined ;
86
+ } ,
87
+ parseValue : identityFunc ,
88
+ } ) ;
89
+
90
+ expectValueFrom ( 'value' , returnUndefinedScalar ) . to . equal ( undefined ) ;
91
+ } ) ;
92
+
56
93
it ( 'converts enum values according to input coercion rules' , ( ) => {
57
94
const testEnum = new GraphQLEnumType ( {
58
95
name : 'TestColor' ,
0 commit comments