Skip to content

Commit 7766955

Browse files
committed
Do not export the use GraphQL hook at the top level
1 parent 898fc22 commit 7766955

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import useEventListener from './use-event-listener'
66
import useFavicon from './use-favicon'
77
import useFetch from './use-fetch'
88
import useGet from './use-get'
9-
import useGraphQL, { useQuery } from './use-graphql'
9+
import { useQuery } from './use-query'
1010
import useKeypressed from './use-keypressed'
1111
import useKeypress from './use-keypress'
1212
import useLocalStorage from './use-local-storage'
@@ -33,7 +33,6 @@ export {
3333
useFavicon,
3434
useFetch,
3535
useGet,
36-
useGraphQL,
3736
useKeypressed,
3837
useKeypress,
3938
useLocalStorage,

src/use-graphql.js

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { GraphQLClient } from 'graphql-request'
22
import { useEffect, useReducer } from 'react'
3-
import { useRequest } from './use-request'
4-
import { mergeDeep } from './utils/merge-deep'
3+
54
const reducer = (state, action) => {
65
switch (action.type) {
76
case 'get':
@@ -67,44 +66,3 @@ const useGraphQL = function(url, query, variables = {}, options = {}) {
6766

6867
export { useGraphQL }
6968
export default useGraphQL
70-
71-
const defaultConfig = {
72-
method: 'POST',
73-
mode: 'cors',
74-
cache: 'no-cache',
75-
credentials: 'same-origin',
76-
headers: {
77-
'Content-Type': 'application/json',
78-
},
79-
redirect: 'follow',
80-
referrer: 'no-referrer',
81-
}
82-
83-
export const useQuery = (
84-
url = '/graphql',
85-
query,
86-
config = {
87-
method: 'POST',
88-
}
89-
) => {
90-
const { makeRequest, ...props } = useRequest(url, {
91-
data: null,
92-
error: null,
93-
loading: false,
94-
})
95-
const fullConfig = mergeDeep(defaultConfig, config)
96-
97-
/**
98-
* postData - Post data to url
99-
*
100-
* @param {object} data - the data to post
101-
*/
102-
const makeQuery = async variables => {
103-
await makeRequest({
104-
...fullConfig,
105-
body: JSON.stringify({ query, variables }),
106-
})
107-
}
108-
109-
return { makeQuery, ...props }
110-
}

src/use-query.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useRequest } from './use-request'
2+
import { mergeDeep } from './utils/merge-deep'
3+
const defaultConfig = {
4+
method: 'POST',
5+
mode: 'cors',
6+
cache: 'no-cache',
7+
credentials: 'same-origin',
8+
headers: {
9+
'Content-Type': 'application/json',
10+
},
11+
redirect: 'follow',
12+
referrer: 'no-referrer',
13+
}
14+
15+
export const useQuery = (
16+
url = '/graphql',
17+
query,
18+
config = {
19+
method: 'POST',
20+
}
21+
) => {
22+
const { makeRequest, ...props } = useRequest(url, {
23+
data: null,
24+
error: null,
25+
loading: false,
26+
})
27+
const fullConfig = mergeDeep(defaultConfig, config)
28+
29+
/**
30+
* postData - Post data to url
31+
*
32+
* @param {object} data - the data to post
33+
*/
34+
const makeQuery = async variables => {
35+
await makeRequest({
36+
...fullConfig,
37+
body: JSON.stringify({ query, variables }),
38+
})
39+
}
40+
41+
return { makeQuery, ...props }
42+
}

0 commit comments

Comments
 (0)