Complete rewrite of local state management.
This commit is contained in:
34
client/src/graphql/client.js
Normal file
34
client/src/graphql/client.js
Normal 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
|
||||
});
|
||||
@@ -1,7 +1,8 @@
|
||||
export default {
|
||||
currentUser: {
|
||||
email: null,
|
||||
displayName: null
|
||||
displayName: null,
|
||||
token: null
|
||||
},
|
||||
selectedNavItem: "Home",
|
||||
recentItems: []
|
||||
|
||||
17
client/src/graphql/local.queries.js
Normal file
17
client/src/graphql/local.queries.js
Normal 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
|
||||
}
|
||||
}
|
||||
`;
|
||||
@@ -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!){
|
||||
|
||||
@@ -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!
|
||||
}
|
||||
`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user