Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 create (initialState) {
// 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: ApolloLink.from([
new ApolloLink((operation, forward) => {
operation.setContext({
headers: {
Authorization: process.browser && localStorage.token
}
});
return forward(operation)
}),
new HttpLink({
uri: config.url,
credentials: 'same-origin',
fetch (uri, options) {
const { operationName } = JSON.parse(options.body)
return fetch(`${uri}?query=${operationName}`, options)
}
})
const authLink = setContext((_, { headers }) => {
const token = getToken();
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: ApolloLink.from([
errorLink,
authLink,
new HttpLink({
uri:
process.env.NODE_ENV === "development"
? "http://localhost:3000/api/graphql"
: `https://graphql-fullstack.now.sh/api/graphql`, // Server URL (must be absolute)
credentials: "same-origin", // Additional fetch() options like `credentials` or `headers`
// Use fetch() polyfill on the server
fetch: !isBrowser && fetch,
}),
]),
cache: new InMemoryCache().restore(initialState || {}),
});
}
if (error.graphQLErrors && error.graphQLErrors[0]) {
const graphQLError = error.graphQLErrors[0] as RadioGraphQLError;
let errorType = 'common';
if (/Token expired/.test(graphQLError.message)) {
console.log('found error', graphQLError.message);
localStorage.removeItem('token');
localStorage.removeItem('refreshToken');
client.resetStore();
errorType = 'expired_token';
}
config.onError(graphQLError, errorType);
}
});
client = new ApolloClient({
link: ApolloLink.from([authLink, errorLink, link]),
cache: new InMemoryCache()
});
connectionAttempts = 0;
return client;
}
}
if (networkError) console.error(`[Network error]: ${networkError}`);
});
const authLink = setContext((_, { headers }) => {
const token = getToken();
return {
headers: {
...headers,
authorization: token || null,
},
};
});
const client = new ApolloClient({
link: ApolloLink.from([errorLink, authLink, uploadLink]),
cache: new InMemoryCache(),
});
return (
import { createBrowserHistory } from 'history'
import { RouterStore, syncHistoryWithStore } from 'mobx-react-router'
import { createContext, useContext } from 'react'
import { AuthStore } from './auth'
import { ContestStore } from './contest'
import { ProblemStore } from './problem'
import { ServerStore } from './server'
import { SubmissionStore } from './submission'
import { WindowStore } from './window'
export const browserHistory = createBrowserHistory()
export const routingStore = new RouterStore()
export const history = syncHistoryWithStore(browserHistory, routingStore)
export const apolloClient = new ApolloClient({
link: ApolloLink.from([
createUploadLink({ uri: 'http://localhost:8000/graphql', credentials: 'same-origin' }),
new HttpLink({ uri: 'http://localhost:8000/graphql', credentials: 'same-origin' }),
]),
cache: new InMemoryCache(),
defaultOptions: {
query: {
fetchPolicy: 'no-cache',
},
},
})
export const authStore = new AuthStore(apolloClient)
export const contestStore = new ContestStore(authStore, apolloClient)
export const problemStore = new ProblemStore(authStore, contestStore, apolloClient)
export const submissionStore = new SubmissionStore(apolloClient, authStore, contestStore)
export const serverStore = new ServerStore(apolloClient)
export const windowStore = new WindowStore()
import { ApolloLink, InMemoryCache } from 'apollo-boost'
import { ApolloClient } from 'apollo-client'
import { createUploadLink } from 'apollo-upload-client'
import { authLink } from './auth'
import { GRAPHQL_API } from './config'
export const apolloClient = new ApolloClient({
link: ApolloLink.from([
authLink,
createUploadLink({
uri: GRAPHQL_API,
credentials: 'same-origin',
}),
]),
cache: new InMemoryCache(),
defaultOptions: {
query: {
fetchPolicy: 'no-cache',
},
},
})
bind(GraphQlTypes.Client).toDynamicValue((context: interfaces.Context) => {
const schema: GraphQLSchema = context.container.get(GraphQlTypes.RootSchema);
return new ApolloClient({
cache,
connectToDevTools: true,
link: ApolloLink.from([
contextContainerLink(context.container),
new DiscoveryLink(),
new SchemaLink({ schema, context: { cache, container: context.container } })
])
});
});
}