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.
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';