Skip to content

Commit b476eac

Browse files
committed
Revert and deprecate and introduce
1 parent bc48f9d commit b476eac

17 files changed

+671
-160
lines changed

src/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,14 @@ export {
409409
// Create a JavaScript value from a GraphQL language AST without a Type.
410410
valueFromASTUntyped,
411411
// Create a GraphQL language AST from a JavaScript value.
412+
// DEPRECATED: use valueToLiteral
412413
astFromValue,
413414
// A helper to use within recursive-descent visitors which need to be aware of
414415
// the GraphQL type system.
415416
TypeInfo,
416417
visitWithTypeInfo,
418+
// Create a GraphQL Literal AST from a JavaScript input value.
419+
valueToLiteral,
417420
// Coerces a GraphQL Literal with a GraphQL type.
418421
coerceInputLiteral,
419422
// Coerces a JavaScript value with a GraphQL type, or produces errors.

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,14 @@ export {
398398
// Create a JavaScript value from a GraphQL language AST without a Type.
399399
valueFromASTUntyped,
400400
// Create a GraphQL language AST from a JavaScript value.
401+
// DEPRECATED: use valueToLiteral
401402
astFromValue,
402403
// A helper to use within recursive-descent visitors which need to be aware of
403404
// the GraphQL type system.
404405
TypeInfo,
405406
visitWithTypeInfo,
407+
// Create a GraphQL Literal AST from a JavaScript input value.
408+
valueToLiteral,
406409
// Coerces a GraphQL Literal with a GraphQL type.
407410
coerceInputLiteral,
408411
// Coerces a JavaScript value with a GraphQL type, or produces errors.

src/type/definition.d.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,27 @@ export function getNullableType<T extends GraphQLNullableType>(
249249
/**
250250
* These named types do not include modifiers like List or NonNull.
251251
*/
252-
export type GraphQLNamedType =
252+
export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType;
253+
254+
export type GraphQLNamedInputType =
255+
| GraphQLScalarType
256+
| GraphQLEnumType
257+
| GraphQLInputObjectType;
258+
259+
export type GraphQLNamedOutputType =
253260
| GraphQLScalarType
254261
| GraphQLObjectType
255262
| GraphQLInterfaceType
256263
| GraphQLUnionType
257-
| GraphQLEnumType
258-
| GraphQLInputObjectType;
264+
| GraphQLEnumType;
259265

260266
export function isNamedType(type: unknown): type is GraphQLNamedType;
261267

262268
export function assertNamedType(type: unknown): GraphQLNamedType;
263269

264270
export function getNamedType(type: undefined): undefined;
271+
export function getNamedType(type: GraphQLInputType): GraphQLNamedInputType;
272+
export function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType;
265273
export function getNamedType(type: GraphQLType): GraphQLNamedType;
266274

267275
/**

src/type/definition.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

479485
export function isNamedType(type: mixed): boolean %checks {
480486
return (
@@ -496,6 +502,8 @@ export function assertNamedType(type: mixed): GraphQLNamedType {
496502

497503
/* eslint-disable no-redeclare */
498504
declare function getNamedType(type: void | null): void;
505+
declare function getNamedType(type: GraphQLInputType): GraphQLNamedInputType;
506+
declare function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType;
499507
declare function getNamedType(type: GraphQLType): GraphQLNamedType;
500508
export 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+
639652
export 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-zA-Z][_a-zA-Z0-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(),

src/type/introspection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { invariant } from '../jsutils/invariant';
33

44
import { print } from '../language/printer';
55
import { DirectiveLocation } from '../language/directiveLocation';
6-
import { astFromValue } from '../utilities/astFromValue';
6+
import { valueToLiteral } from '../utilities/valueToLiteral';
77

88
import type { GraphQLSchema } from './schema';
99
import type { GraphQLDirective } from './directives';
@@ -386,7 +386,7 @@ export const __InputValue: GraphQLObjectType = new GraphQLObjectType({
386386
const { type, defaultValue } = inputValue;
387387
const valueAST =
388388
defaultValue !== undefined
389-
? astFromValue(defaultValue, type)
389+
? valueToLiteral(defaultValue, type)
390390
: undefined;
391391
return valueAST ? print(valueAST) : null;
392392
},

src/type/scalars.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { inspect } from '../jsutils/inspect';
22
import { isObjectLike } from '../jsutils/isObjectLike';
33

4+
import type { ValueNode } from '../language/ast';
45
import { Kind } from '../language/kinds';
56
import { print } from '../language/printer';
67

@@ -268,6 +269,15 @@ export const GraphQLID: GraphQLScalarType = new GraphQLScalarType({
268269
}
269270
return valueNode.value;
270271
},
272+
valueToLiteral(value: mixed): ?ValueNode {
273+
// ID types can use Int literals.
274+
if (typeof value === 'string') {
275+
if (/^-?(?:0|[1-9][0-9]*)$/.test(value)) {
276+
return { kind: Kind.INT, value };
277+
}
278+
return { kind: Kind.STRING, value };
279+
}
280+
},
271281
});
272282

273283
export const specifiedScalarTypes: $ReadOnlyArray<GraphQLScalarType> = Object.freeze(

0 commit comments

Comments
 (0)