Added apollo client and basic provider setup.

This commit is contained in:
Patrick Fic
2020-08-11 21:42:21 -07:00
parent 11d92e9aea
commit 1f8d16370a
12 changed files with 1606 additions and 22 deletions

View File

@@ -0,0 +1,73 @@
import gql from "graphql-tag";
export const QUERY_BODYSHOP = gql`
subscription QUERY_BODYSHOP {
bodyshops(where: { associations: { active: { _eq: true } } }) {
associations {
authlevel
useremail
user {
authid
email
dashboardlayout
employee {
id
}
}
}
address1
address2
city
country
created_at
email
federal_tax_id
id
insurance_vendor_id
logo_img_path
md_ro_statuses
md_order_statuses
shopname
state
state_tax_id
updated_at
zip_post
shoprates
region_config
md_responsibility_centers
messagingservicesid
template_header
textid
production_config
invoice_tax_rates
inhousevendorid
accountingconfig
appt_length
stripe_acct_id
ssbuckets
scoreboard_target
md_referral_sources
md_messaging_presets
intakechecklist
speedprint
md_parts_locations
md_notes_presets
md_rbac
employees {
id
first_name
last_name
employee_number
cost_center
}
}
}
`;
export const QUERY_SHOP_ID = gql`
query QUERY_SHOP_ID {
bodyshops(where: { associations: { active: { _eq: true } } }) {
id
}
}
`;

138
graphql/client.js Normal file
View File

@@ -0,0 +1,138 @@
//GQL Imports
import {
ApolloClient,
InMemoryCache,
split,
ApolloLink,
HttpLink,
from,
} from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
import apolloLogger from "apollo-link-logger";
import { RetryLink } from "@apollo/client/link/retry";
import { WebSocketLink } from "@apollo/client/link/ws";
import { SubscriptionClient } from "subscriptions-transport-ws";
import { auth } from "../firebase/firebase.utils";
import { onError } from "@apollo/client/link/error";
import { getMainDefinition } from "@apollo/client/utilities";
const httpLink = new HttpLink({
uri: "https://bodyshop-dev-db.herokuapp.com/v1/graphql",
});
const wsLink = new WebSocketLink({
uri: "wss://bodyshop-dev-db.herokuapp.com/v1/graphql",
options: {
lazy: true,
reconnect: true,
connectionParams: async () => {
const token =
auth.currentUser && (await auth.currentUser.getIdToken(true));
if (token) {
return {
headers: {
authorization: token ? `Bearer ${token}` : "",
},
};
}
},
},
});
//https://stackoverflow.com/questions/57163454/refreshing-a-token-with-apollo-client-firebase-auth
const errorLink = onError(
({ graphQLErrors, networkError, operation, forward }) => {
console.log(graphQLErrors);
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
);
if (networkError)
console.log(`[Network error]: ${JSON.stringify(networkError)}`);
console.log(operation.getContext());
}
);
const subscriptionMiddleware = {
applyMiddleware: async (options, next) => {
options.authToken =
auth.currentUser && (await auth.currentUser.getIdToken(true));
next();
},
};
wsLink.subscriptionClient.use([subscriptionMiddleware]);
const link = split(
// split based on operation type
({ query }) => {
const definition = getMainDefinition(query);
// console.log(
// "##Intercepted GQL Transaction : " +
// definition.operation +
// "|" +
// definition.name.value +
// "##",
// query
// );
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
wsLink,
httpLink
);
const authLink = setContext((_, { headers }) => {
return (
auth.currentUser &&
auth.currentUser.getIdToken().then((token) => {
if (token) {
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
},
};
} else {
return { headers };
}
})
);
});
const retryLink = new RetryLink({
delay: {
initial: 500,
max: 5,
jitter: true,
},
attempts: {
max: 5,
retryIf: (error, _operation) => !!error,
},
});
const middlewares = [];
if (process.env.NODE_ENV === "development") {
middlewares.push(apolloLogger);
}
middlewares.push(retryLink.concat(errorLink.concat(authLink.concat(link))));
const cache = new InMemoryCache({});
export const client = new ApolloClient({
//link: ApolloLink.from(middlewares),
link: from([authLink, link]),
cache,
// connectToDevTools: process.env.NODE_ENV !== "production",
// defaultOptions: {
// watchQuery: {
// fetchPolicy: "cache-and-network",
// },
// },
});

1115
graphql/jobs.queries.js Normal file

File diff suppressed because it is too large Load Diff