-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
163 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ApolloClient } from 'apollo-client' | ||
import { InMemoryCache } from 'apollo-cache-inmemory' | ||
import { split } from 'apollo-link' | ||
import { WebSocketLink } from 'apollo-link-ws' | ||
import { createHttpLink } from 'apollo-link-http' | ||
import { getMainDefinition } from 'apollo-utilities' | ||
import { setContext } from 'apollo-link-context' | ||
import { getAuthToken } from 'auth0-helpers' | ||
|
||
import { errorLink } from './errorLink' | ||
|
||
const httpLink = createHttpLink({ | ||
uri: 'https://api.graphql.guide/graphql' | ||
}) | ||
|
||
const authLink = setContext(async (_, { headers }) => { | ||
const token = await getAuthToken({ | ||
doLoginIfTokenExpired: true | ||
}) | ||
|
||
if (token) { | ||
return { | ||
headers: { | ||
...headers, | ||
authorization: `Bearer ${token}` | ||
} | ||
} | ||
} else { | ||
return { headers } | ||
} | ||
}) | ||
|
||
const authedHttpLink = authLink.concat(httpLink) | ||
|
||
const wsLink = new WebSocketLink({ | ||
uri: `wss://api.graphql.guide/subscriptions`, | ||
options: { | ||
reconnect: true | ||
} | ||
}) | ||
|
||
const networkLink = split( | ||
({ query }) => { | ||
const { kind, operation } = getMainDefinition(query) | ||
return kind === 'OperationDefinition' && operation === 'subscription' | ||
}, | ||
wsLink, | ||
authedHttpLink | ||
) | ||
|
||
const link = errorLink.concat(networkLink) | ||
|
||
const cache = new InMemoryCache() | ||
|
||
export const apollo = new ApolloClient({ link, cache }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import auth0 from 'auth0-js' | ||
import { | ||
initAuthHelpers, | ||
login as auth0Login, | ||
logout as auth0Logout | ||
} from 'auth0-helpers' | ||
|
||
import { apollo } from './apollo' | ||
|
||
const client = new auth0.WebAuth({ | ||
domain: 'graphql.auth0.com', | ||
clientID: '8fErnZoF3hbzQ2AbMYu5xcS0aVNzQ0PC', | ||
responseType: 'token', | ||
audience: 'https://api.graphql.guide', | ||
scope: 'openid profile guide' | ||
}) | ||
|
||
initAuthHelpers({ | ||
client, | ||
usePopup: true, | ||
authOptions: { | ||
connection: 'github', | ||
owp: true, | ||
popupOptions: { height: 623 } // make tall enough for content | ||
}, | ||
checkSessionOptions: { | ||
redirect_uri: window.location.origin | ||
}, | ||
onError: e => console.error(e) | ||
}) | ||
|
||
export const login = () => { | ||
auth0Login({ | ||
onCompleted: e => { | ||
if (e) { | ||
console.error(e) | ||
return | ||
} | ||
|
||
apollo.reFetchObservableQueries() | ||
} | ||
}) | ||
} | ||
|
||
export const logout = () => { | ||
auth0Logout() | ||
apollo.resetStore() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { graphql } from 'react-apollo' | ||
import gql from 'graphql-tag' | ||
|
||
export const USER_QUERY = gql` | ||
query UserQuery { | ||
currentUser { | ||
firstName | ||
name | ||
username | ||
photo | ||
hasPurchased | ||
favoriteReviews { | ||
id | ||
} | ||
} | ||
} | ||
` | ||
|
||
export const withUser = graphql(USER_QUERY, { | ||
props: ({ data: { currentUser, loading } }) => ({ | ||
user: currentUser, | ||
loggingIn: loading | ||
}) | ||
}) |