Skip to content

Commit

Permalink
retain old parseLiteral, rename new version to parseConstLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Sep 16, 2024
1 parent 1b56a83 commit 4c94747
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 107 deletions.
2 changes: 1 addition & 1 deletion integrationTests/ts/kitchenSink-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ new GraphQLScalarType({
name: 'SomeScalar',
serialize: undefined,
parseValue: undefined,
parseLiteral: undefined,
parseConstLiteral: undefined,
});

new GraphQLError('test', { nodes: undefined });
Expand Down
6 changes: 3 additions & 3 deletions src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const TestFaultyScalar = new GraphQLScalarType({
parseValue() {
throw TestFaultyScalarGraphQLError;
},
parseLiteral() {
parseConstLiteral() {
throw TestFaultyScalarGraphQLError;
},
});
Expand All @@ -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';
},
Expand Down Expand Up @@ -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"})
Expand Down
27 changes: 20 additions & 7 deletions src/type/__tests__/definition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -72,22 +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(parseConstValue('null'))).to.equal(
expect(scalar.parseConstLiteral(parseConstValue('null'))).to.equal(
'parseValue: null',
);
expect(scalar.parseLiteral(parseConstValue('{ foo: "bar" }'))).to.equal(
'parseValue: { foo: "bar" }',
);
expect(
scalar.parseConstLiteral(parseConstValue('{ foo: "bar" }')),
).to.equal('parseValue: { foo: "bar" }');
});

it('rejects a Scalar type defining parseLiteral but not parseValue', () => {
Expand All @@ -101,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', () => {
Expand Down
156 changes: 80 additions & 76 deletions src/type/__tests__/scalars-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,44 @@ describe('Type System: Specified scalar types', () => {
);
});

it('parseLiteral', () => {
function parseLiteral(str: string) {
return GraphQLInt.parseLiteral(parseConstValue(str));
it('parseConstLiteral', () => {
function parseConstLiteral(str: string) {
return GraphQLInt.parseConstLiteral(parseConstValue(str));
}

expect(parseLiteral('1')).to.equal(1);
expect(parseLiteral('0')).to.equal(0);
expect(parseLiteral('-1')).to.equal(-1);
expect(parseConstLiteral('1')).to.equal(1);
expect(parseConstLiteral('0')).to.equal(0);
expect(parseConstLiteral('-1')).to.equal(-1);

expect(() => parseLiteral('9876504321')).to.throw(
expect(() => parseConstLiteral('9876504321')).to.throw(
'Int cannot represent non 32-bit signed integer value: 9876504321',
);
expect(() => parseLiteral('-9876504321')).to.throw(
expect(() => parseConstLiteral('-9876504321')).to.throw(
'Int cannot represent non 32-bit signed integer value: -9876504321',
);

expect(() => parseLiteral('1.0')).to.throw(
expect(() => parseConstLiteral('1.0')).to.throw(
'Int cannot represent non-integer value: 1.0',
);
expect(() => parseLiteral('null')).to.throw(
expect(() => parseConstLiteral('null')).to.throw(
'Int cannot represent non-integer value: null',
);
expect(() => parseLiteral('""')).to.throw(
expect(() => parseConstLiteral('""')).to.throw(
'Int cannot represent non-integer value: ""',
);
expect(() => parseLiteral('"123"')).to.throw(
expect(() => parseConstLiteral('"123"')).to.throw(
'Int cannot represent non-integer value: "123"',
);
expect(() => parseLiteral('false')).to.throw(
expect(() => parseConstLiteral('false')).to.throw(
'Int cannot represent non-integer value: false',
);
expect(() => parseLiteral('[1]')).to.throw(
expect(() => parseConstLiteral('[1]')).to.throw(
'Int cannot represent non-integer value: [1]',
);
expect(() => parseLiteral('{ value: 1 }')).to.throw(
expect(() => parseConstLiteral('{ value: 1 }')).to.throw(
'Int cannot represent non-integer value: { value: 1 }',
);
expect(() => parseLiteral('ENUM_VALUE')).to.throw(
expect(() => parseConstLiteral('ENUM_VALUE')).to.throw(
'Int cannot represent non-integer value: ENUM_VALUE',
);
});
Expand Down Expand Up @@ -226,39 +226,39 @@ describe('Type System: Specified scalar types', () => {
);
});

it('parseLiteral', () => {
function parseLiteral(str: string) {
return GraphQLFloat.parseLiteral(parseConstValue(str));
it('parseConstLiteral', () => {
function parseConstLiteral(str: string) {
return GraphQLFloat.parseConstLiteral(parseConstValue(str));
}

expect(parseLiteral('1')).to.equal(1);
expect(parseLiteral('0')).to.equal(0);
expect(parseLiteral('-1')).to.equal(-1);
expect(parseLiteral('0.1')).to.equal(0.1);
expect(parseLiteral(Math.PI.toString())).to.equal(Math.PI);
expect(parseConstLiteral('1')).to.equal(1);
expect(parseConstLiteral('0')).to.equal(0);
expect(parseConstLiteral('-1')).to.equal(-1);
expect(parseConstLiteral('0.1')).to.equal(0.1);
expect(parseConstLiteral(Math.PI.toString())).to.equal(Math.PI);

expect(() => parseLiteral('null')).to.throw(
expect(() => parseConstLiteral('null')).to.throw(
'Float cannot represent non numeric value: null',
);
expect(() => parseLiteral('""')).to.throw(
expect(() => parseConstLiteral('""')).to.throw(
'Float cannot represent non numeric value: ""',
);
expect(() => parseLiteral('"123"')).to.throw(
expect(() => parseConstLiteral('"123"')).to.throw(
'Float cannot represent non numeric value: "123"',
);
expect(() => parseLiteral('"123.5"')).to.throw(
expect(() => parseConstLiteral('"123.5"')).to.throw(
'Float cannot represent non numeric value: "123.5"',
);
expect(() => parseLiteral('false')).to.throw(
expect(() => parseConstLiteral('false')).to.throw(
'Float cannot represent non numeric value: false',
);
expect(() => parseLiteral('[0.1]')).to.throw(
expect(() => parseConstLiteral('[0.1]')).to.throw(
'Float cannot represent non numeric value: [0.1]',
);
expect(() => parseLiteral('{ value: 0.1 }')).to.throw(
expect(() => parseConstLiteral('{ value: 0.1 }')).to.throw(
'Float cannot represent non numeric value: { value: 0.1 }',
);
expect(() => parseLiteral('ENUM_VALUE')).to.throw(
expect(() => parseConstLiteral('ENUM_VALUE')).to.throw(
'Float cannot represent non numeric value: ENUM_VALUE',
);
});
Expand Down Expand Up @@ -336,33 +336,33 @@ describe('Type System: Specified scalar types', () => {
);
});

it('parseLiteral', () => {
function parseLiteral(str: string) {
return GraphQLString.parseLiteral(parseConstValue(str));
it('parseConstLiteral', () => {
function parseConstLiteral(str: string) {
return GraphQLString.parseConstLiteral(parseConstValue(str));
}

expect(parseLiteral('"foo"')).to.equal('foo');
expect(parseLiteral('"""bar"""')).to.equal('bar');
expect(parseConstLiteral('"foo"')).to.equal('foo');
expect(parseConstLiteral('"""bar"""')).to.equal('bar');

expect(() => parseLiteral('null')).to.throw(
expect(() => parseConstLiteral('null')).to.throw(
'String cannot represent a non string value: null',
);
expect(() => parseLiteral('1')).to.throw(
expect(() => parseConstLiteral('1')).to.throw(
'String cannot represent a non string value: 1',
);
expect(() => parseLiteral('0.1')).to.throw(
expect(() => parseConstLiteral('0.1')).to.throw(
'String cannot represent a non string value: 0.1',
);
expect(() => parseLiteral('false')).to.throw(
expect(() => parseConstLiteral('false')).to.throw(
'String cannot represent a non string value: false',
);
expect(() => parseLiteral('["foo"]')).to.throw(
expect(() => parseConstLiteral('["foo"]')).to.throw(
'String cannot represent a non string value: ["foo"]',
);
expect(() => parseLiteral('{ value: "foo" }')).to.throw(
expect(() => parseConstLiteral('{ value: "foo" }')).to.throw(
'String cannot represent a non string value: { value: "foo" }',
);
expect(() => parseLiteral('ENUM_VALUE')).to.throw(
expect(() => parseConstLiteral('ENUM_VALUE')).to.throw(
'String cannot represent a non string value: ENUM_VALUE',
);
});
Expand Down Expand Up @@ -445,39 +445,39 @@ describe('Type System: Specified scalar types', () => {
);
});

it('parseLiteral', () => {
function parseLiteral(str: string) {
return GraphQLBoolean.parseLiteral(parseConstValue(str));
it('parseConstLiteral', () => {
function parseConstLiteral(str: string) {
return GraphQLBoolean.parseConstLiteral(parseConstValue(str));
}

expect(parseLiteral('true')).to.equal(true);
expect(parseLiteral('false')).to.equal(false);
expect(parseConstLiteral('true')).to.equal(true);
expect(parseConstLiteral('false')).to.equal(false);

expect(() => parseLiteral('null')).to.throw(
expect(() => parseConstLiteral('null')).to.throw(
'Boolean cannot represent a non boolean value: null',
);
expect(() => parseLiteral('0')).to.throw(
expect(() => parseConstLiteral('0')).to.throw(
'Boolean cannot represent a non boolean value: 0',
);
expect(() => parseLiteral('1')).to.throw(
expect(() => parseConstLiteral('1')).to.throw(
'Boolean cannot represent a non boolean value: 1',
);
expect(() => parseLiteral('0.1')).to.throw(
expect(() => parseConstLiteral('0.1')).to.throw(
'Boolean cannot represent a non boolean value: 0.1',
);
expect(() => parseLiteral('""')).to.throw(
expect(() => parseConstLiteral('""')).to.throw(
'Boolean cannot represent a non boolean value: ""',
);
expect(() => parseLiteral('"false"')).to.throw(
expect(() => parseConstLiteral('"false"')).to.throw(
'Boolean cannot represent a non boolean value: "false"',
);
expect(() => parseLiteral('[false]')).to.throw(
expect(() => parseConstLiteral('[false]')).to.throw(
'Boolean cannot represent a non boolean value: [false]',
);
expect(() => parseLiteral('{ value: false }')).to.throw(
expect(() => parseConstLiteral('{ value: false }')).to.throw(
'Boolean cannot represent a non boolean value: { value: false }',
);
expect(() => parseLiteral('ENUM_VALUE')).to.throw(
expect(() => parseConstLiteral('ENUM_VALUE')).to.throw(
'Boolean cannot represent a non boolean value: ENUM_VALUE',
);
});
Expand Down Expand Up @@ -557,39 +557,43 @@ describe('Type System: Specified scalar types', () => {
);
});

it('parseLiteral', () => {
function parseLiteral(str: string) {
return GraphQLID.parseLiteral(parseConstValue(str));
it('parseConstLiteral', () => {
function parseConstLiteral(str: string) {
return GraphQLID.parseConstLiteral(parseConstValue(str));
}

expect(parseLiteral('""')).to.equal('');
expect(parseLiteral('"1"')).to.equal('1');
expect(parseLiteral('"foo"')).to.equal('foo');
expect(parseLiteral('"""foo"""')).to.equal('foo');
expect(parseLiteral('1')).to.equal('1');
expect(parseLiteral('0')).to.equal('0');
expect(parseLiteral('-1')).to.equal('-1');
expect(parseConstLiteral('""')).to.equal('');
expect(parseConstLiteral('"1"')).to.equal('1');
expect(parseConstLiteral('"foo"')).to.equal('foo');
expect(parseConstLiteral('"""foo"""')).to.equal('foo');
expect(parseConstLiteral('1')).to.equal('1');
expect(parseConstLiteral('0')).to.equal('0');
expect(parseConstLiteral('-1')).to.equal('-1');

// Support arbitrary long numbers even if they can't be represented in JS
expect(parseLiteral('90071992547409910')).to.equal('90071992547409910');
expect(parseLiteral('-90071992547409910')).to.equal('-90071992547409910');
expect(parseConstLiteral('90071992547409910')).to.equal(
'90071992547409910',
);
expect(parseConstLiteral('-90071992547409910')).to.equal(
'-90071992547409910',
);

expect(() => parseLiteral('null')).to.throw(
expect(() => parseConstLiteral('null')).to.throw(
'ID cannot represent a non-string and non-integer value: null',
);
expect(() => parseLiteral('0.1')).to.throw(
expect(() => parseConstLiteral('0.1')).to.throw(
'ID cannot represent a non-string and non-integer value: 0.1',
);
expect(() => parseLiteral('false')).to.throw(
expect(() => parseConstLiteral('false')).to.throw(
'ID cannot represent a non-string and non-integer value: false',
);
expect(() => parseLiteral('["1"]')).to.throw(
expect(() => parseConstLiteral('["1"]')).to.throw(
'ID cannot represent a non-string and non-integer value: ["1"]',
);
expect(() => parseLiteral('{ value: "1" }')).to.throw(
expect(() => parseConstLiteral('{ value: "1" }')).to.throw(
'ID cannot represent a non-string and non-integer value: { value: "1" }',
);
expect(() => parseLiteral('ENUM_VALUE')).to.throw(
expect(() => parseConstLiteral('ENUM_VALUE')).to.throw(
'ID cannot represent a non-string and non-integer value: ENUM_VALUE',
);
});
Expand Down
Loading

0 comments on commit 4c94747

Please sign in to comment.