I'm following a graphql tutorial on youtube (https://www.youtube.com/watch?v=ed8SzALpx1Q at about 3hr 16min) and part of it uses compose
from "react-apollo". However, I'm getting an error because the new version of react-apollo does not export this.
I read online that I need to replace import { compose } from "react-apollo"
with import { compose } from "recompose"
but doing that produces the error TypeError: Cannot read property 'loading' of undefined
I've also read that I should replace the import from react-apollo with import * as compose from "lodash"
but when I do this I get other errors, saying that × TypeError: lodash__WEBPACK_IMPORTED_MODULE_2__(...) is not a function
App.js:
import React from "react";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";
import BookList from "./components/BookList";
import AddBook from "./components/AddBook";
//apollo client setup
const client = new ApolloClient({
uri: "http://localhost:4000/graphql"
});
function App() {
return (
<ApolloProvider client={client}>
<div className="main">
<h1>My Reading List</h1>
<BookList />
<AddBook />
</div>
</ApolloProvider>
);
}
export default App;
queries.js:
import { gql } from "apollo-boost";
const getBooksQuery = gql`
{
books {
name
id
}
}
`;
const getAuthorsQuery = gql`
{
authors {
name
id
}
}
`;
const addBookMutation = gql`
mutation {
addBook(name: "", genre: "", authorId: "") {
name
id
}
}
`;
export { getAuthorsQuery, getBooksQuery, addBookMutation };
AddBooks.js:
import React, { Component } from "react";
import { graphql } from "react-apollo";
import { compose } from "recompose";
// import * as compose from "lodash";
import { getAuthorsQuery, addBookMutation } from "../queries/queries";
class AddBook extends Component {
state = {
name: "",
genre: "",
authorId: ""
};
displayAuthors = () => {
let data = this.props.data;
if (data.loading) {
return <option>loading authors...</option>;
} else {
return data.authors.map(author => {
return (
<option key={author.id} value={author.id}>
{author.name}
</option>
);
});
}
};
submitForm(e) {
e.preventDefault();
console.log(this.state);
}
render() {
return (
<form onSubmit={this.submitForm.bind(this)}>
<div className="field">
<label>Book name: </label>
<input
type="text"
onChange={e => {
this.setState({ name: e.target.value });
}}
/>
</div>
<div className="field">
<label>Genre: </label>
<input
type="text"
onChange={e => {
this.setState({ genre: e.target.value });
}}
/>
</div>
<div className="field">
<label>Author: </label>
<select
onChange={e => {
this.setState({ authorId: e.target.value });
}}
>
<option>Select author</option>
{this.displayAuthors()}
</select>
</div>
<button>+</button>
</form>
);
}
}
export default compose(
graphql(getAuthorsQuery, { name: "getAuthorsQuery" }),
graphql(addBookMutation, { name: "addBookMutation" })
)(AddBook);
I expected compose to be imported from react-apollo and to take the query and mutation and make them available inside of AddBook's props, so I can use them in the displayAuthors() and submitForm() funtions, but instead I get the error that it is not exported from react-apollo, and when I try the suggested solutions I found online I get the other errors mentioned above.
Deprecated React Apollo render prop component API NOTE: React Apollo's render prop components are deprecated.
The useQuery React hook is the primary API for executing queries in an Apollo application. To run a query within a React component, call useQuery and pass it a GraphQL query string.
While you don't need React or another front-end framework just to fetch data with Apollo Client, our view layer integrations make it easier to bind your queries to your UI and reactively update your components with data.
You can use Apollo with React Native exactly as you would with React Web. If you are new to using Apollo with React, you should probably read the React guide.
compose
was removed from React Apollo 3.0.0. If you want to use the same HOC pattern, feel free to use the same copy of lodash's flowRight
.
Install lodash in your client folder
npm install lodash
and use this to import compose from lodash (use a capital R in flowRight)
import {flowRight as compose} from 'lodash';
Reference for the braking changes
compose
was removed from React Apollo 3 (see the Breaking Changes).
Now, to use compose, use lodash's flowRight.
Install flowright:
yarn add lodash.flowright
Replace in your code:
import { compose } from 'react-apollo'
by
import {flowRight as compose} from 'lodash';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With