Skip to content

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

Description

@theonetheycallneo

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingenhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions