Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
theonetheycallneo opened this issue Oct 16, 2022 · 1 comment
Assignees
Labels
bug Something isn't working enhancement New feature or request
Milestone

Comments

@theonetheycallneo
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] 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.

@theonetheycallneo theonetheycallneo changed the title feat(config): Would be nice to prefer gqlWsClient over gqlHttpClient when the client's Websocket is active feat(config): Prefer gqlWsClient over gqlHttpClient when the client's Websocket is active Oct 16, 2022
@Benz19
Copy link
Collaborator

Benz19 commented Nov 4, 2022

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

@Benz19 Benz19 added the bug Something isn't working label Nov 4, 2022
@jesse-savary jesse-savary added the enhancement New feature or request label Nov 4, 2022
@jesse-savary jesse-savary added this to the v0.18.0 milestone Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants