diff --git a/examples/react-graphql/client/src/index.tsx b/examples/react-graphql/client/src/index.tsx index f7c1f7435..3f71d2b5d 100644 --- a/examples/react-graphql/client/src/index.tsx +++ b/examples/react-graphql/client/src/index.tsx @@ -5,7 +5,20 @@ import ApolloClient from 'apollo-boost' import { ApolloProvider } from 'react-apollo' import * as serviceWorker from './serviceWorker' -const client = new ApolloClient({ uri: 'http://localhost:4000/' }) +const client = new ApolloClient({ + uri: 'http://localhost:4000/', + + // Authorization is out of scope for this example, + // but this is where you could add your auth logic + request: operation => { + const token = 'hardcoded-example-token' + operation.setContext({ + headers: { + authorization: token ? `Bearer ${token}` : '', + }, + }) + }, +}) ReactDOM.render( diff --git a/examples/react-graphql/server/index.ts b/examples/react-graphql/server/index.ts index fe51df63f..3bbd1ec01 100644 --- a/examples/react-graphql/server/index.ts +++ b/examples/react-graphql/server/index.ts @@ -61,7 +61,16 @@ const server = new ApolloServer({ W3c.Gql.resolvers, SD.Gql.resolvers, ), - context: () => ({ core, dataStore }), + context: ({ req }) => { + // Authorization is out of scope for this example, + // but this is where you could add your auth logic + const token = req.headers.authorization || '' + if (token !== 'Bearer hardcoded-example-token') { + throw Error('Auth error') + } + + return { core, dataStore } + }, introspection: true, })