Skip to content

Add valueToLiteral() #3812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrationTests/ts/kitchenSink-test.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ new GraphQLScalarType({
name: 'SomeScalar',
serialize: undefined,
parseValue: undefined,
parseLiteral: undefined,
parseConstLiteral: undefined,
});

new GraphQLError('test', { nodes: undefined });
6 changes: 3 additions & 3 deletions src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ const TestFaultyScalar = new GraphQLScalarType({
parseValue() {
throw TestFaultyScalarGraphQLError;
},
parseLiteral() {
parseConstLiteral() {
throw TestFaultyScalarGraphQLError;
},
});
@@ -58,7 +58,7 @@ const TestComplexScalar = new GraphQLScalarType({
expect(value).to.equal('SerializedValue');
return 'DeserializedValue';
},
parseLiteral(ast) {
parseConstLiteral(ast) {
expect(ast).to.include({ kind: 'StringValue', value: 'SerializedValue' });
return 'DeserializedValue';
},
@@ -281,7 +281,7 @@ describe('Execute: Handles inputs', () => {
});
});

it('properly runs parseLiteral on complex scalar types', () => {
it('properly runs parseConstLiteral on complex scalar types', () => {
const result = executeQuery(`
{
fieldWithObjectInput(input: {c: "foo", d: "SerializedValue"})
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -445,6 +445,10 @@ export {
// A helper to use within recursive-descent visitors which need to be aware of the GraphQL type system.
TypeInfo,
visitWithTypeInfo,
// Converts a value to a const value by replacing variables.
replaceVariables,
// Create a GraphQL literal (AST) from a JavaScript input value.
valueToLiteral,
// Coerces a JavaScript value to a GraphQL type, or produces errors.
coerceInputValue,
// Coerces a GraphQL literal (AST) to a GraphQL type, or returns undefined.
30 changes: 20 additions & 10 deletions src/type/__tests__/definition-test.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { identityFunc } from '../../jsutils/identityFunc.js';
import { inspect } from '../../jsutils/inspect.js';

import { Kind } from '../../language/kinds.js';
import { parseValue } from '../../language/parser.js';
import { parseConstValue } from '../../language/parser.js';

import type { GraphQLNullableType, GraphQLType } from '../definition.js';
import {
@@ -55,13 +55,13 @@ describe('Type System: Scalars', () => {
).not.to.throw();
});

it('accepts a Scalar type defining parseValue and parseLiteral', () => {
it('accepts a Scalar type defining parseValue and parseConstLiteral', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
parseValue: dummyFunc,
parseLiteral: dummyFunc,
parseConstLiteral: dummyFunc,
}),
).to.not.throw();
});
@@ -72,25 +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');
expect(scalar.parseConstLiteral).to.be.a('function');
});

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

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

it('rejects a Scalar type defining parseLiteral but not parseValue', () => {
@@ -104,6 +102,18 @@ describe('Type System: Scalars', () => {
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.',
);
});

it('rejects a Scalar type defining parseConstLiteral but not parseValue', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
parseConstLiteral: dummyFunc,
}),
).to.throw(
'SomeScalar must provide both "parseValue" and "parseConstLiteral" functions.',
);
});
});

describe('Type System: Objects', () => {
Loading