Skip to content

Commit

Permalink
Remove literalToValue
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed May 15, 2021
1 parent fd85395 commit 56f78d8
Show file tree
Hide file tree
Showing 26 changed files with 168 additions and 556 deletions.
5 changes: 1 addition & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ export {
// Create a GraphQLType from a GraphQL language AST.
typeFromAST,
// Create a JavaScript value from a GraphQL language AST without a Type.
// DEPRECATED: use literalToValue
valueFromASTUntyped,
// Create a GraphQL language AST from a JavaScript value.
astFromValue,
Expand All @@ -418,10 +417,8 @@ export {
visitWithTypeInfo,
// Converts a value to a const value by replacing variables.
replaceVariables,
// Create a GraphQL Literal AST from a JavaScript input value.
// Create a GraphQL literal (AST) from a JavaScript input value.
valueToLiteral,
// Create a JavaScript input value from a GraphQL Literal AST.
literalToValue,
// Coerces a JavaScript value to a GraphQL type, or produces errors.
coerceInputValue,
// Coerces a GraphQL literal (AST) to a GraphQL type, or returns undefined.
Expand Down
5 changes: 1 addition & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ export {
// Create a GraphQLType from a GraphQL language AST.
typeFromAST,
// Create a JavaScript value from a GraphQL language AST without a Type.
// DEPRECATED: use literalToValue
valueFromASTUntyped,
// Create a GraphQL language AST from a JavaScript value.
astFromValue,
Expand All @@ -407,10 +406,8 @@ export {
visitWithTypeInfo,
// Converts a value to a const value by replacing variables.
replaceVariables,
// Create a GraphQL Literal AST from a JavaScript input value.
// Create a GraphQL literal (AST) from a JavaScript input value.
valueToLiteral,
// Create a JavaScript input value from a GraphQL Literal AST.
literalToValue,
// Coerces a JavaScript value to a GraphQL type, or produces errors.
coerceInputValue,
// Coerces a GraphQL literal (AST) to a GraphQL type, or returns undefined.
Expand Down
15 changes: 0 additions & 15 deletions src/jsutils/deprecationWarning.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/jsutils/isSignedInt32.d.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/jsutils/isSignedInt32.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { inspect } from '../../jsutils/inspect';
import { identityFunc } from '../../jsutils/identityFunc';

import { parseConstValue } from '../../language/parser';

import type { GraphQLType, GraphQLNullableType } from '../definition';
import {
GraphQLList,
Expand Down Expand Up @@ -69,6 +72,23 @@ describe('Type System: Scalars', () => {

expect(scalar.serialize).to.equal(identityFunc);
expect(scalar.parseValue).to.equal(identityFunc);
expect(scalar.parseLiteral).to.be.a('function');
});

it('use parseValue for parsing literals if parseLiteral omitted', () => {
const scalar = new GraphQLScalarType({
name: 'Foo',
parseValue(value) {
return 'parseValue: ' + inspect(value);
},
});

expect(scalar.parseLiteral(parseConstValue('null'))).to.equal(
'parseValue: null',
);
expect(scalar.parseLiteral(parseConstValue('{ foo: "bar" }'))).to.equal(
'parseValue: { foo: "bar" }',
);
});

it('rejects a Scalar type without name', () => {
Expand Down Expand Up @@ -116,6 +136,17 @@ describe('Type System: Scalars', () => {
);
});

it('rejects a Scalar type defining valueToLiteral with an incorrect type', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
// $FlowExpectedError[prop-missing]
valueToLiteral: {},
}),
).to.throw('SomeScalar must provide "valueToLiteral" as a function.');
});

it('rejects a Scalar type defining specifiedByURL with an incorrect type', () => {
expect(
() =>
Expand Down
5 changes: 0 additions & 5 deletions src/type/__tests__/scalars-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe('Type System: Specified scalar types', () => {

it('parseLiteral', () => {
function parseLiteral(str: string) {
// $FlowExpectedError[not-a-function]
return GraphQLInt.parseLiteral(parseConstValue(str));
}

Expand Down Expand Up @@ -229,7 +228,6 @@ describe('Type System: Specified scalar types', () => {

it('parseLiteral', () => {
function parseLiteral(str: string) {
// $FlowExpectedError[not-a-function]
return GraphQLFloat.parseLiteral(parseConstValue(str));
}

Expand Down Expand Up @@ -340,7 +338,6 @@ describe('Type System: Specified scalar types', () => {

it('parseLiteral', () => {
function parseLiteral(str: string) {
// $FlowExpectedError[not-a-function]
return GraphQLString.parseLiteral(parseConstValue(str));
}

Expand Down Expand Up @@ -450,7 +447,6 @@ describe('Type System: Specified scalar types', () => {

it('parseLiteral', () => {
function parseLiteral(str: string) {
// $FlowExpectedError[not-a-function]
return GraphQLBoolean.parseLiteral(parseConstValue(str));
}

Expand Down Expand Up @@ -563,7 +559,6 @@ describe('Type System: Specified scalar types', () => {

it('parseLiteral', () => {
function parseLiteral(str: string) {
// $FlowExpectedError[not-a-function]
return GraphQLID.parseLiteral(parseConstValue(str));
}

Expand Down
43 changes: 31 additions & 12 deletions src/type/definition.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,46 @@ export interface GraphQLScalarTypeExtensions {
* const OddType = new GraphQLScalarType({
* name: 'Odd',
* serialize(value) {
* return value % 2 === 1 ? value : null;
* if (value % 2 === 1) {
* return value;
* }
* },
* parseValue(value) {
* if (value % 2 === 1) {
* return value;
* }
* }
* });
*
* Custom scalars behavior is defined via the following functions:
*
* - serialize(value): Implements "Result Coercion". Given an internal value,
* produces an external value valid for this type. Returns undefined or
* throws an error to indicate invalid values.
*
* - parseValue(value): Implements "Input Coercion" for values. Given an
* external value (for example, variable values), produces an internal value
* valid for this type. Returns undefined or throws an error to indicate
* invalid values.
*
* - parseLiteral(ast): Implements "Input Coercion" for literals. Given an
* GraphQL literal (AST) (for example, an argument value), produces an
* internal value valid for this type. Returns undefined or throws an error
* to indicate invalid values.
*
* - valueToLiteral(value): Converts an external value to a GraphQL
* literal (AST). Returns undefined or throws an error to indicate
* invalid values.
*
*/
export class GraphQLScalarType {
name: string;
description: Maybe<string>;
specifiedByURL: Maybe<string>;
serialize: GraphQLScalarSerializer<unknown>;
parseValue: GraphQLScalarValueParser<unknown>;
parseLiteral: Maybe<GraphQLScalarLiteralParser<unknown>>;
parseLiteral: GraphQLScalarLiteralParser<unknown>;
valueToLiteral: Maybe<GraphQLScalarValueToLiteral>;
literalToValue: Maybe<GraphQLScalarLiteralToValue>;
extensions: Maybe<Readonly<GraphQLScalarTypeExtensions>>;
astNode: Maybe<ScalarTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
Expand All @@ -328,9 +354,8 @@ export class GraphQLScalarType {
specifiedByURL: Maybe<string>;
serialize: GraphQLScalarSerializer<unknown>;
parseValue: GraphQLScalarValueParser<unknown>;
parseLiteral: Maybe<GraphQLScalarLiteralParser<unknown>>;
parseLiteral: GraphQLScalarLiteralParser<unknown>;
valueToLiteral: Maybe<GraphQLScalarValueToLiteral>;
literalToValue: Maybe<GraphQLScalarLiteralToValue>;
extensions: Maybe<Readonly<GraphQLScalarTypeExtensions>>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
};
Expand All @@ -352,9 +377,6 @@ export type GraphQLScalarLiteralParser<TInternal> = (
export type GraphQLScalarValueToLiteral = (
inputValue: unknown,
) => Maybe<ConstValueNode>;
export type GraphQLScalarLiteralToValue = (
valueNode: ConstValueNode,
) => unknown;

export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
name: string;
Expand All @@ -366,10 +388,8 @@ export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
parseValue?: GraphQLScalarValueParser<TInternal>;
// Parses an externally provided literal value to use as an input.
parseLiteral?: GraphQLScalarLiteralParser<TInternal>;
// Translates an external input value to a literal (AST).
// Translates an externally provided value to a literal (AST).
valueToLiteral?: Maybe<GraphQLScalarValueToLiteral>;
// Translates a literal (AST) to external input value.
literalToValue?: Maybe<GraphQLScalarLiteralToValue>;
extensions?: Maybe<Readonly<GraphQLScalarTypeExtensions>>;
astNode?: Maybe<ScalarTypeDefinitionNode>;
extensionASTNodes?: Maybe<ReadonlyArray<ScalarTypeExtensionNode>>;
Expand Down Expand Up @@ -801,7 +821,6 @@ export class GraphQLEnumType {
parseValue(value: unknown): Maybe<any>;
parseLiteral(valueNode: ConstValueNode): Maybe<any>;
valueToLiteral(value: unknown): Maybe<ConstValueNode>;
literalToValue(valueNode: ConstValueNode): unknown;

toConfig(): GraphQLEnumTypeConfig & {
extensions: Maybe<Readonly<GraphQLEnumTypeExtensions>>;
Expand Down
Loading

0 comments on commit 56f78d8

Please sign in to comment.