Skip to content

Commit

Permalink
(core) - Make DocumentNode serialization idempotent (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenflow authored Apr 3, 2021
1 parent ed45f96 commit 6e6ac18
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-beds-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix inconsistency in generating keys for `DocumentNode`s, especially when using GraphQL Code Generator, which could cause SSR serialization to fail.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -140,9 +140,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -185,7 +186,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -309,9 +310,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -356,7 +358,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -480,9 +482,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -529,7 +532,7 @@ Object {
"key": 3,
"kind": "mutation",
"query": Object {
"__key": 3781860981,
"__key": 4034972436,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -627,9 +630,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 102,
"end": 125,
"source": Object {
"body": "mutation uploadProfilePicture($picture: File) { uploadProfilePicture(picture: $picture) { location } }",
"body": "# uploadProfilePicture
mutation uploadProfilePicture($picture: File) { uploadProfilePicture(picture: $picture) { location } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -678,7 +682,7 @@ Object {
"key": 3,
"kind": "mutation",
"query": Object {
"__key": 1193185401,
"__key": 2033658603,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -780,9 +784,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 108,
"end": 132,
"source": Object {
"body": "mutation uploadProfilePictures($pictures: [File]) { uploadProfilePicture(pictures: $pictures) { location } }",
"body": "# uploadProfilePictures
mutation uploadProfilePictures($pictures: [File]) { uploadProfilePicture(pictures: $pictures) { location } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down
21 changes: 12 additions & 9 deletions packages/core/src/exchanges/__snapshots__/fetch.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -140,9 +140,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -185,7 +186,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -309,9 +310,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -356,7 +358,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -480,9 +482,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
"key": 4,
"kind": "subscription",
"query": Object {
"__key": 2616620656,
"__key": 2088253569,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -114,9 +114,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 74,
"end": 92,
"source": Object {
"body": "subscription subscribeToUser($user: String) { user(user: $user) { name } }",
"body": "# subscribeToUser
subscription subscribeToUser($user: String) { user(user: $user) { name } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down
35 changes: 20 additions & 15 deletions packages/core/src/internal/__snapshots__/fetchSource.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -140,9 +140,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -175,7 +176,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -299,9 +300,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -334,7 +336,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -458,9 +460,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -497,7 +500,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -621,9 +624,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down Expand Up @@ -676,7 +680,7 @@ Object {
"key": 2,
"kind": "query",
"query": Object {
"__key": 3044551916,
"__key": 3521976120,
"definitions": Array [
Object {
"directives": Array [],
Expand Down Expand Up @@ -800,9 +804,10 @@ Object {
],
"kind": "Document",
"loc": Object {
"end": 76,
"end": 86,
"source": Object {
"body": "query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"body": "# getUser
query getUser($name: String) { user(name: $name) { id firstName lastName } }",
"locationOffset": Object {
"column": 1,
"line": 1,
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export const stringifyDocument = (
.trim();

if (typeof node !== 'string') {
if (node.loc) {
const operationName = 'definitions' in node && getOperationName(node);
if (operationName) str = `# ${operationName}\n${str}`;
} else {
const operationName = 'definitions' in node && getOperationName(node);
if (operationName) {
str = `# ${operationName}\n${str}`;
}

if (!node.loc) {
(node as WritableLocation).loc = {
start: 0,
end: str.length,
Expand Down

0 comments on commit 6e6ac18

Please sign in to comment.