Skip to content

Instantly share code, notes, and snippets.

@jackinf
Last active December 29, 2018 14:04
Show Gist options
  • Save jackinf/88ae23b3d2e6cfadd0379930d1dcdf6a to your computer and use it in GitHub Desktop.
Save jackinf/88ae23b3d2e6cfadd0379930d1dcdf6a to your computer and use it in GitHub Desktop.
Apollo helpers
const { v1: neo4j } = require('neo4j-driver')
const { ApolloServer, makeExecutableSchema } = require('apollo-server')
const driver = neo4j.driver(
`bolt://${process.env.NEO4J_HOST}:7687`,
neo4j.auth.basic(process.env.NEO4J_USER, process.env.NEO4J_PASS))
const typeDefs = `
type Person { name: String! }
type Planet { name: String! }
type Query {
Person: [Person]
Planet: [Planet]
}
`
const schema = makeExecutableSchema({ typeDefs })
new ApolloServer({
schema,
context: { driver },
}).listen(8000, '0.0.0.0')
.then(({ url }) => console.log(`GraphQL API ready at ${url}`))
// https://www.apollographql.com/docs/react/recipes/authentication.html
import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
const httpLink = createHttpLink({
uri: '/graphql',
});
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem('token');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment