mobxjs / mst-gql

Bindings for mobx-state-tree and GraphQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat(config): Prefer gqlWsClient over gqlHttpClient when the client's Websocket is active

theonetheycallneo opened this issue Β· comments

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch mst-gql@0.15.0 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/mst-gql/dist/mst-gql.js b/node_modules/mst-gql/dist/mst-gql.js
index f26f2b2..b3ed540 100644
--- a/node_modules/mst-gql/dist/mst-gql.js
+++ b/node_modules/mst-gql/dist/mst-gql.js
@@ -799,7 +799,7 @@ class Query {
     }
 
     const inCache = this.store.__queryCache.has(this.queryKey);
-
+    console.log("[mst-gql] fetchPolicy",fetchPolicy)
     switch (this.fetchPolicy) {
       case "no-cache":
       case "network-only":
@@ -838,6 +838,7 @@ class Query {
   }
 
   fetchResults() {
+    console.log("[mst-gql.fetchResults]")
     this.loading = true;
     let promise;
 
@@ -926,6 +927,7 @@ const MSTGQLStore = mobxStateTree.types.model("MSTGQLStore", {
   }
 
   function rawRequest(query, variables, options) {
+    console.log("[mst-gql.rawRequest]")
     if (!self.gqlHttpClient && !self.gqlWsClient) throw new Error("Either gqlHttpClient or gqlWsClient (or both) should provided in the MSTGQLStore environment");
 
     if (self.gqlHttpClient) {
diff --git a/node_modules/mst-gql/dist/mst-gql.modern.js b/node_modules/mst-gql/dist/mst-gql.modern.js
index 32c5370..58a9472 100644
--- a/node_modules/mst-gql/dist/mst-gql.modern.js
+++ b/node_modules/mst-gql/dist/mst-gql.modern.js
@@ -955,6 +955,7 @@ const MSTGQLStore = types.model("MSTGQLStore", {
   }
 
   function mutate(mutation, variables, optimisticUpdate) {
+    console.log("[mst-gql] mutate")
     if (optimisticUpdate) {
       const recorder = recordPatches(self);
       optimisticUpdate();
diff --git a/node_modules/mst-gql/dist/mst-gql.module.js b/node_modules/mst-gql/dist/mst-gql.module.js
index f5fa4c3..55c3c70 100644
--- a/node_modules/mst-gql/dist/mst-gql.module.js
+++ b/node_modules/mst-gql/dist/mst-gql.module.js
@@ -928,7 +928,20 @@ const MSTGQLStore = types.model("MSTGQLStore", {
   function rawRequest(query, variables, options) {
     if (!self.gqlHttpClient && !self.gqlWsClient) throw new Error("Either gqlHttpClient or gqlWsClient (or both) should provided in the MSTGQLStore environment");
 
-    if (self.gqlHttpClient) {
+    if (self.gqlWsClient && self.gqlWsClient.isSocketConnected()) {
+      return new Promise((resolve, reject) => {
+        self.gqlWsClient.request({
+          query,
+          variables
+        }).subscribe({
+          next(data) {
+            resolve(data.data);
+          },
+
+          error: reject
+        });
+      });
+    } else if (self.gqlHttpClient) {
       if (options) return self.gqlHttpClient.request({
         document: query,
         variables,
@@ -955,6 +968,7 @@ const MSTGQLStore = types.model("MSTGQLStore", {
   }
 
   function mutate(mutation, variables, optimisticUpdate) {
+    console.log("[mst-gql] mutate module")
     if (optimisticUpdate) {
       const recorder = recordPatches(self);
       optimisticUpdate();
diff --git a/node_modules/mst-gql/dist/mst-gql.umd.js b/node_modules/mst-gql/dist/mst-gql.umd.js
index 55a08e3..6a65f3e 100644
--- a/node_modules/mst-gql/dist/mst-gql.umd.js
+++ b/node_modules/mst-gql/dist/mst-gql.umd.js
@@ -955,6 +955,7 @@
 	  }
 
 	  function mutate(mutation, variables, optimisticUpdate) {
+		console.log("[mst-gql] mutate umd")
 	    if (optimisticUpdate) {
 	      const recorder = mobxStateTree.recordPatches(self);
 	      optimisticUpdate();

This issue body was partially generated by patch-package.

HI @theonetheycallneo - I'm going to help @jesse-savary with these issues and act as a community manager.
If you need anything let me know