Skip to content

Commit fff2399

Browse files
committed
enhance(astFromValueUntyped): respect toJSON
1 parent 2c21e6d commit fff2399

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

.changeset/few-parents-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-tools/utils": patch
3+
---
4+
5+
Respect JS `Date` in `astFromValueUntyped`

packages/utils/src/astFromValueUntyped.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export function astFromValueUntyped(value: any): ValueNode | null {
4141
}
4242

4343
if (typeof value === 'object') {
44-
if (value instanceof Date) {
45-
return { kind: Kind.STRING, value: value.toISOString() };
44+
if (value?.toJSON) {
45+
return astFromValueUntyped(value.toJSON());
4646
}
4747
const fieldNodes: Array<ObjectFieldNode> = [];
4848
for (const fieldName in value) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Kind, valueFromASTUntyped } from 'graphql';
2+
import { astFromValueUntyped } from '../src/astFromValueUntyped.js';
3+
4+
describe('astFromValueUntyped', () => {
5+
it('supports Date', () => {
6+
const date = new Date();
7+
const ast = astFromValueUntyped(date);
8+
expect(ast).toEqual({
9+
kind: Kind.STRING,
10+
value: date.toISOString(),
11+
});
12+
});
13+
it('supports Buffer', () => {
14+
const buffer = Buffer.from('test');
15+
const ast = astFromValueUntyped(buffer);
16+
const generatedValue = valueFromASTUntyped(ast!);
17+
expect(generatedValue).toMatchObject(buffer.toJSON());
18+
});
19+
});

0 commit comments

Comments
 (0)