Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
return {
headers: {
...prevCtx.headers,
...authHeaders
}
}
} catch (ex) {
console.error(ex)
}
})
const link = ApolloLink.from([authLink, stateLink, httpLink])
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link,
cache
})
}
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
);
if (isBrowser && message.includes("not authenticated")) {
Router.replace("/login");
}
});
}
if (networkError) {
console.log(`[Network error]: ${networkError}`);
}
});
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return new ApolloClient({
connectToDevTools: isBrowser,
ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
// link: authLink.concat(httpLink),
link: ApolloLink.from([errorLink, authLink, httpLink]), // Composing Links
cache: new InMemoryCache().restore(initialState || {})
});
}
function createApolloClient(initialState: NormalizedCacheObject) {
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link: new HttpLink({
// uri: "https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn", // Server URL (must be absolute)
uri: "http://localhost:3030/graphql"
// credentials: true
// credentials: "same-origin" // Additional fetch() options like `credentials` or `headers`
}),
cache: new InMemoryCache().restore(initialState)
});
}
function create (initialState) {
const authMiddleware = new ApolloLink((operation, forward) => {
// add the authorization to the headers
operation.setContext({
headers: {
authorization: getToken(),
}
})
return forward(operation);
})
const httpLink = new HttpLink({
uri: 'https://my-app.herokuapp.com/v1/graphql', // Server URL (must be absolute)
credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers`
})
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link: concat(authMiddleware, httpLink),
cache: new InMemoryCache().restore(initialState || {})
})
}
function create(initialState) {
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
link: new HttpLink({
// TODO - replace with env var
uri: GRAPHQL_URL, // Server URL (must be absolute)
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
}),
cache: new InMemoryCache().restore(initialState || {}),
});
}
uri: process.env.NODE_ENV === "production" ? process.env.BACKEND_URI : "http://localhost:4000",
credentials: "include",
fetchOptions
})
const authLink = setContext((_, { headers }) => {
const token = Cookies.get('token')
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
}
})
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
const isBrowser = typeof window !== 'undefined'
return new ApolloClient({
connectToDevTools: isBrowser,
ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
link: authLink.concat(httpLink),
cache: new InMemoryCache().restore(initialState || {})
})
}
export default function initApollo (initialState, options) {
function createApolloClient(apolloConfig, initialState = {}) {
const createCache = apolloConfig.createCache || createDefaultCache
const config = {
ssrMode: typeof window === 'undefined', // Disables forceFetch on the server (so queries are only run once)
cache: createCache().restore(initialState || {}),
...apolloConfig
}
delete config.createCache
return new ApolloClient(config)
}
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
// Use fetch() polyfill on the server
fetch: !isBrowser && fetch
});
const authLink = setContext((_, { headers }) => {
const token = getToken && getToken();
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
};
});
return new ApolloClient({
connectToDevTools: isBrowser,
ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
link: authLink.concat(httpLink),
cache: new InMemoryCache().restore(initialState || {})
});
}
import { ApolloClient, HttpLink, InMemoryCache } from 'apollo-boost';
import 'isomorphic-unfetch';
import config from '../apollo.config';
export const shopify = new ApolloClient({
ssrMode: true,
link: new HttpLink({
uri: config.client.service.url,
headers: config.client.service.headers
}),
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: {
fetchPolicy: 'no-cache',
errorPolicy: 'ignore'
},
query: {
fetchPolicy: 'no-cache',
errorPolicy: 'all'
},
mutate: {
}
}),
httpLink
)
const authLink = setContext((_, { headers }) => {
const token = getToken()
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
}
})
const apolloClient = new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser,
ssrForceFetchDelay: 100,
link: authLink.concat(link),
cache: new InMemoryCache().restore({})
})
const main = (
<main>
)
ReactDOM.render(main, document.getElementById('root'))</main>