-
Notifications
You must be signed in to change notification settings - Fork 0
/
apollo-client.code-snippets
185 lines (185 loc) · 6.05 KB
/
apollo-client.code-snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{
"useFetch": {
"scope": "javascript,javascriptreact",
"prefix": ["FetchHook", "useFetch"],
"description": "Create a useFetch hook based on useQuery from the @apollo/client lib.",
"body": [
"import QUERY from './Fetch${1/(.+)$/${1:/capitalize}/}.gql';",
"import {useQuery} from '@apollo/client';",
"import PropTypes from 'prop-types';",
"",
"export const ${1/(.+)$/${1:/upcase}/}_SHAPE = {};",
"",
"function extract({data}) {",
" return data;",
"}",
"",
"export default function $TM_FILENAME_BASE(options) {",
" const {data, loading, error} = useQuery(QUERY, $2options);",
"",
" try {",
" return {",
" data: data && extract({data}),",
" loading,",
" error,",
" };",
" }",
" catch (cause) {",
" return {loading, error: cause};",
" }",
"}",
""
]
},
"useLazyFetch": {
"scope": "javascript,javascriptreact",
"prefix": ["LazyFetchHook", "useLazyFetch"],
"description": "Create a useLazyFetch hook based on useLazyQuery from the @apollo/client lib.",
"body": [
"import QUERY from './Fetch${1/(.+)$/${1:/capitalize}/}.gql';",
"import {useLazyQuery} from '@apollo/client';",
"import PropTypes from 'prop-types';",
"import {useCallback} from 'react';",
"",
"export const ${1/(.+)$/${1:/upcase}/}_SHAPE = {};",
"",
"function extract({data}) {",
" return data;",
"}",
"",
"export default function $TM_FILENAME_BASE(options) {",
" const [query, {data, loading, error}] = useLazyQuery(QUERY, $2options);",
"",
" const fetch = useCallback(async (variables) => {",
" const response = await query({variables});",
" return extract(response);",
" }, [query]);",
"",
" try {",
" return [fetch, {",
" data: data && extract({data}),",
" loading,",
" error,",
" }];",
" }",
" catch (cause) {",
" return [fetch, {loading, error: cause}];",
" }",
"}",
""
]
},
"useMutation": {
"scope": "javascript,javascriptreact",
"prefix": ["MutationHook", "useMutation"],
"description": "Create a useMutation hook based on useMutation from the @apollo/client lib.",
"body": [
"import MUTATION from './${1/(.+)$/${1:/capitalize}/}${2/(.+)$/${1:/capitalize}/}.gql';",
"import {useMutation, useApolloClient} from '@apollo/client';",
"import {useCallback} from 'react';",
"",
"function clearCache(cache, id) {",
" $BLOCK_COMMENT_START",
" * TODO: Ensure what typename you have to remove. Or may be update is better in your case.",
" * cache.evict({id: `__typename:\\${id}`});",
" $BLOCK_COMMENT_END",
"",
" $BLOCK_COMMENT_START",
" * TODO: Usually needed only for remove operation",
" * cache.gc();",
" $BLOCK_COMMENT_END",
"}",
"",
"function extract({data}) {",
" return data;",
"}",
"",
"export default function $TM_FILENAME_BASE(options) {",
" const {cache} = useApolloClient();",
" const [mutation, {data, loading, error}] = useMutation(MUTATION, $3options);",
"",
" const $1 = useCallback(async (variables) => {",
" const response = await mutation({variables});",
" const result = extract(response);",
" clearCache(cache, result.id);",
" return result;",
" }, [cache, mutation]);",
"",
" try {",
" return [$1, {",
" data: data && extract({data}),",
" loading,",
" error,",
" }];",
" }",
" catch (cause) {",
" return [$1, {loading, error: cause}];",
" }",
"}",
""
]
},
"ContextProvider": {
"scope": "javascript,javascriptreact",
"prefix": ["cp", "ContextProvider"],
"description": "Create a ContextProvider component.",
"body": [
"import {${1/(.+)$/${1:/upcase}/}_SHAPE} from './useFetch';",
"import PropTypes from 'prop-types';",
"import {createContext, useContext} from 'react';",
"",
"const ${1/(.+)$/${1:/capitalize}/}Context = createContext();",
"",
"export function use${1/(.+)$/${1:/capitalize}/}Context() {",
" return useContext(${1/(.+)$/${1:/capitalize}/}Context);",
"}",
"",
"export default function $TM_FILENAME_BASE({children, ...props}) {",
" return (",
" <${1/(.+)$/${1:/capitalize}/}Context.Provider value={props}>",
" {children}",
" </${1/(.+)$/${1:/capitalize}/}Context.Provider>",
" );",
"}",
"",
"$TM_FILENAME_BASE.propTypes = {",
" children: PropTypes.node.isRequired,",
" data: $2PropTypes.shape(${1/(.+)$/${1:/upcase}/}_SHAPE),",
" error: PropTypes.instanceOf(Error),",
" loading: PropTypes.bool,",
"};",
"",
"$TM_FILENAME_BASE.defaultProps = {",
" data: undefined,",
" error: undefined,",
" loading: false,",
"};",
""
]
},
"DataProvider": {
"scope": "javascript,javascriptreact",
"prefix": ["dp", "DataProvider"],
"description": "Create a DataProvider component.",
"body": [
"import ContextProvider from './ContextProvider';",
"import useFetch from './useFetch';",
"import PropTypes from 'prop-types';",
"",
"export default function $TM_FILENAME_BASE({children, ...options}) {",
" const result = useFetch($1options);",
"",
" return (",
" <ContextProvider {...result}>",
" {children}",
" </ContextProvider>",
" );",
"}",
"",
"$TM_FILENAME_BASE.propTypes = {",
" children: PropTypes.node.isRequired,",
"};",
""
]
}
}