-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature/Apollo] Upgrade to Apollo Client 2.0 (#1492)
* ⬆️ Upgrade to Apollo Client 2.0 * Tweak HttpLink Credentials * Fix Layout test
- Loading branch information
Showing
9 changed files
with
169 additions
and
122 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import ApolloClient, { createNetworkInterface } from 'apollo-client'; | ||
import { ApolloClient } from 'apollo-client'; | ||
import { InMemoryCache } from 'apollo-cache-inmemory'; | ||
import { from } from 'apollo-link'; | ||
import { onError } from 'apollo-link-error'; | ||
import { HttpLink } from 'apollo-link-http'; | ||
|
||
const client = new ApolloClient({ | ||
networkInterface: createNetworkInterface({ | ||
const link = from([ | ||
onError(({ graphQLErrors, networkError }) => { | ||
if (graphQLErrors) | ||
graphQLErrors.map(({ message, locations, path }) => | ||
console.warn( | ||
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`, | ||
), | ||
); | ||
if (networkError) console.warn(`[Network error]: ${networkError}`); | ||
}), | ||
new HttpLink({ | ||
uri: '/graphql', | ||
opts: { | ||
// Additional fetch options like `credentials` or `headers` | ||
credentials: 'include', | ||
}, | ||
credentials: 'include', | ||
}), | ||
queryDeduplication: true, | ||
reduxRootSelector: state => state.apollo, | ||
}); | ||
]); | ||
|
||
const cache = new InMemoryCache(); | ||
|
||
export default function createApolloClient() { | ||
return client; | ||
return new ApolloClient({ | ||
link, | ||
cache: cache.restore(window.__APOLLO_CLIENT__), // eslint-disable-line no-underscore-dangle | ||
ssrMode: true, | ||
queryDeduplication: true, | ||
connectToDevTools: true, | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,47 +1,27 @@ | ||
import { validate, execute, specifiedRules } from 'graphql'; | ||
import { ApolloClient } from 'apollo-client'; | ||
import { InMemoryCache } from 'apollo-cache-inmemory'; | ||
import { from } from 'apollo-link'; | ||
import { onError } from 'apollo-link-error'; | ||
import { SchemaLink } from 'apollo-link-schema'; | ||
|
||
import ApolloClient from 'apollo-client'; | ||
export default function createApolloClient(schema) { | ||
const link = from([ | ||
onError(({ graphQLErrors, networkError }) => { | ||
if (graphQLErrors) | ||
graphQLErrors.map(({ message, locations, path }) => | ||
console.warn( | ||
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`, | ||
), | ||
); | ||
if (networkError) console.warn(`[Network error]: ${networkError}`); | ||
}), | ||
new SchemaLink({ ...schema }), | ||
]); | ||
|
||
// Execute all GraphQL requests directly without | ||
class ServerInterface { | ||
constructor(optionsData) { | ||
this.schema = optionsData.schema; | ||
this.optionsData = optionsData; | ||
} | ||
|
||
async query({ query, variables, operationName }) { | ||
try { | ||
let validationRules = specifiedRules; | ||
const customValidationRules = this.optionsData.validationRules; | ||
if (customValidationRules) { | ||
validationRules = validationRules.concat(customValidationRules); | ||
} | ||
|
||
const validationErrors = validate(this.schema, query, validationRules); | ||
if (validationErrors.length > 0) { | ||
return { errors: validationErrors }; | ||
} | ||
|
||
const result = await execute( | ||
this.schema, | ||
query, | ||
this.optionsData.rootValue, | ||
this.optionsData.context, | ||
variables, | ||
operationName, | ||
); | ||
|
||
return result; | ||
} catch (contextError) { | ||
return { errors: [contextError] }; | ||
} | ||
} | ||
} | ||
|
||
export default function createApolloClient(options) { | ||
return new ApolloClient({ | ||
reduxRootSelector: state => state.apollo, | ||
networkInterface: new ServerInterface(options), | ||
link, | ||
cache: new InMemoryCache(), | ||
ssrMode: true, | ||
queryDeduplication: true, | ||
}); | ||
} |
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
Oops, something went wrong.