Mar*_*ell 2 apollo graphql apollo-client
这是我的两个文件。我试图用我自己的数据模拟这个沙箱的结果:https : //codesandbox.io/embed/stoic-haze-ispw2?codemirror=1
基本上我可以看到数据被获取并更新了缓存,但我的组件 ResourceSection 数据列表没有更新。
[更新] 根据反馈进行了一些重大更改。从组件中删除了查询,我创建了一个 skipLimitPagination 函数。查询有效,但我的缓存没有更新或将数据放入其中。
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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
单击获取更多按钮后我的缓存。两个独立的资源集合,应该合并吗?我通过 apollo chrome 插件获得了这些信息。
我正在使用 contenful graphql API:
这是我的资源集合参数和字段:
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]!
Run Code Online (Sandbox Code Playgroud)
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;
},
};
}
Run Code Online (Sandbox Code Playgroud)
我已经连续三天研究这个问题了。我尝试了更新查询的旧方法,但它没有按预期工作,所以现在我正在尝试最新的阿波罗技术。请帮忙 :(
我遇到了几乎相同的问题并找到了解决方案。问题在于 Apollo 站点上的所有示例都假定响应对象的第一个元素是您的项目数组。
这不是 Contentful 的工作方式,数组总是嵌套在items集合中。例如,您resourceCollection有一个包含所有资源的属性项目。所以你必须合并items但返回整个resourceCollection,它看起来像这样:
new InMemoryCache({
typePolicies: {
Query: {
fields: {
resourceCollection: {
keyArgs: false,
merge(existing, incoming) {
if (!incoming) return existing
if (!existing) return incoming // existing will be empty the first time
const { items, ...rest } = incoming;
let result = rest;
result.items = [...existing.items, ...items]; // Merge existing items with the items from incoming
return result
}
}
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
这将返回resourceCollectionwith 合并items。
| 归档时间: |
|
| 查看次数: |
2339 次 |
| 最近记录: |