Skip to content

Commit f468ce7

Browse files
feat(overmind-graphql): allow raw requests
1 parent c24b47b commit f468ce7

File tree

1 file changed

+66
-3
lines changed
  • packages/node_modules/overmind-graphql/src

1 file changed

+66
-3
lines changed

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

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as withAbsintheSocket from '@absinthe/socket';
22
import { GraphQLClient } from 'graphql-request';
3-
import { RequestInit } from 'graphql-request/dist/types.dom';
3+
import * as Dom from 'graphql-request/dist/types.dom';
44
import { print } from 'graphql/language/printer';
55
import { Socket as PhoenixSocket } from 'phoenix';
66

77
import gqlTag from 'graphql-tag';
8+
import { GraphQLError } from 'graphql';
89

910

1011
export interface Query<Result extends any, Payload extends any = void> {
@@ -32,8 +33,8 @@ interface Subscription {
3233

3334
type Http = {
3435
endpoint: string;
35-
headers?: () => RequestInit['headers'];
36-
options?: RequestInit;
36+
headers?: () => Dom.RequestInit['headers'];
37+
options?: Dom.RequestInit;
3738
};
3839

3940
type Ws =
@@ -44,9 +45,15 @@ type Ws =
4445
| (() => PhoenixSocket | null);
4546

4647
type Queries = {
48+
rawQueries?: {
49+
[key: string]: (payload: any) => any;
50+
};
4751
queries?: {
4852
[key: string]: (payload: any) => any;
4953
};
54+
rawMutations?: {
55+
[key: string]: (payload: any) => any;
56+
};
5057
mutations?: {
5158
[key: string]: (payload: any) => any;
5259
};
@@ -55,9 +62,26 @@ type Queries = {
5562
};
5663
};
5764

65+
type RawResult<T extends any> = Promise<{
66+
data?: T;
67+
extensions?: any;
68+
headers: Dom.Headers;
69+
status: number;
70+
errors?: GraphQLError[];
71+
}>
72+
5873
export type Graphql<T extends Queries> = {
5974
initialize(http: Http, ws?: Ws): void;
6075
} & {
76+
rawQueries: {
77+
[N in keyof T['rawQueries']]: T['rawQueries'][N] extends (
78+
payload: infer P
79+
) => infer R
80+
? P extends void
81+
? () => RawResult<R>
82+
: (payload: P) => RawResult<R>
83+
: never;
84+
}
6185
queries: {
6286
[N in keyof T['queries']]: T['queries'][N] extends (
6387
payload: infer P
@@ -67,6 +91,15 @@ export type Graphql<T extends Queries> = {
6791
: (payload: P) => Promise<R>
6892
: never;
6993
};
94+
rawMutations: {
95+
[N in keyof T['rawMutations']]: T['rawMutations'][N] extends (
96+
payload: infer P
97+
) => infer R
98+
? P extends void
99+
? () => RawResult<R>
100+
: (payload: P) => RawResult<R>
101+
: never;
102+
}
70103
mutations: {
71104
[N in keyof T['mutations']]: T['mutations'][N] extends (
72105
payload: infer P
@@ -151,6 +184,21 @@ export const graphql: <T extends Queries>(
151184
}
152185

153186
const evaluatedQueries = {
187+
rawQueries: Object.keys(queries.rawQueries || {}).reduce((aggr, key) => {
188+
aggr[key] = variables => {
189+
const query = queries.rawQueries![key] as any;
190+
const client = getClient();
191+
192+
if (client) {
193+
return client.rawRequest(print(query), variables);
194+
}
195+
196+
throw createError(
197+
'You are running a query, though there is no HTTP endpoint configured'
198+
);
199+
};
200+
return aggr;
201+
}, {}),
154202
queries: Object.keys(queries.queries || {}).reduce((aggr, key) => {
155203
aggr[key] = variables => {
156204
const query = queries.queries![key] as any;
@@ -166,6 +214,21 @@ export const graphql: <T extends Queries>(
166214
};
167215
return aggr;
168216
}, {}),
217+
rawMutations: Object.keys(queries.rawMutations || {}).reduce((aggr, key) => {
218+
aggr[key] = variables => {
219+
const query = queries.rawMutations![key] as any;
220+
const client = getClient();
221+
222+
if (client) {
223+
return client.rawRequest(print(query), variables);
224+
}
225+
226+
throw createError(
227+
'You are running a query, though there is no HTTP endpoint configured'
228+
);
229+
};
230+
return aggr;
231+
}, {}),
169232
mutations: Object.keys(queries.mutations || {}).reduce((aggr, key) => {
170233
aggr[key] = variables => {
171234
const query = queries.mutations![key] as any;

0 commit comments

Comments
 (0)