So I've run into an issue with Apollo where I'm trying to dynamically add/update/remove to a list of topics in my app, however my pollInterval in my Query keeps making network requests over and over, even after I have the data in my Apollo cache. I know there are ways to manually trigger a refetch, but that after playing around with it, it's a lot of extra steps to get it the way I want. Is there something I'm missing here?
Here is the code for the component:
import React from 'react';
import { gql } from 'apollo-boost';
import { Query, Mutation } from 'react-apollo';
import TopicForm from "./TopicForm";
import { Link, withRouter} from "react-router-dom";
const REMOVE_TOPIC = gql`
mutation REMOVE_TOPIC(
$id: String!
) {
removeTopic(id: $id) {
id
}
}
`
const GET_TOPICS = gql`
{
topics {
id
name
}
}
`;
class Topics extends React.Component {
removeTopic(id, removeTopicById) {
removeTopicById({
variables: {
id
}
}).then(result => {
// Redirect to main topics page
this.props.history.push('/topics');
})
}
componentDidMount() {
}
render() {
const RemoveButton = props => {
return <Mutation mutation={REMOVE_TOPIC}>
{(removeTopicById, { loading }) => {
return <button type="button" onClick={(e) => {
e.preventDefault();
this.removeTopic(props.id, removeTopicById);
}}>X</button>
}}
</Mutation>
}
const TopicList = () => {
return (<Query query={GET_TOPICS} pollInterval={40}>
{(({ data: { topics }, loading }) => {
if (loading || !topics) {
return <div>Loading ...</div>;
}
return topics.map(({ id, name }) => {
return <div><Link to={`/topics/${id}`}>{name}<RemoveButton id={id} /></Link></div>
})
})
}
</Query>)
}
return (<div>
{this.props.match.params.topicId ? <h2>Update Topic</h2> : <h2>Add Topic</h2>}
<TopicForm topicId={this.props.match.params.topicId}/>
<TopicList />
</div>)
}
}
export default withRouter(Topics)
The main part I'm talking about is inside the TopicList function
pollInterval is doing what it is meant to
refetchQueries isn't too complex. In this case I think it would be:
<Mutation
mutation={REMOVE_TOPIC}
refetchQueries={[{ query: GET_TOPICS }]}
With hooks you can do the following:
Use the startPolling and stopPolling dependencies like this for example:
useEffect(() => {
startPolling(10000);
return () => {
stopPolling();
};
}, [startPolling, stopPolling]);
This will refetch every 10 seconds and stopPolling when component unmount.
Related
import { GraphQLClient, gql } from "graphql-request";
import { RichText } from "#graphcms/rich-text-react-renderer";
import { useEffect } from "react";
import Prism from "prismjs";
import "prismjs/plugins/line-numbers/prism-line-numbers";
import "prismjs/themes/prism-tomorrow.css";
import "prismjs/plugins/line-numbers/prism-line-numbers.css";
export default function Demo({ posts }) {
useEffect(() => {
Prism.highlightAll();
}, []);
return (
<section className="prose m-auto">
<h1>{posts.title}</h1>
<RichText
content={posts.content.json}
renderers={{
code_block: ({ children }) => {
return (
<pre className="line-numbers language-none">
<code>{children}</code>
</pre>
);
},
}}
/>
</section>
);
}
export const getServerSideProps = async (context) => {
const endPoint =
"https://randomenpoint.com/api";
const slug = "random slug";
const query = gql`
query ($slug: String!) {
posts(where: { slug: $slug }) {
id
publishedAt
createdAt
slug
title
updatedAt
content {
json
}
}
}
`;
const client = new GraphQLClient(endPoint);
const { posts } = await client.request(query, { slug });
return {
props: {
posts,
},
};
};
On my above code, when I'm adding the line numbers className inside pre tag I'm getting hydration error, what's wrong here? But without extra className inside pre tag, my code works fine. But it returns a normal code block in the browser. I want to use prismjs for styling. I'm using Nextjs,GraphCMS and tailwindCSS
I have a code which access data from GraphQL API in an arrow function:
const LinkList = () => {
const { loading, error, data } = useQuery(CURRENCIES);
if (loading) return <Loader/>;
if (error) return <pre>{error.message}</pre>
return (
<div className="options">
{data.currencies.map((currency) => {
return (
<button
key={currency}
id={currency}
className="option"
>
{currency.symbol}
{currency.label}
</button>
);
})}
</div>
);
};
But I really need to implement this piece of code with access to it in a class component. I was searching a documentation with accessing data in a classes, but nothing.
Any ideas?
You can use #apollo/client package and we can use client.query directly in the class component
import {
ApolloClient,
gql,
InMemoryCache,
NormalizedCacheObject
} from '#apollo/client';
const client = new ApolloClient<NormalizedCacheObject> ({
cache: new InMemoryCache({}),
uri: 'https://countries.trevorblades.com'
});
import * as React from 'react';
const GET_Countries = gql`
query {
countries{
code
name
}
}
`;
class App extends React.Component {
getData = async () => {
let res = await client.query({
query: GET_Countries
})
console.log(res)
// Set the state to make changes in UI
}
componentDidMount() {
this.getData()
}
render() {
return "Test";
}
}
export default App;
I am getting loading state only and data as undefined in testing. I don't know why I am following everything in the given example. Please help.
Testing file. When i am waiting thsi line toexecute await wait(() => getByTestId('edit-category'));. It is giving response data of query as undefined.
Error: TypeError: Cannot read property 'getCategory' of undefined
Line 34 on editConatinerCategory.tsx => category={data!.getCategory!}
import React from 'react';
import gql from 'graphql-tag';
import { cleanup, wait } from 'react-testing-library';
import { customRender } from '../../../test-utils/customRender';
import { EditCategoryContainer } from './Container';
afterEach(() => {
cleanup();
console.error;
});
console.error = jest.fn();
const getCategoryMock = {
request: {
query: gql`
query getCategory($id: Int!) {
getCategory(id: $id) {
id
name
active
position
}
}
`,
variables: {
id: 1
}
},
result: {
data: {
getCategory: {
id: 1,
name: 'category',
active: true,
position: 1
}
}
}
};
describe('create edit category module', () => {
test('Rendering correct', async () => {
const { container, debug, getByTestId } = customRender(<EditCategoryContainer />, [
getCategoryMock
]);
await wait(() => getByTestId('edit-category'));
await wait(() => expect(container).toMatchSnapshot());
//Getting this TypeError: Cannot read property 'getCategory' of undefined. Because i am data as undefined from my query response
});
});
CustomRender.tsx
import React from 'react';
import { render } from 'react-testing-library';
import { MockedProvider, MockedResponse } from 'react-apollo/test-utils';
import { Router, Switch } from 'react-router-dom';
import { createMemoryHistory } from 'history';
export const customRender = (
node: JSX.Element | null,
mocks?: MockedResponse[],
{
route = '/',
history = createMemoryHistory({ initialEntries: [route] })
} = {}
) => {
return {
history,
...render(
<MockedProvider mocks={mocks} addTypename={false}>
<Router history={history}>
<Switch>{node}</Switch>
</Router>
</MockedProvider>
)
};
};
EditCategoryContainer.tsx
import React from 'react';
import { withRouter } from 'react-router';
import { Spin } from 'antd';
import {
AddCategoryComponent,
GetCategoryComponent
} from '../../../generated/graphql';
import { EditCategory } from './Edit';
import { LoadingComponent } from '../../../components/LoadingComponent';
export const EditCategoryContainer = withRouter(({ history, match }) => {
const id: number = parseInt(match.params.id, 10);
return (
<GetCategoryComponent
variables={{
id
}}
>
{({ data, loading: getCategoryLoading }) => {
console.log(getCategoryLoading, 'getCategoryLoading');
if (getCategoryLoading) {
return <LoadingComponent />;
}
if (data && !data.getCategory) {
return <div>Category not found!</div>;
}
console.log(data);
return (
<AddCategoryComponent>
{(addCategory, { loading, error }) => {
return (
<EditCategory
data-testid="edit-category"
category={data!.getCategory!}
loading={loading || getCategoryLoading}
onSubmit={values => {
addCategory({ variables: values }).then(() => {
history.push('/dashboard/categories');
});
}}
/>
);
}}
</AddCategoryComponent>
);
}}
</GetCategoryComponent>
);
});
Edit:
I tried #mikaelrs solution which is passed match. But it is not working. I also tried to pass id:1 as fixed. But it is still giving error.
<GetCategoryComponent
variables={{
id:1
}}
>
...rest of code.
</GetCategoryComponent>
This is not working. My query without veriable is working fine. Mutation is also working fine. I am having only problem with this. When i have to pass like varible like this.
What I do to wait for the loading state of the MockedProvider to pass is to use the wait function from waait. This is actually what Apollo recommends as well.
So in your test you would do:
import React from 'react';
import gql from 'graphql-tag';
import { cleanup } from 'react-testing-library';
import wait from 'waait'
import { customRender } from '../../../test-utils/customRender';
import { EditCategoryContainer } from './Container';
afterEach(() => {
cleanup();
});
const getCategoryMock = {
request: {
query: gql`
query getCategory($id: Int!) {
getCategory(id: $id) {
id
name
active
position
}
}
`,
variables: {
id: 1
}
},
result: {
data: {
getCategory: {
id: 1,
name: 'category',
active: true,
position: 1
}
}
}
};
describe('create edit category module', () => {
test('Rendering correct', async () => {
const { container, debug } = customRender(<EditCategoryContainer />, [
getCategoryMock
]);
await wait(0);
// Your loading state should be false after this, and your component should
// get it's data from apollo for you to do any assertion you would like to
// after this point. To see that the component is rendered with data invoke
// the debug function from react-testing-library after this point
debug();
expect(container).toMatchSnapshot()
});
});
Another solution is to use react-testing-librarys wait function to wait for an element that would be present after the loading state switches to true.
For instance
describe('create edit category module', () => {
test('Rendering correct', async () => {
const { container, debug, queryByText } = customRender(<EditCategoryContainer />, [
getCategoryMock
]);
await wait(()=> queryByText("Some Data"));
// Your loading state should be false after this, and your component should
// get it's data from apollo for you to do any assertion you would like to
// after this point
expect(container).toMatchSnapshot()
});
});
I faced a similar issue. Here is how I resolved my issue.
First, wait for the query to resolve, as recommended by #mikaelrs and the docs:
await new Promise(resolve => setTimeout(resolve, 0));
After doing that, the loading property was false, but data was still undefined. I discovered that my mock result object was missing a property. Once I added that missing property to the mock result, the data was populated as expected.
I have been reading the Apollo documentation and I can't find any examples on how to refetch after a mutation with the client props that is being passed by withApollo HOC.
My component:
import React, {Fragment} from 'react';
import gql from 'graphql-tag';
import { withApollo } from 'react-apollo';
...
const getPosts = gql`
{
posts {
_id
title
description
user {
_id
}
}
}`;
const deletePost = gql`
mutation deletePost($_id: String){
deletePost(_id: $_id)
}
`;
class PostList extends React.Component {
static propTypes = {
match: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
};
state = {posts: null};
componentDidMount() {
this.props.client.query({
query: getPosts,
}).then(({ data }) => {
this.setState({ posts: data.posts });
});
}
deletePost = postId => {
this.props.client
.mutate({
mutation: deletePost,
variables: {
_id: postId
},
})
.then(({ data }) => {
alert('Post deleted!');
});
};
render() {
const {posts} = this.state;
if (!posts) {
return <div>Loading....</div>
}
return (
<div className="post">
...stuff...
</div>
)
}
}
export default withApollo(PostList);
I want to refetch the posts each time one is deleted.
The posts data that you render is in your component state, but you only query posts in componentDidMount. This method is called only when your component is first rendered.
class PostList extends React.Component {
fetchPosts() {
this.props.client.query({
query: getPosts,
}).then(({ data }) => {
this.setState({ posts: data.posts });
});
}
componentDidMount() {
this.fetchPosts();
}
deletePost = postId => {
this.props.client
.mutate({
mutation: deletePost,
variables: {
_id: postId
},
})
.then(({ data }) => {
this.fetchPosts()
});
};
render() {
const {posts} = this.state;
if (!posts) {
return <div>Loading....</div>
}
return (
<div className="post">
...stuff...
</div>
)
}
}
In fact, you do not even need to have a local state, you can rely on Apollo client local cache and use the Query component as well as the Mutation component.
In my Next.js component I made a mutation request to GraphQL server and after it successfully done I need to redirect to another page. How I do it now:
import React, { Component } from 'react';
import Router from 'next/router';
import { Mutation } from 'react-apollo';
import { gql } from 'apollo-boost';
const signInMutation = gql`
mutation signIn($accessToken: String!) {
signIn(accessToken: $accessToken)
}
`;
export default class extends Component {
static async getInitialProps({ query: { accessToken } }) {
return { accessToken };
}
render() {
const { accessToken } = this.props;
return (
<Mutation mutation={signInMutation} ignoreResults>
{signIn => {
signIn({ variables: { accessToken } }).then(() => {
Router.push({
pathname: '/user'
});
});
return null;
}}
</Mutation>
);
}
}
It works fine but Next.js throws an error: You should only use "next/router" inside the client side of your app.. So, what is the best way to fix the error?
Your signIn mutation is executed on render, and when NextJS renders your app on the server side, executes your mutation.
You should render a button and only trigger the mutation on click:
import React, { Component } from 'react';
import Router from 'next/router';
import { Mutation } from 'react-apollo';
import { gql } from 'apollo-boost';
const signInMutation = gql`
mutation signIn($accessToken: String!) {
signIn(accessToken: $accessToken)
}
`;
export default class extends Component {
static async getInitialProps({ query: { accessToken } }) {
return { accessToken };
}
render() {
const { accessToken } = this.props;
return (
<Mutation mutation={signInMutation} ignoreResults>
{signIn => {
return (
<button onClick={async () => {
await signIn({ variables: { accessToken } })
Router.push({ pathname: '/user' })
}}>Login</button>
)
}}
</Mutation>
);
}
}