Skip to content

Commit 2752991

Browse files
authored
Merge pull request rmosolgo#4756 from rubendinho/patch-2
Fix ActionCable fetcher
2 parents d43c8a8 + 23130fb commit 2752991

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

javascript_client/src/subscriptions/__tests__/createActionCableFetcherTest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("createActionCableFetcherTest", () => {
2828

2929
var options = {
3030
consumer: (dummyActionCableConsumer as unknown) as Consumer,
31-
url: "/graphql",
31+
url: "/some_graphql_endpoint",
3232
fetch: dummyFetch as typeof fetch,
3333
fetchOptions: {
3434
custom: true,
@@ -58,7 +58,7 @@ describe("createActionCableFetcherTest", () => {
5858
return promise.then(() => {
5959
let res2 = fetcher({ operationName: null, query: "{ __typename } ", variables: {}}, {})
6060
const promise2 = res2.next().then(() => {
61-
expect(fetchLog).toEqual([["/graphql", true]])
61+
expect(fetchLog).toEqual([["/some_graphql_endpoint", true]])
6262
})
6363
return promise2
6464
})

javascript_client/src/subscriptions/createActionCableFetcher.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type { Consumer, Subscription } from "@rails/actioncable"
44

55
type ActionCableFetcherOptions = {
66
consumer: Consumer,
7-
url: String,
7+
url: string,
8+
channelName?: string,
89
fetch?: typeof fetch,
910
fetchOptions?: any,
1011
}
@@ -17,6 +18,8 @@ type SubscriptionIteratorPayload = {
1718
export default function createActionCableFetcher(options: ActionCableFetcherOptions) {
1819
let currentChannel: Subscription | null = null
1920
const consumer = options.consumer
21+
const url = options.url || "/graphql"
22+
const channelName = options.channelName || "GraphqlChannel"
2023

2124
const subscriptionFetcher = async function*(graphqlParams: any, fetcherOpts: any) {
2225
let isSubscription = false;
@@ -32,7 +35,7 @@ export default function createActionCableFetcher(options: ActionCableFetcherOpti
3235

3336
if (isSubscription) {
3437
currentChannel?.unsubscribe()
35-
currentChannel = consumer.subscriptions.create("GraphqlChannel",
38+
currentChannel = consumer.subscriptions.create(channelName,
3639
{
3740
connected: function() {
3841
currentChannel?.perform("execute", {
@@ -75,7 +78,7 @@ export default function createActionCableFetcher(options: ActionCableFetcherOpti
7578
} else {
7679
const fetchFn = options.fetch || window.fetch
7780
// Not a subscription fetcher, post to the given URL
78-
yield fetchFn("/graphql", {
81+
yield fetchFn(url, {
7982
method: "POST",
8083
body: JSON.stringify({
8184
query: graphqlParams.query,

0 commit comments

Comments
 (0)