@@ -468,13 +468,19 @@ export function getNullableType(type) {
468
468
/**
469
469
* These named types do not include modifiers like List or NonNull.
470
470
*/
471
- export type GraphQLNamedType =
471
+ export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType ;
472
+
473
+ export type GraphQLNamedInputType =
474
+ | GraphQLScalarType
475
+ | GraphQLEnumType
476
+ | GraphQLInputObjectType ;
477
+
478
+ export type GraphQLNamedOutputType =
472
479
| GraphQLScalarType
473
480
| GraphQLObjectType
474
481
| GraphQLInterfaceType
475
482
| GraphQLUnionType
476
- | GraphQLEnumType
477
- | GraphQLInputObjectType ;
483
+ | GraphQLEnumType ;
478
484
479
485
export function isNamedType ( type : mixed ) : boolean % checks {
480
486
return (
@@ -496,6 +502,8 @@ export function assertNamedType(type: mixed): GraphQLNamedType {
496
502
497
503
/* eslint-disable no-redeclare */
498
504
declare function getNamedType ( type : void | null ) : void ;
505
+ declare function getNamedType ( type : GraphQLInputType ) : GraphQLNamedInputType ;
506
+ declare function getNamedType ( type : GraphQLOutputType ) : GraphQLNamedOutputType ;
499
507
declare function getNamedType ( type : GraphQLType ) : GraphQLNamedType ;
500
508
export function getNamedType ( type ) {
501
509
/* eslint-enable no-redeclare */
@@ -554,6 +562,7 @@ export class GraphQLScalarType {
554
562
serialize : GraphQLScalarSerializer < mixed > ;
555
563
parseValue : GraphQLScalarValueParser < mixed > ;
556
564
parseLiteral : GraphQLScalarLiteralParser < mixed > ;
565
+ valueToLiteral : ?GraphQLScalarValueToLiteral ;
557
566
extensions : ?ReadOnlyObjMap < mixed > ;
558
567
astNode : ?ScalarTypeDefinitionNode ;
559
568
extensionASTNodes : $ReadOnlyArray < ScalarTypeExtensionNode > ;
@@ -568,6 +577,7 @@ export class GraphQLScalarType {
568
577
this . parseLiteral =
569
578
config . parseLiteral ??
570
579
( ( node , variables ) => parseValue ( valueFromASTUntyped ( node , variables ) ) ) ;
580
+ this . valueToLiteral = config . valueToLiteral ;
571
581
this . extensions = config . extensions && toObjMap ( config . extensions ) ;
572
582
this . astNode = config . astNode ;
573
583
this . extensionASTNodes = config . extensionASTNodes ?? [ ] ;
@@ -603,6 +613,7 @@ export class GraphQLScalarType {
603
613
serialize: this.serialize,
604
614
parseValue: this.parseValue,
605
615
parseLiteral: this.parseLiteral,
616
+ valueToLiteral: this.valueToLiteral,
606
617
extensions: this.extensions,
607
618
astNode: this.astNode,
608
619
extensionASTNodes: this.extensionASTNodes,
@@ -636,6 +647,8 @@ export type GraphQLScalarLiteralParser<TInternal> = (
636
647
variables: ?ObjMap<mixed>,
637
648
) => ?TInternal;
638
649
650
+ export type GraphQLScalarValueToLiteral = (inputValue: mixed) => ?ValueNode;
651
+
639
652
export type GraphQLScalarTypeConfig<TInternal, TExternal> = {|
640
653
name: string,
641
654
description?: ?string,
@@ -646,6 +659,8 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {|
646
659
parseValue?: GraphQLScalarValueParser<TInternal>,
647
660
// Parses an externally provided literal value to use as an input.
648
661
parseLiteral?: GraphQLScalarLiteralParser<TInternal>,
662
+ // Translates an external input value to an external literal (AST).
663
+ valueToLiteral?: ?GraphQLScalarValueToLiteral,
649
664
extensions?: ?ReadOnlyObjMapLike<mixed>,
650
665
astNode?: ?ScalarTypeDefinitionNode,
651
666
extensionASTNodes?: ?$ReadOnlyArray<ScalarTypeExtensionNode>,
@@ -1339,6 +1354,16 @@ export class GraphQLEnumType /* <T> */ {
1339
1354
return enumValue . value ;
1340
1355
}
1341
1356
1357
+ valueToLiteral ( value : mixed ) : ?ValueNode {
1358
+ if ( typeof value === 'string' ) {
1359
+ // https://spec.graphql.org/draft/#Name
1360
+ if ( / ^ [ _ a - z A - Z ] [ _ a - z A - Z 0 - 9 ] * $ / . test ( value ) ) {
1361
+ return { kind : Kind . ENUM , value } ;
1362
+ }
1363
+ return { kind : Kind . STRING , value } ;
1364
+ }
1365
+ }
1366
+
1342
1367
toConfig ( ) : GraphQLEnumTypeNormalizedConfig {
1343
1368
const values = keyValMap (
1344
1369
this . getValues ( ) ,
0 commit comments