Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Pass headers here if your graphql server requires them
}
});
networkInterface.use([{
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {}; // Create the header object if needed.
}
// get the authentication token from local storage if it exists
req.options.headers.authorization = cookie.load('token') || null;
next();
}
}]);
return new ApolloClient({
initialState,
ssrMode: !process.browser,
dataIdFromObject: result => result.id || null,
networkInterface: networkInterface
})
}
function create(initialState) {
const networkInterface = createNetworkInterface({
uri: SIMPLE_ENDPOINT,
opts: { // Additional fetch() options like `credentials` or `headers`
credentials: 'same-origin'
},
dataIdFromObject: o => o.id,
})
return new ApolloClient({
initialState,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
networkInterface,
})
}
import Tour from './Tour';
// fancy!
import TOUR_QUERY from './tour.graphql';
const TourWithData = graphql(TOUR_QUERY, {
options: ({ tourRepository }) => ({ variables: { tourRepository } }),
props: ({ data: { tour, loading, error } }) => ({
tour,
loading,
error,
}),
})(Tour);
const client = new ApolloClient({
networkInterface: createNetworkInterface({
// === CodeTours GraphQL server
uri: 'http://localhost:8080/graphql',
}),
});
export default () =>
;
function createClient (headers) {
return new ApolloClient({
ssrMode: !process.browser,
dataIdFromObject: result => result.id || null,
networkInterface: createNetworkInterface({
// open this url in browser to see schema
uri: 'https://api.graph.cool/simple/v1/cj0n21ce5ffo50118z67ao3th',
opts: {
credentials: 'same-origin'
// Pass headers here if your graphql server requires them
}
})
})
}
// use the auth0IdToken in localStorage for authorized requests
networkInterface.use([{
applyMiddleware: function applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {};
}
// get the authentication token from local storage if it exists
if (typeof localStorage !== 'undefined' && localStorage.getItem("auth0IdToken")) {
req.options.headers.authorization = 'Bearer ' + localStorage.getItem("auth0IdToken");
}
next();
}
}]);
return new _reactApollo.ApolloClient({
initialState: initialState,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
networkInterface: networkInterface
});
}
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {}; // eslint-disable-line no-param-reassign
}
if (userToken) {
req.options.headers.authorization = `Bearer ${userToken}`; // eslint-disable-line no-param-reassign
} else if (req.options.headers.authorization) {
delete req.options.headers.authorization; // eslint-disable-line no-param-reassign
}
next();
},
}]);
return new ApolloClient({
ssrMode: !process.browser,
dataIdFromObject: result => result.id || null,
networkInterface,
});
}
{
networkInterface = createNetworkInterface({
uri: apolloUri,
opts
})
}
client = new ApolloClient({
ssrMode,
queryDeduplication,
networkInterface
})
}
else
{
client = new ApolloClient({
ssrMode,
queryDeduplication
})
}
return client
}
function createClient (headers) {
return new ApolloClient({
ssrMode: !process.browser,
dataIdFromObject: result => result.id || null,
queryDeduplication: true,
fragmentMatcher,
networkInterface: createNetworkInterface({
uri: `${API_BASE_URL}/graphql`,
opts: {
credentials: 'include',
headers: {
Authorization: API_AUTHORIZATION_HEADER,
cookie: headers.cookie
}
}
})
})
}