Skip to content

Commit

Permalink
enhance(astFromValueUntyped): respect toJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Mar 12, 2024
1 parent 2c21e6d commit fff2399
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-parents-love.md
@@ -0,0 +1,5 @@
---
"@graphql-tools/utils": patch
---

Respect JS `Date` in `astFromValueUntyped`
4 changes: 2 additions & 2 deletions packages/utils/src/astFromValueUntyped.ts
Expand Up @@ -41,8 +41,8 @@ export function astFromValueUntyped(value: any): ValueNode | null {
}

if (typeof value === 'object') {
if (value instanceof Date) {
return { kind: Kind.STRING, value: value.toISOString() };
if (value?.toJSON) {
return astFromValueUntyped(value.toJSON());
}
const fieldNodes: Array<ObjectFieldNode> = [];
for (const fieldName in value) {
Expand Down
19 changes: 19 additions & 0 deletions packages/utils/tests/astFromValueUntyped.test.ts
@@ -0,0 +1,19 @@
import { Kind, valueFromASTUntyped } from 'graphql';
import { astFromValueUntyped } from '../src/astFromValueUntyped.js';

describe('astFromValueUntyped', () => {
it('supports Date', () => {
const date = new Date();
const ast = astFromValueUntyped(date);
expect(ast).toEqual({
kind: Kind.STRING,
value: date.toISOString(),
});
});
it('supports Buffer', () => {
const buffer = Buffer.from('test');
const ast = astFromValueUntyped(buffer);
const generatedValue = valueFromASTUntyped(ast!);
expect(generatedValue).toMatchObject(buffer.toJSON());
});
});

0 comments on commit fff2399

Please sign in to comment.