Skip to content

Commit 7e8c9b1

Browse files
committed
feat: add support of Mutation
1 parent d60b3d5 commit 7e8c9b1

File tree

5 files changed

+67
-55
lines changed

5 files changed

+67
-55
lines changed

packages/smooth/apollo.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/smooth/src/query/Mutation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react'
2+
import { Mutation as ApolloMutation } from 'react-apollo'
3+
import { useOperationProps } from './utils'
4+
5+
export function Mutation(props) {
6+
const operationProps = useOperationProps(props)
7+
return <ApolloMutation {...operationProps} />
8+
}

packages/smooth/src/query/Query.js

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,8 @@
1-
import React, { useEffect } from 'react'
2-
import qs from 'query-string'
1+
import React from 'react'
32
import { Query as ApolloQuery } from 'react-apollo'
4-
import { usePageContext } from '../page/PageContext'
5-
import { usePause } from '../router/HiddenRouter'
3+
import { useOperationProps } from './utils'
64

7-
function getQueryContext({ location, lang, pageContent }) {
8-
const { id, preview } = qs.parse(location.search)
9-
const headers = {}
10-
11-
if (lang) {
12-
headers['x-smooth-lang'] = lang
13-
}
14-
15-
if (preview && pageContent) {
16-
headers['x-smooth-preview-id'] = id
17-
headers['x-smooth-preview'] = 1
18-
}
19-
20-
return { headers }
21-
}
22-
23-
function PrefetchHandler({ children, ...props }) {
24-
const unpause = usePause()
25-
useEffect(() => {
26-
if (!props.loading) {
27-
unpause()
28-
}
29-
}, [props.loading, unpause])
30-
return children(props)
31-
}
32-
33-
export function Query({
34-
children,
35-
prefetch = true,
36-
pageContent = false,
37-
context,
38-
...props
39-
}) {
40-
const { lang, location } = usePageContext()
41-
42-
return (
43-
<ApolloQuery
44-
context={getQueryContext({ location, lang, pageContent })}
45-
{...props}
46-
>
47-
{apolloProps =>
48-
prefetch ? (
49-
<PrefetchHandler {...apolloProps}>{children}</PrefetchHandler>
50-
) : (
51-
children(apolloProps)
52-
)
53-
}
54-
</ApolloQuery>
55-
)
5+
export function Query({ prefetch = true, ...props }) {
6+
const operationProps = useOperationProps({ prefetch, ...props })
7+
return <ApolloQuery {...operationProps} />
568
}

packages/smooth/src/query/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { Query } from './Query'
2+
export { Mutation } from './Mutation'

packages/smooth/src/query/utils.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { useEffect, useCallback, useRef } from 'react'
2+
import qs from 'query-string'
3+
import { usePageContext } from '../page/PageContext'
4+
import { usePause } from '../router/HiddenRouter'
5+
6+
function PrefetchHandler({ children, ...props }) {
7+
const unpause = usePause()
8+
useEffect(() => {
9+
if (!props.loading) {
10+
unpause()
11+
}
12+
}, [props.loading, unpause])
13+
return children(props)
14+
}
15+
16+
function useOperationContext(props) {
17+
const { lang, location } = usePageContext()
18+
const headers = {}
19+
20+
if (lang) {
21+
headers['x-smooth-lang'] = lang
22+
}
23+
24+
if (props.pageContent) {
25+
const { id, preview } = qs.parse(location.search)
26+
if (preview) {
27+
headers['x-smooth-preview-id'] = id
28+
headers['x-smooth-preview'] = 1
29+
}
30+
}
31+
32+
return { headers }
33+
}
34+
35+
function useRequestHandler(props) {
36+
const propsRef = useRef({})
37+
propsRef.current = props
38+
return useCallback(result => {
39+
if (propsRef.current.prefetch)
40+
return (
41+
<PrefetchHandler {...result}>
42+
{propsRef.current.children}
43+
</PrefetchHandler>
44+
)
45+
return propsRef.current.children(result)
46+
}, [])
47+
}
48+
49+
export function useOperationProps(props) {
50+
const context = useOperationContext(props)
51+
const children = useRequestHandler(props)
52+
return { ...props, context, children }
53+
}

0 commit comments

Comments
 (0)