@@ -19,7 +19,7 @@ import { PluginProps, createPluginSlice } from '../stores/plugin';
19
19
import { SchemaProps , createSchemaSlice } from '../stores/schema' ;
20
20
import { StorageStore , useStorage } from '../stores/storage' ;
21
21
import { ThemeStore } from '../stores/theme' ;
22
- import { AllSlices } from '../types' ;
22
+ import { SlicesWithActions } from '../types' ;
23
23
import { pick , useSynchronizeValue } from '../utility' ;
24
24
import {
25
25
FragmentDefinitionNode ,
@@ -48,7 +48,7 @@ type GraphiQLProviderProps =
48
48
ComponentPropsWithoutRef < typeof StorageStore > &
49
49
ComponentPropsWithoutRef < typeof ThemeStore > ;
50
50
51
- type GraphiQLStore = UseBoundStore < StoreApi < AllSlices > > ;
51
+ type GraphiQLStore = UseBoundStore < StoreApi < SlicesWithActions > > ;
52
52
53
53
const GraphiQLContext = createContext < RefObject < GraphiQLStore > | null > ( null ) ;
54
54
@@ -164,8 +164,8 @@ const InnerGraphiQLProvider: FC<InnerGraphiQLProviderProps> = ({
164
164
return map ;
165
165
} ) ( ) ;
166
166
167
- const store = create < AllSlices > ( ( ...args ) => ( {
168
- ... createEditorSlice ( {
167
+ const store = create < SlicesWithActions > ( ( ...args ) => {
168
+ const editorSlice = createEditorSlice ( {
169
169
activeTabIndex,
170
170
defaultHeaders,
171
171
defaultQuery,
@@ -181,12 +181,25 @@ const InnerGraphiQLProvider: FC<InnerGraphiQLProviderProps> = ({
181
181
onTabChange,
182
182
shouldPersistHeaders : $shouldPersistHeaders ,
183
183
tabs,
184
- } ) ( ...args ) ,
185
- ...createExecutionSlice ( ...args ) ,
186
- ...createPluginSlice ( ...args ) ,
187
- ...createSchemaSlice ( ...args ) ,
188
- } ) ) ;
189
- store . getState ( ) . storeTabs ( { activeTabIndex, tabs } ) ;
184
+ } ) ( ...args ) ;
185
+ const executionSlice = createExecutionSlice ( ...args ) ;
186
+ const pluginSlice = createPluginSlice ( ...args ) ;
187
+ const schemaSlice = createSchemaSlice ( ...args ) ;
188
+ return {
189
+ ...editorSlice ,
190
+ ...executionSlice ,
191
+ ...pluginSlice ,
192
+ ...schemaSlice ,
193
+ actions : {
194
+ ...editorSlice . actions ,
195
+ ...executionSlice . actions ,
196
+ ...pluginSlice . actions ,
197
+ ...schemaSlice . actions ,
198
+ } ,
199
+ } ;
200
+ } ) ;
201
+ const { actions } = store . getState ( ) ;
202
+ actions . storeTabs ( { activeTabIndex, tabs } ) ;
190
203
return store ;
191
204
}
192
205
@@ -224,7 +237,7 @@ const InnerGraphiQLProvider: FC<InnerGraphiQLProviderProps> = ({
224
237
// storage.set(STORAGE_KEY, '');
225
238
// }
226
239
const store = storeRef . current ;
227
- const { setPlugins, setVisiblePlugin } = store . getState ( ) ;
240
+ const { setPlugins, setVisiblePlugin } = store . getState ( ) . actions ;
228
241
229
242
setPlugins ( plugins ) ;
230
243
setVisiblePlugin ( visiblePlugin ?? null ) ;
@@ -260,7 +273,8 @@ const InnerGraphiQLProvider: FC<InnerGraphiQLProviderProps> = ({
260
273
} ) ) ;
261
274
262
275
// Trigger introspection
263
- void store . getState ( ) . introspect ( ) ;
276
+ const { actions } = store . getState ( ) ;
277
+ void actions . introspect ( ) ;
264
278
} , [
265
279
schema ,
266
280
dangerouslyAssumeSchemaIsValid ,
@@ -277,7 +291,8 @@ const InnerGraphiQLProvider: FC<InnerGraphiQLProviderProps> = ({
277
291
useEffect ( ( ) => {
278
292
function triggerIntrospection ( event : KeyboardEvent ) {
279
293
if ( event . ctrlKey && event . key === 'R' ) {
280
- void storeRef . current . getState ( ) . introspect ( ) ;
294
+ const { actions } = storeRef . current . getState ( ) ;
295
+ void actions . introspect ( ) ;
281
296
}
282
297
}
283
298
@@ -315,10 +330,16 @@ const SynchronizeValue: FC<SynchronizeValueProps> = ({
315
330
return children as ReactElement ;
316
331
} ;
317
332
318
- export function useGraphiQL < T > ( selector : ( state : AllSlices ) => T ) : T {
333
+ export function useGraphiQL < T > ( selector : ( state : SlicesWithActions ) => T ) : T {
319
334
const store = useContext ( GraphiQLContext ) ;
320
335
if ( ! store ) {
321
336
throw new Error ( 'Missing `GraphiQLContext.Provider` in the tree' ) ;
322
337
}
323
338
return useStore ( store . current , useShallow ( selector ) ) ;
324
339
}
340
+
341
+ /**
342
+ * Actions are functions used to update values in your store. They are static and never change.
343
+ * @see https://tkdodo.eu/blog/working-with-zustand#separate-actions-from-state
344
+ */
345
+ export const useGraphiQLActions = ( ) => useGraphiQL ( state => state . actions ) ;
0 commit comments