Complete rewrite of local state management.

This commit is contained in:
Patrick Fic
2019-12-06 23:30:15 -08:00
parent 8a72294c91
commit 4f3d917e06
17 changed files with 366 additions and 217 deletions

View File

@@ -0,0 +1,34 @@
import ApolloClient from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";
import { resolvers, typeDefs } from "./resolvers";
import apolloLogger from "apollo-link-logger";
import { ApolloLink } from "apollo-boost";
const httpLink = new HttpLink({
uri: process.env.REACT_APP_GRAPHQL_ENDPOINT
});
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
if (token) {
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ""
}
};
} else {
return { headers };
}
});
export const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({ addTypename: false }),
typeDefs,
resolvers
});

View File

@@ -1,7 +1,8 @@
export default {
currentUser: {
email: null,
displayName: null
displayName: null,
token: null
},
selectedNavItem: "Home",
recentItems: []

View File

@@ -0,0 +1,17 @@
import { gql } from "apollo-boost";
export const SET_CURRENT_USER = gql`
mutation SetCurrentUser($user: User!) {
setCurrentUser(user: $user) @client {
email
}
}
`;
export const GET_CURRENT_USER = gql`
{
currentUser @client {
email
}
}
`;

View File

@@ -21,14 +21,7 @@ export const GET_SELECTED_NAV_ITEM = gql`
}
`;
export const GET_CURRENT_USER = gql`
query GetCurrentUser {
currentUser @client {
email
displayName
}
}
`;
// export const SET_CURRENT_USER = gql`
// mutation SetCurrentUser($user User!){

View File

@@ -1,20 +1,15 @@
import { gql } from "apollo-boost";
import { GET_CURRENT_USER } from "./local.queries";
export const typeDefs = gql`
extend type Mutation {
SetCurrentUser(user: User!): User!
}
type User {
displayName: String!,
email: String!,
photoUrl: String!
}
`;
const GET_CURRENT_USER = gql`
{
currentUser @client
extend type User {
email: String!
displayName: String!
token: String!
}
`;