Repository: FormidableLabs/urql
Pull request #579 (2020-03-07 09:25:05)

Fix regression where rootDirs caused dist/types to disappear
## Summary

Apparently `rootDirs` causes type-only files to be skipped while bundling, this removes `shared` since actually only `node.ts` was going to be shared. `help` would soon have to be duplicated

Fixes: https://github.com/FormidableLabs/urql/issues/573

## Set of changes

- remove the shared package


changeset-bot[bot]: ###  💥  No Changeset

Latest commit: 29a4e023bd84004af34ce6c5f9c5b890dfd72cfd

Merging this PR will not cause any packages to be released. If these changes should not cause updates to packages in this repo, this is fine 🙂

**If these changes should be published to npm, you need to add a changeset.**

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/FormidableLabs/urql/new/types?filename=.changeset/tough-books-change.md&value=---%0A%22%40fake-scope%2Ffake-pkg%22%3A%20patch%0A---%0A%0AUpdate%20the%20package%20info%20above%20to%20the%20changes%20you%20want%2C%20and%20write%20your%20description%20here.%20You%20can%20add%20more%20packages%20on%20new%20lines.%0A)



Patch:
diff --git a/.changeset/loud-tomatoes-accept.md b/.changeset/loud-tomatoes-accept.md
new file mode 100644
index 00000000..8b7b669c
--- /dev/null
+++ b/.changeset/loud-tomatoes-accept.md
@@ -0,0 +1,6 @@
+---
+'@urql/exchange-graphcache': patch
+'@urql/exchange-populate': patch
+---
+
+Remove the shared package, this will fix the types file generation for graphcache.
diff --git a/.changeset/sharp-students-flash.md b/.changeset/sharp-students-flash.md
new file mode 100644
index 00000000..c25a19c8
--- /dev/null
+++ b/.changeset/sharp-students-flash.md
@@ -0,0 +1,5 @@
+---
+'@urql/core': patch
+---
+
+Prevent active teardowns for queries on subscriptionExchange
diff --git a/exchanges/graphcache/src/ast/index.ts b/exchanges/graphcache/src/ast/index.ts
index 2111b254..ad9f4243 100644
--- a/exchanges/graphcache/src/ast/index.ts
+++ b/exchanges/graphcache/src/ast/index.ts
@@ -1,3 +1,4 @@
 export { getFieldArguments, normalizeVariables } from './variables';
 export * from './traversal';
 export * from './schemaPredicates';
+export * from './node';
diff --git a/exchanges/shared/src/ast/node.test.ts b/exchanges/graphcache/src/ast/node.test.ts
similarity index 100%
rename from exchanges/shared/src/ast/node.test.ts
rename to exchanges/graphcache/src/ast/node.test.ts
diff --git a/exchanges/shared/src/ast/node.ts b/exchanges/graphcache/src/ast/node.ts
similarity index 100%
rename from exchanges/shared/src/ast/node.ts
rename to exchanges/graphcache/src/ast/node.ts
diff --git a/exchanges/graphcache/src/ast/schemaPredicates.ts b/exchanges/graphcache/src/ast/schemaPredicates.ts
index 580b3d36..bcc3db11 100644
--- a/exchanges/graphcache/src/ast/schemaPredicates.ts
+++ b/exchanges/graphcache/src/ast/schemaPredicates.ts
@@ -9,7 +9,7 @@ import {
   GraphQLUnionType,
 } from 'graphql';
 
-import { warn, invariant } from 'shared';
+import { warn, invariant } from '../helpers/help';
 
 export const isFieldNullable = (
   schema: GraphQLSchema,
diff --git a/exchanges/graphcache/src/ast/traversal.ts b/exchanges/graphcache/src/ast/traversal.ts
index b75a8959..4f58e102 100644
--- a/exchanges/graphcache/src/ast/traversal.ts
+++ b/exchanges/graphcache/src/ast/traversal.ts
@@ -7,8 +7,10 @@ import {
   valueFromASTUntyped,
   Kind,
 } from 'graphql';
-import { invariant, getName } from 'shared';
 
+import { getName } from './node';
+
+import { invariant } from '../helpers/help';
 import { Fragments, Variables } from '../types';
 
 const isFragmentNode = (node: DefinitionNode): node is FragmentDefinitionNode =>
diff --git a/exchanges/graphcache/src/ast/variables.ts b/exchanges/graphcache/src/ast/variables.ts
index 8298e7b9..c05af732 100644
--- a/exchanges/graphcache/src/ast/variables.ts
+++ b/exchanges/graphcache/src/ast/variables.ts
@@ -3,7 +3,8 @@ import {
   OperationDefinitionNode,
   valueFromASTUntyped,
 } from 'graphql';
-import { getName } from 'shared';
+
+import { getName } from './node';
 
 import { makeDict } from '../helpers/dict';
 import { Variables } from '../types';
diff --git a/exchanges/shared/src/helpers/help.ts b/exchanges/graphcache/src/helpers/help.ts
similarity index 100%
rename from exchanges/shared/src/helpers/help.ts
rename to exchanges/graphcache/src/helpers/help.ts
diff --git a/exchanges/graphcache/src/operations/invalidate.ts b/exchanges/graphcache/src/operations/invalidate.ts
index 42df0aaa..83b1acdb 100644
--- a/exchanges/graphcache/src/operations/invalidate.ts
+++ b/exchanges/graphcache/src/operations/invalidate.ts
@@ -1,5 +1,5 @@
 import { FieldNode } from 'graphql';
-import { getSelectionSet, getName, SelectionSet } from 'shared';
+import { getSelectionSet, getName, SelectionSet } from '../ast/node';
 
 import {
   getMainOperation,
diff --git a/exchanges/graphcache/src/operations/query.ts b/exchanges/graphcache/src/operations/query.ts
index 48c7590d..678242db 100644
--- a/exchanges/graphcache/src/operations/query.ts
+++ b/exchanges/graphcache/src/operations/query.ts
@@ -5,9 +5,8 @@ import {
   SelectionSet,
   getFragmentTypeName,
   getFieldAlias,
-  warn,
-  pushDebugNode,
-} from 'shared';
+} from '../ast';
+import { warn, pushDebugNode } from '../helpers/help';
 
 import {
   getFragments,
diff --git a/exchanges/graphcache/src/operations/shared.ts b/exchanges/graphcache/src/operations/shared.ts
index d8030714..db278565 100644
--- a/exchanges/graphcache/src/operations/shared.ts
+++ b/exchanges/graphcache/src/operations/shared.ts
@@ -1,14 +1,13 @@
 import { FieldNode, InlineFragmentNode, FragmentDefinitionNode } from 'graphql';
 import {
-  warn,
-  pushDebugNode,
   isInlineFragment,
   getTypeCondition,
   getSelectionSet,
   getName,
   SelectionSet,
   isFieldNode,
-} from 'shared';
+} from '../ast';
+import { warn, pushDebugNode } from '../helpers/help';
 
 import { hasField } from '../store/data';
 import { Store, keyOfField } from '../store';
diff --git a/exchanges/graphcache/src/operations/write.ts b/exchanges/graphcache/src/operations/write.ts
index 64edc2f3..5d57ebd6 100644
--- a/exchanges/graphcache/src/operations/write.ts
+++ b/exchanges/graphcache/src/operations/write.ts
@@ -6,17 +6,14 @@ import {
   normalizeVariables,
   getFieldArguments,
   isFieldAvailableOnType,
-} from '../ast';
-import {
   getSelectionSet,
   getName,
   SelectionSet,
   getFragmentTypeName,
   getFieldAlias,
-  invariant,
-  warn,
-  pushDebugNode,
-} from 'shared';
+} from '../ast';
+
+import { invariant, warn, pushDebugNode } from '../helpers/help';
 
 import {
   NullArray,
diff --git a/exchanges/graphcache/src/store/data.ts b/exchanges/graphcache/src/store/data.ts
index bf1e418a..402e8078 100644
--- a/exchanges/graphcache/src/store/data.ts
+++ b/exchanges/graphcache/src/store/data.ts
@@ -7,7 +7,7 @@ import {
 } from '../types';
 
 import { makeDict } from '../helpers/dict';
-import { invariant, currentDebugStack } from 'shared';
+import { invariant, currentDebugStack } from '../helpers/help';
 import { fieldInfoOfKey, joinKeys, prefixKey } from './keys';
 import { defer } from './timing';
 
diff --git a/exchanges/graphcache/src/store/store.ts b/exchanges/graphcache/src/store/store.ts
index 8bbb7b36..94866520 100644
--- a/exchanges/graphcache/src/store/store.ts
+++ b/exchanges/graphcache/src/store/store.ts
@@ -20,7 +20,7 @@ import {
   KeyingConfig,
   DataFields,
 } from '../types';
-import { invariant } from 'shared';
+import { invariant } from '../helpers/help';
 
 import { read, readFragment } from '../operations/query';
 import { writeFragment, startWrite } from '../operations/write';
diff --git a/exchanges/graphcache/tsconfig.json b/exchanges/graphcache/tsconfig.json
index 1cc933ea..3553d898 100644
--- a/exchanges/graphcache/tsconfig.json
+++ b/exchanges/graphcache/tsconfig.json
@@ -7,8 +7,7 @@
     "paths": {
       "urql": ["../../node_modules/urql/src"],
       "*-urql": ["../../node_modules/*-urql/src"],
-      "@urql/*": ["../../node_modules/@urql/*/src"],
-      "shared": ["../../node_modules/shared/src"]
+      "@urql/*": ["../../node_modules/@urql/*/src"]
     }
   }
 }
diff --git a/exchanges/populate/src/helpers/help.ts b/exchanges/populate/src/helpers/help.ts
new file mode 100644
index 00000000..ba0d536a
--- /dev/null
+++ b/exchanges/populate/src/helpers/help.ts
@@ -0,0 +1,48 @@
+// These are guards that are used throughout the codebase to warn or error on
+// unexpected behaviour or conditions.
+// Every warning and error comes with a number that uniquely identifies them.
+// You can read more about the messages themselves in `docs/help.md`
+export type ErrorCode =
+  | 1
+  | 2
+  | 3
+  | 4
+  | 5
+  | 6
+  | 7
+  | 8
+  | 9
+  | 10
+  | 11
+  | 12
+  | 13
+  | 15
+  | 16
+  | 17
+  | 18
+  | 19;
+
+const helpUrl =
+  '\nhttps://github.com/FormidableLabs/urql/tree/master/exchanges/graphcache/help.md#';
+const cache = new Set<string>();
+
+export function invariant(
+  condition: any,
+  message: string,
+  code: ErrorCode
+): asserts condition {
+  if (!condition) {
+    const errorMessage = message || 'Minfied Error #' + code + '\n';
+
+    const error = new Error(errorMessage + helpUrl + code);
+    error.name = 'Graphcache Error';
+    throw error;
+  }
+}
+
+export function warn(message: string, code: ErrorCode) {
+  if (!cache.has(message)) {
+    console.warn(message + helpUrl + code);
+    cache.add(message);
+  }
+}
diff --git a/exchanges/populate/src/helpers/node.ts b/exchanges/populate/src/helpers/node.ts
new file mode 100644
index 00000000..348c5d69
--- /dev/null
+++ b/exchanges/populate/src/helpers/node.ts
@@ -0,0 +1,30 @@
+import {
+  NameNode,
+  SelectionNode,
+  SelectionSetNode,
+  GraphQLOutputType,
+  isWrappingType,
+  GraphQLWrappingType,
+} from 'graphql';
+
+export type SelectionSet = ReadonlyArray<SelectionNode>;
+export type GraphQLFlatType = Exclude<GraphQLOutputType, GraphQLWrappingType>;
+
+/** Returns the name of a given node */
+export const getName = (node: { name: NameNode }): string => node.name.value;
+
+/** Returns the SelectionSet for a given inline or defined fragment node */
+export const getSelectionSet = (node: {
+  selectionSet?: SelectionSetNode;
+}): SelectionSet =>
+  node.selectionSet !== undefined ? node.selectionSet.selections : [];
+
+export const unwrapType = (
+  type: null | undefined | GraphQLOutputType
+): GraphQLFlatType | null => {
+  if (isWrappingType(type)) {
+    return unwrapType(type.ofType);
+  }
+
+  return type || null;
+};
diff --git a/exchanges/populate/src/populateExchange.ts b/exchanges/populate/src/populateExchange.ts
index 67f3a293..a4235986 100644
--- a/exchanges/populate/src/populateExchange.ts
+++ b/exchanges/populate/src/populateExchange.ts
@@ -14,7 +14,8 @@ import {
   Kind,
   visit,
 } from 'graphql';
-import { getName, getSelectionSet, unwrapType, invariant, warn } from 'shared';
+import { getName, getSelectionSet, unwrapType } from './helpers/node';
+import { invariant, warn } from './helpers/help';
 
 import { pipe, tap, map } from 'wonka';
 import { Exchange, Operation } from '@urql/core';
diff --git a/exchanges/populate/tsconfig.json b/exchanges/populate/tsconfig.json
index e713330c..639624e0 100644
--- a/exchanges/populate/tsconfig.json
+++ b/exchanges/populate/tsconfig.json
@@ -7,8 +7,7 @@
     "paths": {
       "urql": ["../../node_modules/urql/src"],
       "*-urql": ["../../node_modules/*-urql/src"],
-      "@urql/*": ["../../node_modules/@urql/*/src"],
-      "shared": ["../../node_modules/shared/src"]
+      "@urql/*": ["../../node_modules/@urql/*/src"]
     }
   }
 }
diff --git a/exchanges/shared/package.json b/exchanges/shared/package.json
deleted file mode 100644
index fc3e9f97..00000000
--- a/exchanges/shared/package.json
+++ /dev/null
@@ -1,11 +0,0 @@
-{
-  "private": true,
-  "name": "shared",
-  "version": "1.0.0",
-  "scripts": {
-    "build": "echo 'not building shared'"
-  },
-  "jest": {
-    "preset": "../../scripts/jest/preset"
-  }
-}
diff --git a/exchanges/shared/src/ast/index.ts b/exchanges/shared/src/ast/index.ts
deleted file mode 100644
index d0dddb69..00000000
--- a/exchanges/shared/src/ast/index.ts
+++ /dev/null
@@ -1,1 +0,0 @@
-export * from './node';
diff --git a/exchanges/shared/src/helpers/index.ts b/exchanges/shared/src/helpers/index.ts
deleted file mode 100644
index b6588483..00000000
--- a/exchanges/shared/src/helpers/index.ts
+++ /dev/null
@@ -1,1 +0,0 @@
-export * from './help';
diff --git a/exchanges/shared/src/index.ts b/exchanges/shared/src/index.ts
deleted file mode 100644
index 2cb11bca..00000000
--- a/exchanges/shared/src/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from './ast';
-export * from './helpers';
diff --git a/exchanges/shared/tsconfig.json b/exchanges/shared/tsconfig.json
deleted file mode 100644
index cd80aea8..00000000
--- a/exchanges/shared/tsconfig.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
-  "extends": "../../tsconfig.json",
-  "include": ["src"],
-  "compilerOptions": {
-    "baseUrl": "./",
-    "paths": {
-      "urql": ["../../node_modules/urql/src"],
-      "*-urql": ["../../node_modules/*-urql/src"],
-      "@urql/*": ["../../node_modules/@urql/*/src"]
-    }
-  }
-}
diff --git a/packages/core/src/exchanges/subscription.ts b/packages/core/src/exchanges/subscription.ts
index 983d45d4..0378a0ad 100644
--- a/packages/core/src/exchanges/subscription.ts
+++ b/packages/core/src/exchanges/subscription.ts
@@ -85,10 +85,12 @@ export const subscriptionExchange = ({
           complete: () => {
             if (!isComplete) {
               isComplete = true;
-              client.reexecuteOperation({
-                ...operation,
-                operationName: 'teardown',
-              });
+              if (operation.operationName === 'subscription') {
+                client.reexecuteOperation({
+                  ...operation,
+                  operationName: 'teardown',
+                });
+              }
 
               complete();
             }
diff --git a/scripts/jest/preset.js b/scripts/jest/preset.js
index eafa23ce..e20a2f7c 100644
--- a/scripts/jest/preset.js
+++ b/scripts/jest/preset.js
@@ -10,7 +10,6 @@ module.exports = {
     "^urql$": "<rootDir>/../../node_modules/urql/src",
     "^(.*-urql)$": "<rootDir>/../../node_modules/$1/src",
     "^@urql/(.*)$": "<rootDir>/../../node_modules/@urql/$1/src",
-    "^shared$": "<rootDir>/../../node_modules/shared/src",
   },
   watchPlugins: ['jest-watch-yarn-workspaces'],
   testRegex: '(src/.*(\\.|/)(test|spec))\\.tsx?$',
diff --git a/tsconfig.json b/tsconfig.json
index 9d018e0f..b5b2fabc 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -5,8 +5,7 @@
       "urql": ["packages/react-urql/src"],
       "*-urql": ["packages/*-urql/src"],
       "@urql/exchange-*": ["exchanges/*/src"],
-      "@urql/*": ["packages/*-urql/src", "packages/*/src"],
-      "shared": ["exchanges/shared/src"]
+      "@urql/*": ["packages/*-urql/src", "packages/*/src"]
     },
     "esModuleInterop": true,
     "forceConsistentCasingInFileNames": true,


Test patch:
diff --git a/exchanges/graphcache/src/ast/traversal.test.ts b/exchanges/graphcache/src/ast/traversal.test.ts
index e5e58bba..cb69b7d7 100644
--- a/exchanges/graphcache/src/ast/traversal.test.ts
+++ b/exchanges/graphcache/src/ast/traversal.test.ts
@@ -1,5 +1,5 @@
 import gql from 'graphql-tag';
-import { getSelectionSet } from 'shared';
+import { getSelectionSet } from './node';
 
 import { getMainOperation, shouldInclude } from './traversal';