├── .eslintrc ├── public ├── favicon.ico ├── manifest.json └── index.html ├── src ├── index.css ├── resolvers.js ├── App.test.js ├── App.css ├── index.js ├── apollo.js ├── App.js ├── logo.svg └── registerServiceWorker.js ├── README.md ├── .gitignore ├── package.json └── LICENSE /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "react-app" 3 | } -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pantharshit00/apolloStateBookExample/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, 5 | Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; 6 | } 7 | -------------------------------------------------------------------------------- /src/resolvers.js: -------------------------------------------------------------------------------- 1 | export default { 2 | Mutation: { 3 | changeShowType: (_, { show_type }, { cache }) => { 4 | cache.writeData({ data: { show_type } }); 5 | return { show_type, __typename: 'ShowType' }; 6 | } 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | button { 2 | padding: 0.5rem; 3 | cursor: pointer; 4 | background: rebeccapurple; 5 | border: none; 6 | outline: none; 7 | border-radius: 0.6rem; 8 | font-size: 1.4rem; 9 | margin: 0 1rem 0 0; 10 | color: white; 11 | } 12 | 13 | #root { 14 | margin: 1rem; 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | Clone graphql server first https://github.com/pantharshit00/grapqhql-api 4 | 5 | then 6 | ```bash 7 | > git clone https://github.com/pantharshit00/apolloStateBookExample.git 8 | > cd apolloStateBookExample 9 | > npm i # or yarn users 10 | > # yarn 11 | ``` 12 | 13 | ### Have any questions or found a bug or made an improvement. Open an Issue 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { ApolloProvider } from 'react-apollo'; 4 | import client from './apollo'; 5 | import './index.css'; 6 | import App from './App'; 7 | import registerServiceWorker from './registerServiceWorker'; 8 | 9 | ReactDOM.render( 10 | 11 | 12 | , 13 | document.getElementById('root') 14 | ); 15 | registerServiceWorker(); 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bookstore-apollo", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "apollo-client-preset": "^1.0.5", 7 | "apollo-link-state": "^0.3.0", 8 | "graphql": "^0.12.3", 9 | "graphql-tag": "^2.6.1", 10 | "react": "^16.2.0", 11 | "react-apollo": "^2.0.4", 12 | "react-dom": "^16.2.0", 13 | "react-scripts": "1.0.17" 14 | }, 15 | "scripts": { 16 | "start": "react-scripts start", 17 | "build": "react-scripts build", 18 | "test": "react-scripts test --env=jsdom", 19 | "eject": "react-scripts eject" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/apollo.js: -------------------------------------------------------------------------------- 1 | import { ApolloClient } from 'apollo-client'; 2 | import { InMemoryCache } from 'apollo-cache-inmemory'; 3 | import { withClientState } from 'apollo-link-state'; 4 | import { HttpLink } from 'apollo-link-http'; 5 | import { ApolloLink } from 'apollo-link'; 6 | import resolvers from './resolvers'; 7 | 8 | const httpLink = new HttpLink({ 9 | uri: 'http://localhost:8080/graphql' 10 | }); 11 | 12 | const cache = new InMemoryCache(); 13 | 14 | const stateLink = withClientState({ 15 | cache, 16 | resolvers, 17 | defaults: { 18 | show_type: 'BELOW_15' 19 | } 20 | }); 21 | 22 | const link = ApolloLink.from([stateLink, httpLink]); 23 | 24 | const client = new ApolloClient({ 25 | link, 26 | cache, 27 | connectToDevTools: true 28 | }); 29 | 30 | export default client; 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Harshit Pant 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import gql from 'graphql-tag'; 3 | import { graphql, compose } from 'react-apollo'; 4 | import './App.css'; 5 | 6 | class App extends Component { 7 | render() { 8 | const { showTypeQuery, allBooksQuery } = this.props; 9 | if (showTypeQuery.loading || allBooksQuery.loading) { 10 | return

Loading....

; 11 | } 12 | const books = allBooksQuery.getAllBooks.filter(item => { 13 | if (showTypeQuery.show_type === 'BELOW_15') { 14 | return item.price < 15; 15 | } 16 | return item.price >= 15; 17 | }); 18 | return ( 19 |
20 |

Bookstore

21 | {books.map(item => ( 22 |

23 | {item.title} - ${item.price} 24 |

25 | ))} 26 | 27 | 28 |
29 | ); 30 | } 31 | changeShow = type => { 32 | this.props.mutate({ 33 | variables: { show_type: type } 34 | }); 35 | }; 36 | } 37 | 38 | const allBooksQuery = gql` 39 | query allBooksQuery { 40 | getAllBooks { 41 | _id 42 | author 43 | title 44 | price 45 | } 46 | } 47 | `; 48 | 49 | const showTypeQuery = gql` 50 | query showTypeQuery { 51 | show_type @client 52 | } 53 | `; 54 | 55 | const showTypeMutation = gql` 56 | mutation showTypeMutation($show_type: String!) { 57 | changeShowType(show_type: $show_type) @client { 58 | show_type 59 | } 60 | } 61 | `; 62 | 63 | export default compose( 64 | graphql(showTypeQuery, { name: 'showTypeQuery' }), 65 | graphql(allBooksQuery, { name: 'allBooksQuery' }), 66 | graphql(showTypeMutation) 67 | )(App); 68 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | } else { 39 | // Is not local host. Just register service worker 40 | registerValidSW(swUrl); 41 | } 42 | }); 43 | } 44 | } 45 | 46 | function registerValidSW(swUrl) { 47 | navigator.serviceWorker 48 | .register(swUrl) 49 | .then(registration => { 50 | registration.onupdatefound = () => { 51 | const installingWorker = registration.installing; 52 | installingWorker.onstatechange = () => { 53 | if (installingWorker.state === 'installed') { 54 | if (navigator.serviceWorker.controller) { 55 | // At this point, the old content will have been purged and 56 | // the fresh content will have been added to the cache. 57 | // It's the perfect time to display a "New content is 58 | // available; please refresh." message in your web app. 59 | console.log('New content is available; please refresh.'); 60 | } else { 61 | // At this point, everything has been precached. 62 | // It's the perfect time to display a 63 | // "Content is cached for offline use." message. 64 | console.log('Content is cached for offline use.'); 65 | } 66 | } 67 | }; 68 | }; 69 | }) 70 | .catch(error => { 71 | console.error('Error during service worker registration:', error); 72 | }); 73 | } 74 | 75 | function checkValidServiceWorker(swUrl) { 76 | // Check if the service worker can be found. If it can't reload the page. 77 | fetch(swUrl) 78 | .then(response => { 79 | // Ensure service worker exists, and that we really are getting a JS file. 80 | if ( 81 | response.status === 404 || 82 | response.headers.get('content-type').indexOf('javascript') === -1 83 | ) { 84 | // No service worker found. Probably a different app. Reload the page. 85 | navigator.serviceWorker.ready.then(registration => { 86 | registration.unregister().then(() => { 87 | window.location.reload(); 88 | }); 89 | }); 90 | } else { 91 | // Service worker found. Proceed as normal. 92 | registerValidSW(swUrl); 93 | } 94 | }) 95 | .catch(() => { 96 | console.log( 97 | 'No internet connection found. App is running in offline mode.' 98 | ); 99 | }); 100 | } 101 | 102 | export function unregister() { 103 | if ('serviceWorker' in navigator) { 104 | navigator.serviceWorker.ready.then(registration => { 105 | registration.unregister(); 106 | }); 107 | } 108 | } 109 | --------------------------------------------------------------------------------