@@ -468,13 +468,19 @@ export function getNullableType(type) {
468468/**
469469 * These named types do not include modifiers like List or NonNull.
470470 */
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 =
472479 | GraphQLScalarType
473480 | GraphQLObjectType
474481 | GraphQLInterfaceType
475482 | GraphQLUnionType
476- | GraphQLEnumType
477- | GraphQLInputObjectType ;
483+ | GraphQLEnumType ;
478484
479485export function isNamedType ( type : mixed ) : boolean % checks {
480486 return (
@@ -496,6 +502,8 @@ export function assertNamedType(type: mixed): GraphQLNamedType {
496502
497503/* eslint-disable no-redeclare */
498504declare function getNamedType ( type : void | null ) : void ;
505+ declare function getNamedType ( type : GraphQLInputType ) : GraphQLNamedInputType ;
506+ declare function getNamedType ( type : GraphQLOutputType ) : GraphQLNamedOutputType ;
499507declare function getNamedType ( type : GraphQLType ) : GraphQLNamedType ;
500508export function getNamedType ( type ) {
501509 /* eslint-enable no-redeclare */
@@ -554,6 +562,7 @@ export class GraphQLScalarType {
554562 serialize : GraphQLScalarSerializer < mixed > ;
555563 parseValue : GraphQLScalarValueParser < mixed > ;
556564 parseLiteral : GraphQLScalarLiteralParser < mixed > ;
565+ valueToLiteral : ?GraphQLScalarValueToLiteral ;
557566 extensions : ?ReadOnlyObjMap < mixed > ;
558567 astNode : ?ScalarTypeDefinitionNode ;
559568 extensionASTNodes : $ReadOnlyArray < ScalarTypeExtensionNode > ;
@@ -568,6 +577,7 @@ export class GraphQLScalarType {
568577 this . parseLiteral =
569578 config . parseLiteral ??
570579 ( ( node , variables ) => parseValue ( valueFromASTUntyped ( node , variables ) ) ) ;
580+ this . valueToLiteral = config . valueToLiteral ;
571581 this . extensions = config . extensions && toObjMap ( config . extensions ) ;
572582 this . astNode = config . astNode ;
573583 this . extensionASTNodes = config . extensionASTNodes ?? [ ] ;
@@ -603,6 +613,7 @@ export class GraphQLScalarType {
603613 serialize: this.serialize,
604614 parseValue: this.parseValue,
605615 parseLiteral: this.parseLiteral,
616+ valueToLiteral: this.valueToLiteral,
606617 extensions: this.extensions,
607618 astNode: this.astNode,
608619 extensionASTNodes: this.extensionASTNodes,
@@ -636,6 +647,8 @@ export type GraphQLScalarLiteralParser<TInternal> = (
636647 variables: ?ObjMap<mixed>,
637648) => ?TInternal;
638649
650+ export type GraphQLScalarValueToLiteral = (inputValue: mixed) => ?ValueNode;
651+
639652export type GraphQLScalarTypeConfig<TInternal, TExternal> = {|
640653 name: string,
641654 description?: ?string,
@@ -646,6 +659,8 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {|
646659 parseValue?: GraphQLScalarValueParser<TInternal>,
647660 // Parses an externally provided literal value to use as an input.
648661 parseLiteral?: GraphQLScalarLiteralParser<TInternal>,
662+ // Translates an external input value to an external literal (AST).
663+ valueToLiteral?: ?GraphQLScalarValueToLiteral,
649664 extensions?: ?ReadOnlyObjMapLike<mixed>,
650665 astNode?: ?ScalarTypeDefinitionNode,
651666 extensionASTNodes?: ?$ReadOnlyArray<ScalarTypeExtensionNode>,
@@ -1339,6 +1354,16 @@ export class GraphQLEnumType /* <T> */ {
13391354 return enumValue . value ;
13401355 }
13411356
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+
13421367 toConfig ( ) : GraphQLEnumTypeNormalizedConfig {
13431368 const values = keyValMap (
13441369 this . getValues ( ) ,
0 commit comments