-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate all React Apollo SSR features into Apollo Client #6499
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8de7a60
Migrate React Apollo SSR capabilities into apollo-client
hwillson ae4a043
Keep React SSR out of the main `@apollo/client` bundle
hwillson 21fc4ad
Update the migration guide to cover SSR
hwillson 0119556
Changelog update
hwillson 65851ef
Import from src/react/ssr directory in tests to verify package.json w…
benjamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from 'react'; | ||
import { DocumentNode } from 'graphql'; | ||
import gql from 'graphql-tag'; | ||
import { mockSingleLink } from '../../../utilities/testing/mocking/mockLink'; | ||
import { ApolloClient } from '../../../ApolloClient'; | ||
import { InMemoryCache } from '../../../cache/inmemory/inMemoryCache'; | ||
import { ApolloProvider } from '../../context/ApolloProvider'; | ||
import { useLazyQuery } from '../../hooks/useLazyQuery'; | ||
import { renderToStringWithData } from '..'; | ||
|
||
describe('useLazyQuery Hook SSR', () => { | ||
const CAR_QUERY: DocumentNode = gql` | ||
query { | ||
cars { | ||
make | ||
model | ||
vin | ||
} | ||
} | ||
`; | ||
|
||
const CAR_RESULT_DATA = { | ||
cars: [ | ||
{ | ||
make: 'Audi', | ||
model: 'RS8', | ||
vin: 'DOLLADOLLABILL', | ||
__typename: 'Car' | ||
} | ||
] | ||
}; | ||
|
||
it('should run query only after calling the lazy mode execute function', () => { | ||
const link = mockSingleLink({ | ||
request: { query: CAR_QUERY }, | ||
result: { data: CAR_RESULT_DATA } | ||
}); | ||
|
||
const client = new ApolloClient({ | ||
cache: new InMemoryCache(), | ||
link, | ||
ssrMode: true | ||
}); | ||
|
||
const Component = () => { | ||
let html = null; | ||
const [execute, { loading, called, data }] = useLazyQuery(CAR_QUERY); | ||
|
||
if (!loading && !called) { | ||
execute(); | ||
} | ||
|
||
if (!loading && called) { | ||
expect(loading).toEqual(false); | ||
expect(data).toEqual(CAR_RESULT_DATA); | ||
html = <p>{data.cars[0].make}</p>; | ||
} | ||
|
||
return html; | ||
}; | ||
|
||
const app = ( | ||
<ApolloProvider client={client}> | ||
<Component /> | ||
</ApolloProvider> | ||
); | ||
|
||
return renderToStringWithData(app).then(markup => { | ||
expect(markup).toMatch(/Audi/); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import React from 'react'; | ||
import { DocumentNode } from 'graphql'; | ||
import gql from 'graphql-tag'; | ||
import { MockedProvider } from '../../../utilities/testing/mocking/MockedProvider'; | ||
import { mockSingleLink } from '../../../utilities/testing/mocking/mockLink'; | ||
import { ApolloClient } from '../../../ApolloClient'; | ||
import { InMemoryCache } from '../../../cache/inmemory/inMemoryCache'; | ||
import { ApolloProvider } from '../../context/ApolloProvider'; | ||
import { useQuery } from '../../hooks/useQuery'; | ||
import { render, wait } from '@testing-library/react'; | ||
import { renderToStringWithData } from '..'; | ||
|
||
describe('useQuery Hook SSR', () => { | ||
const CAR_QUERY: DocumentNode = gql` | ||
query { | ||
cars { | ||
make | ||
model | ||
vin | ||
} | ||
} | ||
`; | ||
|
||
const CAR_RESULT_DATA = { | ||
cars: [ | ||
{ | ||
make: 'Audi', | ||
model: 'RS8', | ||
vin: 'DOLLADOLLABILL', | ||
__typename: 'Car' | ||
} | ||
] | ||
}; | ||
|
||
const CAR_MOCKS = [ | ||
{ | ||
request: { | ||
query: CAR_QUERY | ||
}, | ||
result: { data: CAR_RESULT_DATA } | ||
} | ||
]; | ||
|
||
it('should support SSR', () => { | ||
const Component = () => { | ||
const { loading, data } = useQuery(CAR_QUERY); | ||
if (!loading) { | ||
expect(data).toEqual(CAR_RESULT_DATA); | ||
const { make, model, vin } = data.cars[0]; | ||
return ( | ||
<div> | ||
{make}, {model}, {vin} | ||
</div> | ||
); | ||
} | ||
return null; | ||
}; | ||
|
||
const app = ( | ||
<MockedProvider mocks={CAR_MOCKS}> | ||
<Component /> | ||
</MockedProvider> | ||
); | ||
|
||
return renderToStringWithData(app).then(markup => { | ||
expect(markup).toMatch(/Audi/); | ||
}); | ||
}); | ||
|
||
it('should initialize data as `undefined` when loading', () => { | ||
const Component = () => { | ||
const { data, loading } = useQuery(CAR_QUERY); | ||
if (loading) { | ||
expect(data).toBeUndefined(); | ||
} | ||
return null; | ||
}; | ||
|
||
const app = ( | ||
<MockedProvider mocks={CAR_MOCKS}> | ||
<Component /> | ||
</MockedProvider> | ||
); | ||
|
||
return renderToStringWithData(app); | ||
}); | ||
|
||
it('should skip SSR tree rendering if `ssr` option is `false`', async () => { | ||
let renderCount = 0; | ||
const Component = () => { | ||
const { data, loading } = useQuery(CAR_QUERY, { ssr: false }); | ||
renderCount += 1; | ||
|
||
if (!loading) { | ||
const { make } = data.cars[0]; | ||
return <div>{make}</div>; | ||
} | ||
return null; | ||
}; | ||
|
||
const app = ( | ||
<MockedProvider mocks={CAR_MOCKS}> | ||
<Component /> | ||
</MockedProvider> | ||
); | ||
|
||
return renderToStringWithData(app).then(result => { | ||
expect(renderCount).toBe(1); | ||
expect(result).toEqual(''); | ||
}); | ||
}); | ||
|
||
it( | ||
'should skip both SSR tree rendering and SSR component rendering if ' + | ||
'`ssr` option is `false` and `ssrMode` is `true`', | ||
async () => { | ||
const link = mockSingleLink({ | ||
request: { query: CAR_QUERY }, | ||
result: { data: CAR_RESULT_DATA } | ||
}); | ||
|
||
const client = new ApolloClient({ | ||
cache: new InMemoryCache(), | ||
link, | ||
ssrMode: true | ||
}); | ||
|
||
let renderCount = 0; | ||
const Component = () => { | ||
const { data, loading } = useQuery(CAR_QUERY, { ssr: false }); | ||
|
||
let content = null; | ||
switch (renderCount) { | ||
case 0: | ||
expect(loading).toBeTruthy(); | ||
expect(data).toBeUndefined(); | ||
break; | ||
case 1: // FAIL; should not render a second time | ||
default: | ||
} | ||
|
||
renderCount += 1; | ||
return content; | ||
}; | ||
|
||
const app = ( | ||
<ApolloProvider client={client}> | ||
<Component /> | ||
</ApolloProvider> | ||
); | ||
|
||
await renderToStringWithData(app).then(result => { | ||
expect(renderCount).toBe(1); | ||
expect(result).toEqual(''); | ||
}); | ||
|
||
renderCount = 0; | ||
|
||
render( | ||
<ApolloProvider client={client}> | ||
<Component /> | ||
</ApolloProvider> | ||
); | ||
|
||
await wait(() => { | ||
expect(renderCount).toBe(1); | ||
}); | ||
} | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that
@apollo/client/react/ssr
is currently the only subdirectory entry point within@apollo/client/react
, I would love to see@apollo/client/react/components
and@apollo/client/react/hoc
added as well!