Here are my two files. I am trying to mimic the results of this sandbox with my own data: https://codesandbox.io/embed/stoic-haze-ispw2?codemirror=1
Essentially I can see the data was fetched and cache updated, but my component ResourceSection list of data isn't updated.
[UPDATE] Made some major changes based on feedback. Queries were removed from components and I made a skipLimitPagination function. The query works but my cache is not updating or placing the data inside.
import React from "react";
import { BrowserRouter as Router } from "react-router-dom";
import "./App.css";
import { ApolloClient, InMemoryCache, ApolloProvider } from "@apollo/client";
import Home from "./screens";
import { skipLimitPagination } from './utils/utilities'
const client = new ApolloClient({
uri: `https://graphql.contentful.com/content/v1/spaces/${process.env.REACT_APP_SPACE_ID}/?access_token=${process.env.REACT_APP_CDA_TOKEN}`,
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
resourceCollection: {items: skipLimitPagination()}
}
}
}
}),
});
function App() {
return (
<ApolloProvider client={client}>
<Router>
<Home />
</Router>
</ApolloProvider>
);
}
export default App;
import React, { useState } from "react";
import Navbar from "../components/Navbar";
import MobileNav from "../components/MobileNav";
import HeroSection from "../components/HeroSection";
import FeaturesSection from "../components/FeatureSection";
import Split from "../components/SplitWindow";
import Loading from "../components/Loading";
import { useQuery, gql } from "@apollo/client";
import Resource from "../components/ResourceSection";
import Contact from "../components/ContactSection";
import Footer from "../components/Footer";
const MASS_COLLECTION = gql`
query($skip: Int) {
resourceCollection(limit: 5, skip: $skip ) {
items {
type
category
title
link
bgColor
color
}
},
splitSectionCollection(order: splitId_ASC) {
items {
splitId
lightBg
left
lightText
darkText
image {
url
}
alt
heading
content {
json
}
}
}
}
`;
const Home = () => {
const [isOpen, setIsOpen] = useState(false);
const { loading, error, data, fetchMore } = useQuery(MASS_COLLECTION, {
variables: {
skip: 0,
},
});
if (loading) return <Loading />;
if (error) return <p>Error</p>;
const toggle = () => {
setIsOpen(!isOpen);
};
return (
<>
<MobileNav isOpen={isOpen} toggle={toggle} />
<Navbar toggle={toggle} />
<HeroSection />
<FeaturesSection />
{data.splitSectionCollection.items.map((item) => {
return <Split item={item} key={item.splitId} />;
})}
<Resource data={data.resourceCollection.items} fetchMore={fetchMore}/>
<Contact />
<Footer />
</>
);
};
export default Home;
import React, { useState, useCallback } from "react";
import {
ResourceContainer,
ResourcesWrapper,
ResourceRow,
TextWrapper,
Column1,
Heading,
Content,
Column2,
ImgWrap,
Img,
Form,
FormSelect,
FormOption,
// LinkContainer,
// LinkWrapper,
// LinkIcon,
// LinkTitle,
// LoadMore,
// ButtonWrapper,
} from "./ResourceElements";
const ResourceSection = ({ data, fetchMore }) => {
console.log(data)
const handleClick = useCallback(() => {
fetchMore({
variables: {
skip:
data
? data.length
: 0,
},
});
}, [fetchMore, data]);
return (
<ResourceContainer lightBg={true} id="resource">
<ResourcesWrapper>
<ResourceRow left={true}>
<Column1>
<TextWrapper>
<Heading lightText={false}>Resources</Heading>
<Content darkText={true} className="split_cms">
Cyber Streets strives in sharing education resources to all.
Below you can find an exhaustive list of resources covering
everything from computer programming to enterneurship. "Be
knowledgeable in your niche, provide some information free of
charge, and share other trustworthy people's free resources
whenever possible..." - Heather Hart
</Content>
</TextWrapper>
</Column1>
<Column2>
<ImgWrap>
<Img
src="/assets/images/Resource.svg"
alt="Two looking at computer screen svg"
/>
</ImgWrap>
</Column2>
</ResourceRow>
<Form action="">
<FormSelect
// onChange={(e) => {
// setCategory(e.target.value);
// // setLimit(5);
// }}
>
<FormOption value="">Filter by category</FormOption>
<FormOption value="MEDIA">Media</FormOption>
<FormOption value="TEDX">Ted Talks</FormOption>
<FormOption value="INTERNET SAFETY/AWARENESS">
Internet safety & awareness
</FormOption>
<FormOption value="K-12/COMPUTER SCIENCE">
k-12 & computer science
</FormOption>
<FormOption value="CODING">Programming</FormOption>
<FormOption value="CYBER/IT OPERATIONS">
Cyber ∧ IT operations
</FormOption>
<FormOption value="ROBOTICS">Robotics</FormOption>
<FormOption value="CLOUD">Cloud</FormOption>
<FormOption value="SCIENCE">Science</FormOption>
<FormOption value="PROFESSIONAL DEVELOPMENT">
Professional Development
</FormOption>
<FormOption value="3D PRINTING">3D Printing</FormOption>
<FormOption value="ART">Art</FormOption>
<FormOption value="MOOC">Massive Open Online Courses</FormOption>
<FormOption value="GAMES">Games & Challenges</FormOption>
<FormOption value="OTHER">Other</FormOption>
</FormSelect>
</Form>
<div className="list">
{data.map((resource, i) => (
<div key={resource.title} className="item">
{resource.title}
</div>
))}
</div>
<button className="button" onClick={handleClick}>
Fetch!
</button>
</ResourcesWrapper>
</ResourceContainer>
);
};
export default ResourceSection;
My cache after clicking the fetch more button. Two separate resource collections, should this be combined? I got this information through apollo chrome plugin.
I am using the contenful graphql API:
Here is my resource collection args and fields:
ResourceCollection
ARGS
skip: Int = 0
limit: Int = 100
preview: Boolean
locale: String
where: ResourceFilter
order: [ResourceOrder]
Fields
total: Int!
skip: Int!
limit: Int!
items: [Resource]!
export function skipLimitPagination(keyArgs) {
return {
keyArgs,
merge(existing, incoming, { args }) {
const merged = existing ? existing.slice(0) : [];
if (args) {
const { skip = 0 } = args;
for (let i = 0; i < incoming.length; ++i) {
merged[skip + i] = incoming[i];
}
} else {
merged.push.apply(merged, incoming);
}
return merged;
},
};
}
I've been working on this issue for three days straight. I tried the older way with update query but it wasn't working as intended so now I am trying to the most update apollo technique. Please help :(