Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (error) {
const { message, locations, path } = error;
console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
return;
}
console.error('[GraphQL error]: Received null error');
});
}
if (networkError) {
console.error(`[Network error]: ${networkError}`);
}
});
const httpLink = new HttpLink({
uri: getGraphqlUrl(),
fetch,
});
const link = ApolloLink.from([errorLink, authLink.concat(httpLink)]);
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
cache: cache.restore(initialState),
link,
});
}
function createApolloClient(initialState = {}) {
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return new ApolloClient({
ssrMode: typeof window === 'undefined', // 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)
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
fetch,
}),
cache: new InMemoryCache().restore(initialState),
})
}
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
import { InMemoryCache } from 'apollo-cache-inmemory'
import VueApollo from 'vue-apollo'
import App from './App'
import router from './router'
Vue.config.productionTip = false
// install the vue-momnet plugin
Vue.use(require('vue-moment'))
const httpLink = new HttpLink({ uri: 'http://localhost:4000/' })
const httpLinkAuth = setContext((_, { headers }) => {
// get the authentication token from localstorage if it exists
const token = localStorage.getItem('USER_TOKEN')
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
Authorization: token ? `Bearer ${token}` : ''
}
}
})
// Create the apollo client
const apolloClient = new ApolloClient({
const token = localStorage.getItem('growth_auth_token')
const returnObject = {
headers: {
...headers
}
}
if (token) {
returnObject.headers['authentication'] = `{"growth_auth_token": "${token}"}`
}
// return the headers to the context so httpLink can read them
return returnObject
})
const httpLink = new HttpLink({ uri: `${growthServerAddress}/graphql`, fetch })
const schema = mergeSchemas({
schemas: [
makeRemoteExecutableSchema({
schema: growthSchema,
link: authLink.concat(httpLink)
}),
makeExecutableSchema({ typeDefs, resolvers })
]
})
export default schema
async function run() {
// Step 1: Create local version of the CRUD API
const link = new HttpLink({ uri: process.env.GRAPHQL_ENDPOINT, fetch })
const graphcoolSchema = makeRemoteExecutableSchema({
schema: await introspectSchema(link),
link,
})
// Step 2: Define schema for the new API
const extendTypeDefs = `
extend type Query {
viewer: Viewer!
}
type Viewer {
me: User
topPosts(limit: Int): [Post!]!
}
clientName: CLIENT_NAME,
clientVersion: CLIENT_VERSION
};
operation.setContext({
http: {
includeExtensions: true
}
});
return forward(operation);
});
// Terminating link to fetch data from server
// NOTE: Reads request headers from context
const networkLink = new HttpLink({
uri: GRAPHQL_ENDPOINT
});
const apolloClient = new ApolloClient({
cache,
link: ApolloLink.from([
errorLink,
stateLink,
authLink,
clientIdentifierLink,
networkLink
])
});
return {
apolloClient,
getApolloLink() {
return (
this.options.link ||
new HttpLink({
uri: this.config.graphqlUri,
fetch,
})
);
}
import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';
import { useQuery, ApolloProvider } from '@apollo/react-hooks';
import gql from 'graphql-tag';
const client = new ApolloClient({
cache: new InMemoryCache(),
link: new HttpLink({
uri: 'http://localhost:4000/graphql'
})
});
client.writeData({
data: {
isLoggedIn: false
}
});
const IS_LOGGED_IN = gql`
query IsUserLoggedIn {
isLoggedIn @client
}
`;
function makeHttpLink(clientName, {urlMap}) {
return new HttpLink({ uri: urlMap[clientName].uri, fetch, credentials: 'include' });
}
export default () => ({
link: new HttpLink({ uri: GRAPHCMS_API }),
cache: new InMemoryCache(),
defaultHttpLink: false
})