Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: primaryShop query is not working #823

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/apollo/apolloClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { omitTypenameLink } from "./omitVariableTypenameLink";
const STATUS_BAD_REQUEST = 400;

/**
* Instantiate the Apollo client
* Instantiate the Apollo client
* @returns {Object} a new Apollo Client instance
*/
export default function createApolloClient() {
function createApolloClient() {
// Config
let graphqlUrl;

Expand Down Expand Up @@ -63,10 +63,18 @@ export default function createApolloClient() {
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return new ApolloClient({
ssrMode: false,
link: ApolloLink.from([omitTypenameLink, errorLink, typeof window === "undefined" ? serverLink : authLink, httpLink]),
link: ApolloLink.from([
omitTypenameLink,
errorLink,
typeof window === "undefined" ? serverLink : authLink,
httpLink
]),
cache: new InMemoryCache({
typePolicies: {
}
typePolicies: {}
})
});
}

const apolloClient = createApolloClient();

export default apolloClient;
34 changes: 8 additions & 26 deletions lib/apollo/withApollo.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
import React from "react";
import { ApolloProvider } from "@apollo/react-components";
import { ApolloProvider as NewApolloProvider } from "@apollo/client";
import createApolloClient from "./apolloClient";

// On the client we store the apollo client in the following variable
// this prevents the client from reinitializing between page transitions.
let globalApolloClient = null;


const initApolloClient = () => {
// Reuse client on the client-side
if (!globalApolloClient) {
globalApolloClient = createApolloClient();
}

return globalApolloClient;
};
import client from "./apolloClient";

export const withApollo = () => (PageComponent) => {
const WithApollo = ({ ...pageProps }) => {
const client = initApolloClient();

return (
<ApolloProvider client={client}>
<NewApolloProvider client={client}>
<PageComponent {...pageProps} />
</NewApolloProvider>
</ApolloProvider>
);
};
const WithApollo = ({ ...pageProps }) => (
<ApolloProvider client={client}>
<NewApolloProvider client={client}>
<PageComponent {...pageProps} />
</NewApolloProvider>
</ApolloProvider>
);

// Set the correct displayName in development
if (process.env.NODE_ENV !== "production") {
Expand Down
13 changes: 9 additions & 4 deletions staticUtils/shop/fetchPrimaryShop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import graphQLRequest from "staticUtils/graphQLRequest";
import primaryShopQuery from "./primaryShop.js";
import client from "../../lib/apollo/apolloClient";
import primaryShopQuery from "./primaryShop.gql";

/**
* Fetch the primary shop's information
Expand All @@ -8,7 +8,12 @@ import primaryShopQuery from "./primaryShop.js";
* @returns {Object} The primary shop
*/
export default async function fetchPrimaryShop(language) {
const data = await graphQLRequest(primaryShopQuery, { language });
const { data } = await client.query({
query: primaryShopQuery,
variables: {
language
}
});

return (data && data.primaryShop && { shop: data.primaryShop }) || { shop: null };
return data?.primaryShop ? { shop: data.primaryShop } : { shop: null };
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export default `
query primaryShop($language: String! = "en") {
primaryShop {
_id
Expand Down Expand Up @@ -45,4 +44,3 @@ fragment NavigationItemFields on NavigationItemData {
isUrlRelative
shouldOpenInNewWindow
}
`;