Repository: FormidableLabs/urql (ts)

Issue:
Unable to parse some scalar values for fieldKey
```
urql@1.9.6
@urql/exchange-graphcache@2.3.1
```

**Steps to reproduce**

1. Define a field that uses the [`DateTime`](https://github.com/excitement-engineer/graphql-iso-date#datetime) type for an argument:

```graphql
type Query {
  timetable(from: DateTime!, to: DateTime!): [Session!]!
}
```

2. Query that field with the DateTime args:

```graphql
query {
  timetable(from: 2020-04-13T03:00:00.000Z, to: 2020-04-20T02:59:59.999Z}) {
    ...
  }
}
```

3. In a Graphcache Custom Update, try to inspect the `Query` fields:

```js
cache.inspectFields({ __typename: 'Query' })
```

**Expected behavior**

You get the `timetable` field with the used args:

```js
[
  { fieldName: 'timetable', arguments: { from: '2020-04-13T03:00:00.000Z', to: '2020-04-20T02:59:59.999Z' }, fieldKey: 'timetable({"from":"2020-04-13T03:00:00.000Z","to":"2020-04-20T02:59:59.999Z"})' },
]
```

**Actual behavior**

`fieldInfoOfKey` throws:

```
SyntaxError: Unexpected number in JSON at position 12
```

at this line:

https://github.com/FormidableLabs/urql/blob/522736606028fafcb8a313b2b2c3160a6400a852/exchanges/graphcache/src/store/keys.ts#L13

when trying to parse:

```
{"from":2020-04-13T03:00:00.000Z,"to":2020-04-20T02:59:59.999Z}
```

This is because `2020-04-13T03:00:00.000Z` is not a valid JSON value.

**Further comments**

I fixed this by calling `.toString()` on the dates before passing them to `urql` which also works with the DateTime type.

Pull request description:
Fix stringifyVariables not stringifying toJSON values for scalars
Fix #717 

Patch:
diff --git a/.changeset/stale-onions-check.md b/.changeset/stale-onions-check.md
new file mode 100644
index 00000000..733b757a
--- /dev/null
+++ b/.changeset/stale-onions-check.md
@@ -0,0 +1,5 @@
+---
+'@urql/core': patch
+---
+
+Fix stringifyVariables breaking on x.toJSON scalars.
diff --git a/packages/core/src/utils/stringifyVariables.ts b/packages/core/src/utils/stringifyVariables.ts
index 493cb42c..d8ce554e 100644
--- a/packages/core/src/utils/stringifyVariables.ts
+++ b/packages/core/src/utils/stringifyVariables.ts
@@ -7,7 +7,7 @@ const stringify = (x: any): string => {
   } else if (typeof x !== 'object') {
     return JSON.stringify(x) || '';
   } else if (x.toJSON) {
-    return x.toJSON();
+    return stringify(x.toJSON());
   } else if (Array.isArray(x)) {
     let out = '[';
     for (let i = 0, l = x.length; i < l; i++) {


Test patch:
diff --git a/packages/core/src/utils/stringifyVariables.test.ts b/packages/core/src/utils/stringifyVariables.test.ts
index 6e6c6ae4..2d592e4e 100644
--- a/packages/core/src/utils/stringifyVariables.test.ts
+++ b/packages/core/src/utils/stringifyVariables.test.ts
@@ -29,7 +29,7 @@ it('returns null for circular structures', () => {
 
 it('stringifies dates correctly', () => {
   const date = new Date('2019-12-11T04:20:00');
-  expect(stringifyVariables(date)).toBe(date.toJSON());
+  expect(stringifyVariables(date)).toBe(`"${date.toJSON()}"`);
 });
 
 it('stringifies dictionaries (Object.create(null)) correctly', () => {