Skip to content

Commit

Permalink
Merge branch 'canary' into fix/incoming-message-cookies-type
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfwood authored Dec 3, 2020
2 parents edcc50a + 2094961 commit b436967
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 108 deletions.
8 changes: 4 additions & 4 deletions docs/basic-features/data-fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ The `context` parameter is an object containing the following keys:
> This includes reading from the filesystem or a database.
> **Note**: You should not use [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to
> call an API route in your application.
> Instead, directly import the API route and call its function yourself.
> call an API route in `getStaticProps`.
> Instead, directly import the logic used inside your API route.
> You may need to slightly refactor your code for this approach.
>
> Fetching from an external API is fine!
Expand Down Expand Up @@ -661,8 +661,8 @@ The `context` parameter is an object containing the following keys:
> This includes reading from the filesystem or a database.
> **Note**: You should not use [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to
> call an API route in your application.
> Instead, directly import the API route and call its function yourself.
> call an API route in `getServerSideProps`.
> Instead, directly import the logic used inside your API route.
> You may need to slightly refactor your code for this approach.
>
> Fetching from an external API is fine!
Expand Down
2 changes: 1 addition & 1 deletion docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ By using the DPS workflow, in addition to doing _code reviews_, you can do _depl
For example, the [hybrid pages](/docs/basic-features/pages.md) approach is fully supported out of the box.

- Every page can either use [Static Generation](/docs/basic-features/pages.md#static-generation) or [Server-Side Rendering](/docs/basic-features/pages.md#server-side-rendering).
- Pages that use [Static Generation](/docs/basic-features/pages.md#static-generation) and assets (JS, CSS, images, fonts, etc) will automatically be served from the [Vercel's Edge Network](https://vercel.com/docs/v2/edge-network/overview), which is blazingly fast.
- Pages that use [Static Generation](/docs/basic-features/pages.md#static-generation) and assets (JS, CSS, images, fonts, etc) will automatically be served from [Vercel's Edge Network](https://vercel.com/docs/v2/edge-network/overview), which is blazingly fast.
- Pages that use [Server-Side Rendering](/docs/basic-features/pages.md#server-side-rendering) and [API routes](/docs/api-routes/introduction.md) will automatically become isolated Serverless Functions. This allows page rendering and API requests to scale infinitely.

### Custom Domains, Environment Variables, Automatic HTTPS, and more
Expand Down
2 changes: 2 additions & 0 deletions examples/with-apollo-and-redux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This example serves as a conduit if you were using Apollo 1.X with Redux, and ar

In 3.0.0, Apollo serves out-of-the-box support for redux in favor of Apollo's state management. This example aims to be an amalgamation of the [`with-apollo`](https://github.com/vercel/next.js/tree/master/examples/with-apollo) and [`with-redux`](https://github.com/vercel/next.js/tree/master/examples/with-redux) examples.

To inspect the GraphQL API, use its [web IDE](https://nextjs-graphql-with-prisma-simple.vercel.app/api).

## Deploy your own

Deploy the example using [Vercel](https://vercel.com):
Expand Down
2 changes: 1 addition & 1 deletion examples/with-apollo-and-redux/components/PostList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PostUpvoter from './PostUpvoter'

export const ALL_POSTS_QUERY = gql`
query allPosts($first: Int!, $skip: Int!) {
allPosts(orderBy: createdAt_DESC, first: $first, skip: $skip) {
allPosts(orderBy: { createdAt: desc }, first: $first, skip: $skip) {
id
title
votes
Expand Down
11 changes: 5 additions & 6 deletions examples/with-apollo-and-redux/components/PostUpvoter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { gql, useMutation } from '@apollo/client'
import PropTypes from 'prop-types'

const UPDATE_POST_MUTATION = gql`
mutation updatePost($id: ID!, $votes: Int) {
updatePost(id: $id, votes: $votes) {
const VOTE_POST = gql`
mutation votePost($id: String!) {
votePost(id: $id) {
__typename
id
votes
Expand All @@ -12,13 +12,12 @@ const UPDATE_POST_MUTATION = gql`
`

const PostUpvoter = ({ votes, id }) => {
const [updatePost] = useMutation(UPDATE_POST_MUTATION)
const [votePost] = useMutation(VOTE_POST)

const upvotePost = () => {
updatePost({
votePost({
variables: {
id,
votes: votes + 1,
},
optimisticResponse: {
__typename: 'Mutation',
Expand Down
28 changes: 15 additions & 13 deletions examples/with-apollo-and-redux/components/Submit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { gql, useMutation } from '@apollo/client'
import { ALL_POSTS_QUERY, allPostsQueryVars } from './PostList'

const CREATE_POST_MUTATION = gql`
mutation createPost($title: String!, $url: String!) {
Expand All @@ -26,19 +25,22 @@ const Submit = () => {

createPost({
variables: { title, url },
update: (proxy, { data: { createPost } }) => {
const data = proxy.readQuery({
query: ALL_POSTS_QUERY,
variables: allPostsQueryVars,
})
// Update the cache with the new post at the top of the
proxy.writeQuery({
query: ALL_POSTS_QUERY,
data: {
...data,
allPosts: [createPost, ...data.allPosts],
update: (cache, { data: { createPost } }) => {
cache.modify({
fields: {
allPosts(existingPosts = []) {
const newPostRef = cache.writeFragment({
data: createPost,
fragment: gql`
fragment NewPost on allPosts {
id
type
}
`,
})
return [newPostRef, ...existingPosts]
},
},
variables: allPostsQueryVars,
})
},
})
Expand Down
13 changes: 11 additions & 2 deletions examples/with-apollo-and-redux/lib/apollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { useMemo } from 'react'
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'
import { concatPagination } from '@apollo/client/utilities'
import merge from 'deepmerge'
import isEqual from 'lodash/isEqual'

let apolloClient

function createApolloClient() {
return new ApolloClient({
ssrMode: typeof window === 'undefined',
link: new HttpLink({
uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
uri: 'https://nextjs-graphql-with-prisma-simple.vercel.app/api', // Server URL (must be absolute)
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
}),
cache: new InMemoryCache({
Expand All @@ -34,7 +35,15 @@ export function initializeApollo(initialState = null) {
const existingCache = _apolloClient.extract()

// Merge the existing cache into data passed from getStaticProps/getServerSideProps
const data = merge(initialState, existingCache)
const data = merge(initialState, existingCache, {
// combine arrays using object equality (like in sets)
arrayMerge: (destinationArray, sourceArray) => [
...sourceArray,
...destinationArray.filter((d) =>
sourceArray.every((s) => !isEqual(d, s))
),
],
})

// Restore the cache with the merged data
_apolloClient.cache.restore(data)
Expand Down
1 change: 1 addition & 0 deletions examples/with-apollo-and-redux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@apollo/client": "^3.0.0",
"deepmerge": "^4.2.2",
"graphql": "14.5.8",
"lodash": "4.17.20",
"next": "latest",
"prop-types": "^15.6.0",
"react": "^16.11.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
]
},
"dependencies": {
"@ampproject/toolbox-optimizer": "2.7.0-alpha.1",
"@ampproject/toolbox-optimizer": "2.7.1-alpha.0",
"@babel/runtime": "7.12.5",
"@hapi/accept": "5.0.1",
"@next/env": "10.0.4-canary.1",
Expand Down
Loading

0 comments on commit b436967

Please sign in to comment.