项目作者: BlackWaspTech

项目描述 :
Integration between redux-wasp and apollo-link-state.
高级语言: JavaScript
项目地址: git://github.com/BlackWaspTech/redux-wasp-apollo.git
创建时间: 2018-09-26T15:57:35Z
项目社区:https://github.com/BlackWaspTech/redux-wasp-apollo

开源协议:MIT License

下载


redux-wasp-apollo

code style: prettier

All Contributors

v0.2.0 (beta)


Provides integration between redux-wasp and apollo-link-state.

For the base query, mutation, and subscription, check out wasp-graphql.

For additional features unique to Redux, check out redux-wasp.

For a live, full-stack application showcasing redux-wasp in action, go here.

Purpose

Updating to Apollo 2.0+ and don’t want to completely remove redux store/state? Just use ReduxWaspApollo to bridge the gap between the two! Pass in the new data you need to update Apollo’s Cache with and let ReduxWaspApollo do the rest.

Usage

Pass your Apollo Client down as a prop. Pass props into the ReduxWaspApollo functions.

Example

index.js

  1. import React from 'react';
  2. import { Provider } from 'react-redux';
  3. import store from './store';
  4. import ReactDOM from 'react-dom';
  5. import App from './components/app';
  6. import { ApolloProvider } from 'react-apollo';
  7. import { ApolloClient } from 'apollo-client';
  8. import { HttpLink } from 'apollo-link-http';
  9. import { InMemoryCache } from 'apollo-cache-inmemory';
  10. const client = new ApolloClient({
  11. link: new HttpLink({ uri: 'http://localhost:3000/graphql' }),
  12. cache: new InMemoryCache()
  13. });
  14. ReactDOM.render(
  15. <ApolloProvider client={client}>
  16. <Provider store={store}>
  17. <App client={client} ></App>
  18. </Provider>
  19. </ApolloProvider>,
  20. document.getElementById('root')
  21. );

app.js

  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import * as queries from './queries/queries';
  4. // Wasp Links
  5. import WaspLinkMutate from 'WaspReduxApolloLinkMutate';
  6. import WaspLinkQuery from 'WaspReduxApolloQuery';
  7. // Wasp Fetch
  8. import WaspFetch from 'WaspFetch';
  9. let API = 'http://localhost:3000/graphql';
  10. const mapStateToProps = state => {
  11. return {
  12. user: state
  13. };
  14. };
  15. class App extends Component {
  16. constructor(props) {
  17. super(props);
  18. }
  19. getUsers(e, props) {
  20. e.preventDefault();
  21. // Wasp Query usage
  22. // Passing in props with Client passed down, as well as, the query to be used
  23. WaspLinkQuery(props, queries.getAllUsers);
  24. // Get information with WaspFetch
  25. WaspFetch(API, queries.getAllUsersWaspFetch);
  26. }
  27. render() {
  28. return (
  29. <div>
  30. <button onClick={e => this.getUsers(e, this.props)}>Get Users</button>
  31. </div>
  32. );
  33. }
  34. }
  35. export default connect(mapStateToProps)(App);

queries.js

  1. import gql from 'graphql-tag';
  2. let addUser = gql`
  3. mutation addUser($name: String!, $pass: String!) {
  4. addUser(name: $name, password: $pass) {
  5. userName
  6. userPass
  7. }
  8. }
  9. `;
  10. let getUser = gql`
  11. query($id: ID!) {
  12. user(id: $id) {
  13. userName
  14. }
  15. }
  16. `;
  17. let getAllUsers = gql`
  18. {
  19. getUsers {
  20. userName
  21. }
  22. }
  23. `;
  24. export { addUser, getUser, getAllUsers };

Contributors

Thanks goes to these wonderful people (emoji key):


marceca


Reynolds A Colon


Denny Temple


kamo31

This project follows the all-contributors specification. Contributions of any kind welcome!

Code of Conduct

Read our Code of Conduct here.

License

Open Sourced under the MIT License.