-
Notifications
You must be signed in to change notification settings - Fork 817
Expand file tree
/
Copy pathindex.tsx
More file actions
64 lines (57 loc) · 1.59 KB
/
index.tsx
File metadata and controls
64 lines (57 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import ReactDOM from 'react-dom';
import {
ApolloClient,
NormalizedCacheObject,
ApolloProvider,
gql,
useQuery
} from '@apollo/client';
import Pages from './pages';
import Login from './pages/login';
import injectStyles from './styles';
import { cache } from './cache';
export const typeDefs = gql`
extend type Query {
isLoggedIn: Boolean!
cartItems: [ID!]!
}
`;
// Set up our apollo-client to point at the server we created
// this can be local or a remote endpoint
const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
cache,
uri: 'http://localhost:4000/graphql',
headers: {
authorization: localStorage.getItem('token') || '',
'client-name': 'Space Explorer [web]',
'client-version': '1.0.0',
},
typeDefs,
resolvers: {},
});
/**
* Render our app
* - We wrap the whole app with ApolloProvider, so any component in the app can
* make GraphqL requests. Our provider needs the client we created above,
* so we pass it as a prop
* - We need a router, so we can navigate the app. We're using React router for this.
* The router chooses between which component to render, depending on the url path.
* ex: localhost:3000/login will render only the `Login` component
*/
const IS_LOGGED_IN = gql`
query IsUserLoggedIn {
isLoggedIn @client
}
`;
function IsLoggedIn() {
const { data } = useQuery(IS_LOGGED_IN);
return data.isLoggedIn ? <Pages /> : <Login />;
}
injectStyles();
ReactDOM.render(
<ApolloProvider client={client}>
<IsLoggedIn />
</ApolloProvider>,
document.getElementById('root'),
);