const Client = () => { const networkInterface = createNetworkInterface({ uri: 'http://localhost:8080/graphql' }) const client = new ApolloClient({ networkInterface }); return ( <ApolloProvider client={client}> <App /> </ApolloProvider>) }
createClient() { return new ApolloClient({ networkInterface: createNetworkInterface({ uri: 'http://api.githunt.com/graphql' }), // Inform Apollo about the id field of our objects. // Apollo will use this field as the key for its caches. dataIdFromObject: o => o.id }); }
function create (initialState) { return new ApolloClient({ initialState, ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once) networkInterface: createNetworkInterface({ uri: 'http://localhost:4000/graphql', // Server URL (must be absolute) opts: { // Additional fetch() options like `credentials` or `headers` credentials: 'same-origin' } }) }) }
var getClient = function getClient(_ref) { var client = _ref.client, clientOptions = _ref.clientOptions; if (client) return client; if (clientOptions) { var networkInterface = clientOptions.networkInterface, uri = clientOptions.uri, options = (0, _objectWithoutProperties3.default)(clientOptions, ['networkInterface', 'uri']); if (networkInterface) { if (networkInterface && uri) { console.error('Warning: You specified a networkInterface and an uri option. uri will be ignored.'); } return new _apolloClient.ApolloClient((0, _extends3.default)({}, options, { networkInterface: networkInterface })); } if (!networkInterface && uri) { options.networkInterface = (0, _apolloClient.createNetworkInterface)({ uri: uri }); } return new _apolloClient.ApolloClient(options); } return new _apolloClient.ApolloClient(); }
createNetworkInterface({ uri: _.get(process.env, "REACT_APP_GRAPHQL_URL") || "http://localhost:4000/graphql", dataIdFromObject: object => object.id })
const _initNetworkInterface = graphql_url => { const networkInterface = createNetworkInterface({ uri: graphql_url, opts: { credentials: 'same-origin' } }) // Authen 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 (async () => { const token = await persist.willGetSessionToken() req.options.headers.authorization = token ? `Bearer ${token}` : null next() })() } }]) return networkInterface }
const getClient = ({ client, clientOptions }) => { if (client) return client; if (clientOptions) { const { networkInterface, uri, ...options } = clientOptions; if (networkInterface) { if (networkInterface && uri) { console.error('Warning: You specified a networkInterface and an uri option. uri will be ignored.'); } return new ApolloClient({ ...options, networkInterface }); } if (!networkInterface && uri) { options.networkInterface = createNetworkInterface({ uri }); } return new ApolloClient(options); } return new ApolloClient(); }
export default function render(req, res) { const client = new ApolloClient({ ssrMode: true, networkInterface: createNetworkInterface({ uri: apiUrl }, { credentials: 'same-origin', headers: req.headers
const result = getClient({ clientOptions: { networkInterface: createNetworkInterface({ uri: 'http://localhost.dev/graphql' }), }, });
function create(data) { const networkInterface = createNetworkInterface({ uri: data.endPoint, }); const middleWares = [ { applyMiddleware(req, next) { const token = data.oAuthToken; if (token) { req.options.headers = {}; req.options.headers.authorization = `Bearer ${token}`; } next(); }, }, ]; networkInterface.use(middleWares); const severNetworkInterface = createNetworkInterface({ uri: data.endPoint }); severNetworkInterface.use(middleWares); return new ApolloClient({ ssrMode: true, networkInterface: severNetworkInterface, }); }
constructor(...args) { super(...args); const networkInterface = createNetworkInterface('https://swapi.apis.guru'); this.client = new ApolloClient({ networkInterface, dataIdFromObject: r => r.id, }); }
const Client = () => { const networkInterface = createNetworkInterface({ uri: 'http://localhost:8080/graphql' }) const client = new ApolloClient({ networkInterface }); return ( <ApolloProvider client={client}> <App /> </ApolloProvider>) }