Skip to content

Commit 9f788cc

Browse files
committed
node 8, react 16, apollo 2
1 parent 24179d3 commit 9f788cc

File tree

43 files changed

+603
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+603
-266
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.0
4+
5+
* BREAK: Node 8, react 16, apollo 2 (affecting only packages that were still in 0.X: compose-next, ooth-client-react, ooth-client-react-next, ooth-client-react-next-apollo)
6+
37
## 1.0.0
48

59
* BREAK: Target engine from now on is node 8.

examples/integrated/api/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Copy `settings.dist.js` to `settings.js` and adapt.
1212
Run
1313

1414
```
15-
npm start
15+
yarn start
1616
```
1717

1818
The GraphQL endpoint is `/graphql`. Visit `/graphiql` to play with the data.

examples/integrated/api/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
"subscriptions-transport-ws": "^0.9.1"
3535
},
3636
"engines": {
37-
"node": ">=7.5"
37+
"node": ">=8"
3838
}
3939
}

examples/standalone/api/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
"passport-jwt": "^3.0.1"
2626
},
2727
"engines": {
28-
"node": ">=7.5"
28+
"node": ">=8"
2929
}
3030
}

examples/standalone/ooth/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ yarn
1616
### Start
1717

1818
```
19-
npm start
19+
yarn start
2020
```
2121

2222
Open `http://localhost:3000` to see a list of available routes. E.g. try out `http://localhost:3000/status`

examples/standalone/ooth/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"ooth-mongo": "^1.0.1"
2424
},
2525
"engines": {
26-
"node": ">=7.5"
26+
"node": ">=8"
2727
}
2828
}

lerna.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"packages": [
44
"packages/*"
55
],
6+
"npmClient": "yarn",
67
"version": "1.0.1"
78
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"lerna": "2.0.0-beta.36"
44
},
55
"scripts": {
6+
"link": "lerna run link --concurrency=1",
7+
"test": "lerna run test --concurrency=1",
68
"bootstrap": "lerna exec yarn --concurrency=1",
79
"prepublish": "lerna run prepublish --concurrency=1"
810
}

packages/compose-next/package.json

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {
7-
"link": "npm run prepublish && yarn link",
8-
"prepublish": "npm run build",
9-
"build": "babel src --out-dir lib",
10-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"link": "yarn prepublish && yarn link",
8+
"prepublish": "yarn build",
9+
"build": "babel src --out-dir lib --ignore '**/*.test.js'",
10+
"test": "jest"
1111
},
1212
"author": "Nick Redmark <[email protected]> (http://nmr.io)",
1313
"license": "ISC",
1414
"dependencies": {
15-
"react": "^15.4.2"
15+
"create-react-class": "^15.6.2",
16+
"react": "^16.1.1"
1617
},
1718
"devDependencies": {
1819
"babel": "^6.23.0",
19-
"babel-cli": "^6.24.1",
20-
"babel-preset-env": "^1.6.0",
21-
"babel-preset-react": "^6.24.1"
20+
"babel-cli": "^6.26.0",
21+
"babel-preset-env": "^1.6.1",
22+
"babel-preset-react": "^6.24.1",
23+
"jest": "^21.2.1"
2224
}
2325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`composeInitialProps should merge initial props 1`] = `
4+
Object {
5+
"childProps": Object {
6+
"b": 2,
7+
},
8+
"parentProps": Object {
9+
"a": 1,
10+
},
11+
}
12+
`;

packages/compose-next/src/index.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const addInitialProps = (getInitialProps) => {
77
return getInitialProps(ctx)
88
}
99
render() {
10-
return React.createElement(Component, this.props)
10+
return <Component {...this.props}/>
1111
}
1212
}
1313
)
@@ -50,11 +50,15 @@ export const composeInitialProps = (Parent) => (
5050
}
5151
}
5252
render() {
53-
return React.createElement(
54-
Parent,
55-
Object.assign({}, this.props, this.props.parentProps),
56-
React.createElement(Child, Object.assign({}, this.props, this.props.childProps))
57-
)
53+
return <Parent
54+
{...this.props}
55+
{...this.props.parentProps}
56+
>
57+
<Child
58+
{...this.props}
59+
{...this.props.childProps}
60+
/>
61+
</Parent>
5862
}
5963
}
6064
)
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const {Component} = require('react')
2+
const {composeInitialProps} = require('.')
3+
4+
class A extends Component {
5+
static getInitialProps() {
6+
return Promise.resolve({
7+
a: 1,
8+
})
9+
}
10+
}
11+
12+
class B extends Component {
13+
static getInitialProps() {
14+
return Promise.resolve({
15+
b: 2,
16+
})
17+
}
18+
}
19+
20+
describe('composeInitialProps', () => {
21+
test('should merge initial props', async () => {
22+
const Composed = composeInitialProps(A)(B)
23+
24+
expect(await Composed.getInitialProps()).toMatchSnapshot()
25+
})
26+
})

packages/ooth-client-react-next-apollo/package.json

+15-12
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,29 @@
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {
7-
"link": "npm run prepublish && yarn link",
8-
"prepublish": "npm run build",
7+
"link": "yarn prepublish && yarn link",
8+
"prepublish": "yarn build",
99
"build": "babel src --out-dir lib --ignore '**/*.test.js'",
1010
"test": "jest"
1111
},
1212
"author": "Nicola Marcacci Rossi <[email protected]> (http://nmr.io)",
1313
"license": "ISC",
1414
"dependencies": {
15-
"apollo-client": "^1.9.0-0",
16-
"react": "^15.6.1",
17-
"react-apollo": "^1.4.4",
18-
"react-dom": "^15.6.1",
19-
"recompose": "^0.24.0",
20-
"redux": "^3.7.2"
15+
"apollo-cache-inmemory": "^1.1.1",
16+
"apollo-client": "^2.0.3",
17+
"apollo-link": "^1.0.3",
18+
"apollo-link-http": "^1.2.0",
19+
"graphql": "^0.11.7",
20+
"isomorphic-unfetch": "^2.0.0",
21+
"react": "^16.1.1",
22+
"react-apollo": "^2.0.1",
23+
"react-dom": "^16.1.1"
2124
},
2225
"devDependencies": {
23-
"babel-cli": "^6.24.1",
24-
"babel-preset-env": "^1.6.0",
26+
"babel-cli": "^6.26.0",
27+
"babel-preset-env": "^1.6.1",
2528
"babel-preset-react": "^6.24.1",
26-
"jest": "^20.0.4",
27-
"react-test-renderer": "^15.6.1"
29+
"jest": "^21.2.1",
30+
"react-test-renderer": "^16.1.1"
2831
}
2932
}
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,72 @@
1-
import ApolloClient, {createNetworkInterface} from 'apollo-client'
1+
import 'isomorphic-unfetch'
2+
import ApolloClient from 'apollo-client'
3+
import {HttpLink} from 'apollo-link-http'
4+
import {ApolloLink} from 'apollo-link'
5+
import {InMemoryCache} from 'apollo-cache-inmemory'
26
import {ApolloProvider, getDataFromTree} from 'react-apollo'
37
import React from 'react'
4-
import {defaultProps} from 'recompose'
5-
import { createStore as createReduxStore, combineReducers, applyMiddleware } from 'redux'
68

79
let client = null
8-
let store = null
910

10-
const createClient = (uri, opts) => {
11-
const networkInterface = createNetworkInterface({
11+
const getBrowserClient = (uri, cacheOpts, initialData) => {
12+
const link = new HttpLink({
1213
uri,
13-
opts: {
14-
credentials: 'include'
15-
}
14+
credentials: 'include'
15+
})
16+
const cache = new InMemoryCache(cacheOpts)
17+
if (initialData) {
18+
cache.restore(initialData)
19+
}
20+
return new ApolloClient({
21+
link,
22+
cache,
1623
})
17-
return new ApolloClient(Object.assign({}, opts, {
18-
networkInterface
19-
}))
2024
}
2125

22-
const createSSRClient = (uri, cookies, opts) => {
23-
const networkInterface = createNetworkInterface({
26+
const getServerClient = (uri, cookies, cacheOpts) => {
27+
let link = new HttpLink({
2428
uri
2529
})
26-
networkInterface.use([{
27-
applyMiddleware(req, next) {
28-
if (!req.options.headers) {
29-
req.options.headers = {}
30-
}
31-
req.options.headers['Cookie'] = Object.keys(cookies).map(key => `${key}=${cookies[key]}`).join('; ')
32-
next()
33-
}
34-
}])
35-
return new ApolloClient(Object.assign({}, opts, {
30+
if (cookies && Object.keys(cookies).length) {
31+
const middlewareLink = new ApolloLink((operation, forward) => {
32+
operation.setContext({
33+
headers: {
34+
Cookie: Object.keys(cookies).map(key => `${key}=${cookies[key]}`).join('; '),
35+
},
36+
})
37+
return forward(operation)
38+
})
39+
link = middlewareLink.concat(link);
40+
}
41+
42+
const cache = new InMemoryCache(cacheOpts)
43+
44+
return new ApolloClient({
3645
ssrMode: true,
37-
networkInterface
38-
}))
46+
link,
47+
cache,
48+
})
3949
}
4050

41-
const initClient = (uri, cookies, opts) => {
51+
const getClient = (uri, cookies, cacheOpts, initialData) => {
4252
if (!process.browser) {
4353
// on server, create a new client for each request
44-
return createSSRClient(uri, cookies, opts)
54+
return getServerClient(uri, cookies, cacheOpts)
4555
} else {
4656
// on client, create singleton
4757
if (!client) {
48-
client = createClient(uri, opts)
58+
client = getBrowserClient(uri, cacheOpts, initialData)
4959
}
5060
return client
5161
}
5262
}
5363

54-
const createStore = (client, initialState) => {
55-
return createReduxStore(
56-
combineReducers({
57-
apollo: client.reducer()
58-
}),
59-
initialState,
60-
applyMiddleware(client.middleware())
61-
)
62-
}
63-
64-
const initStore = (client, initialState) => {
65-
if (!process.browser) {
66-
// on server, create a new store for each request
67-
return createStore(client, initialState)
68-
} else {
69-
// on client, create singleton
70-
if (!store) {
71-
store = createStore(client, initialState)
72-
}
73-
return store
74-
}
75-
}
76-
7764
module.exports = ({url, opts}) => {
7865
return (Component) => (
7966
class extends React.Component {
8067
static getInitialProps (ctx) {
8168
const cookies = ctx.req && ctx.req.cookies
82-
const client = initClient(url, cookies, opts)
83-
const store = initStore(client, client.initialState)
69+
const client = getClient(url, cookies, opts)
8470

8571
return Promise.resolve(Component.getInitialProps ? Component.getInitialProps(ctx) : {})
8672
.then(props => {
@@ -93,41 +79,30 @@ module.exports = ({url, opts}) => {
9379
asPath: ctx.asPath
9480
}
9581
const app = (
96-
<ApolloProvider client={client} store={store}>
82+
<ApolloProvider client={client}>
9783
<Component url={url} {...props} />
9884
</ApolloProvider>
9985
)
10086
return getDataFromTree(app)
10187
}
10288
})
10389
.then(() => {
104-
const state = store.getState()
105-
106-
return Object.assign({
107-
initialState: Object.assign(
108-
{},
109-
state,
110-
{
111-
apollo: {
112-
data: client.getInitialState().data
113-
}
114-
}
115-
)
116-
}, props)
90+
return {
91+
initialData: client.cache.extract(),
92+
}
11793
})
11894
})
11995
}
12096

12197
constructor (props) {
12298
super(props)
12399
// On server, this client won't be doing any work, because all work has been done in getInitialProps
124-
this.client = initClient(url, {}, opts)
125-
this.store = initStore(this.client, props.initialState)
100+
this.client = getClient(url, {}, opts, props.initialData)
126101
}
127102

128103
render () {
129104
return (
130-
<ApolloProvider client={this.client} store={this.store}>
105+
<ApolloProvider client={this.client}>
131106
<Component {...this.props} />
132107
</ApolloProvider>
133108
)
@@ -136,4 +111,4 @@ module.exports = ({url, opts}) => {
136111
)
137112
}
138113

139-
module.exports.clear = () => client = null
114+
module.exports.clear = () => client = null

0 commit comments

Comments
 (0)