Skip to content

Commit 29eda11

Browse files
fix(overmind-graphql): fix typing issues
1 parent 785602e commit 29eda11

File tree

1 file changed

+24
-21
lines changed
  • packages/node_modules/overmind-graphql/src

1 file changed

+24
-21
lines changed

packages/node_modules/overmind-graphql/src/index.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ interface PayloadSubscription<P, R> {
2727
disposeWhere(cb: (variables: { [variables: string]: Variable }) => boolean);
2828
}
2929

30+
interface Subscription {
31+
variables: { [key: string]: Variable };
32+
dispose: () => void;
33+
}
34+
3035
type Http = {
3136
url: string;
3237
headers?: () => HttpHeaders;
@@ -67,8 +72,8 @@ export type Graphql<T extends Queries> = {
6772
payload: infer P
6873
) => infer R
6974
? P extends void
70-
? () => R
71-
: (payload: P) => R
75+
? () => Promise<R>
76+
: (payload: P) => Promise<R>
7277
: never;
7378
};
7479
subscriptions: {
@@ -89,10 +94,7 @@ function createError(message: string) {
8994
const _clients: { [url: string]: GraphQLClient } = {};
9095
const _wsClients: { [url: string]: PhoenixSocket } = {};
9196
const _subscriptions: {
92-
[query: string]: Array<{
93-
variables: { [key: string]: Variable };
94-
dispose: () => void;
95-
}>;
97+
[query: string]: Subscription[];
9698
} = {};
9799

98100
export const graphql: <T extends Queries>(
@@ -106,7 +108,7 @@ export const graphql: <T extends Queries>(
106108
const headers = // eslint-disable-next-line
107109
typeof _http.headers === 'function'
108110
? _http.headers()
109-
: _http.options
111+
: _http.options && _http.options.headers
110112
? _http.options.headers
111113
: {};
112114

@@ -130,7 +132,7 @@ export const graphql: <T extends Queries>(
130132
if (!_wsClients[_ws.url]) {
131133
_wsClients[_ws.url] = withAbsintheSocket.create(
132134
new PhoenixSocket(_ws.url, {
133-
params: _ws.params ? _ws.params() : null,
135+
params: _ws.params ? _ws.params() : undefined,
134136
})
135137
);
136138
}
@@ -144,7 +146,7 @@ export const graphql: <T extends Queries>(
144146
const evaluatedQueries = {
145147
queries: Object.keys(queries.queries || {}).reduce((aggr, key) => {
146148
aggr[key] = variables => {
147-
const query = queries.queries[key] as any;
149+
const query = queries.queries![key] as any;
148150
const client = getClient();
149151

150152
if (client) {
@@ -159,7 +161,7 @@ export const graphql: <T extends Queries>(
159161
}, {}),
160162
mutations: Object.keys(queries.mutations || {}).reduce((aggr, key) => {
161163
aggr[key] = variables => {
162-
const query = queries.mutations[key] as any;
164+
const query = queries.mutations![key] as any;
163165
const client = getClient();
164166

165167
if (client) {
@@ -174,7 +176,7 @@ export const graphql: <T extends Queries>(
174176
}, {}),
175177
subscriptions: Object.keys(queries.subscriptions || {}).reduce(
176178
(aggr, key) => {
177-
const query = queries.subscriptions[key] as any;
179+
const query = queries.subscriptions![key] as any;
178180
const queryString = print(query);
179181

180182
if (!_subscriptions[queryString]) {
@@ -216,15 +218,14 @@ export const graphql: <T extends Queries>(
216218
};
217219

218220
subscription.disposeWhere = cb => {
219-
_subscriptions[queryString] = _subscriptions[queryString].reduce(
220-
(subAggr, sub) => {
221-
if (cb(sub.variables)) {
222-
return subAggr;
223-
}
224-
return subAggr.concat(sub);
225-
},
226-
[]
227-
);
221+
_subscriptions[queryString] = _subscriptions[queryString].reduce<
222+
Subscription[]
223+
>((subAggr, sub) => {
224+
if (cb(sub.variables)) {
225+
return subAggr;
226+
}
227+
return subAggr.concat(sub);
228+
}, []);
228229
};
229230

230231
aggr[key] = subscription;
@@ -238,7 +239,9 @@ export const graphql: <T extends Queries>(
238239
return {
239240
initialize(http: Http, ws?: Ws) {
240241
_http = http;
241-
_ws = ws;
242+
if (ws) {
243+
_ws = ws;
244+
}
242245
},
243246
...evaluatedQueries,
244247
} as any;

0 commit comments

Comments
 (0)