├── .env ├── .github └── FUNDING.yml ├── .gitignore ├── .prettierrc ├── .travis.yml ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json └── src ├── App.css ├── App.js ├── App.test.js ├── index.css ├── index.js └── serviceWorker.js /.env: -------------------------------------------------------------------------------- 1 | REACT_APP_GITHUB_PERSONAL_ACCESS_TOKEN=xxxxXXXXX -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: rwieruch 4 | patreon: # rwieruch 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with a single custom sponsorship URL 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-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 15 | .env.local 16 | .env.development.local 17 | .env.test.local 18 | .env.production.local 19 | 20 | npm-debug.log* 21 | yarn-debug.log* 22 | yarn-error.log* 23 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "trailingComma": "all", 4 | "singleQuote": true, 5 | "printWidth": 70, 6 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - stable 5 | 6 | install: 7 | - npm install 8 | 9 | script: 10 | - npm test 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-graphql-github-vanilla 2 | 3 | [![Build Status](https://travis-ci.org/the-road-to-graphql/react-graphql-github-vanilla.svg?branch=master)](https://travis-ci.org/the-road-to-graphql/react-graphql-github-vanilla) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://slack-the-road-to-learn-react.wieruch.com/) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-graphql/react-graphql-github-vanilla.svg)](https://greenkeeper.io/) 4 | 5 | A simple React application consuming the GitHub GraphQL API with plain HTTP requests. [Read more about it over here](https://www.robinwieruch.de/react-with-graphql-tutorial). 6 | 7 | ## Features 8 | 9 | * React 16 with create-react-app 10 | * GitHub GraphQL API 11 | * Consuming GraphQL with plain JS 12 | * no Apollo/Relay 13 | * [are you curious about Apollo though?](https://github.com/rwieruch/react-graphql-github-apollo) 14 | 15 | ## Installation 16 | 17 | * `git clone git@github.com:the-road-to-graphql/react-graphql-github-vanilla.git` 18 | * cd react-graphql-github-vanilla 19 | * npm install 20 | * [add your own REACT_APP_GITHUB_PERSONAL_ACCESS_TOKEN in .env file](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) 21 | * scopes/permissions you need to check: admin:org, repo, user, notifications 22 | * npm start 23 | * visit `http://localhost:3000` 24 | 25 | ## Want to learn more about React + GraphQL + Apollo? 26 | 27 | * Don't miss [upcoming Tutorials and Courses](https://www.getrevue.co/profile/rwieruch) 28 | * Check out current [React Courses](https://roadtoreact.com) 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-graphql-github-vanilla", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "axios": "^0.18.0", 7 | "react": "^16.6.0", 8 | "react-dom": "^16.6.0", 9 | "react-scripts": "3.1.0" 10 | }, 11 | "scripts": { 12 | "start": "react-scripts start", 13 | "build": "react-scripts build", 14 | "test": "react-scripts test", 15 | "eject": "react-scripts eject" 16 | }, 17 | "eslintConfig": { 18 | "extends": "react-app" 19 | }, 20 | "browserslist": [ 21 | ">0.2%", 22 | "not dead", 23 | "not ie <= 11", 24 | "not op_mini all" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/the-road-to-graphql/react-graphql-github-vanilla/9d54b85c991a74483d376c349f71689aa41eed65/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /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": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | } 9 | 10 | .App-header { 11 | background-color: #282c34; 12 | min-height: 100vh; 13 | display: flex; 14 | flex-direction: column; 15 | align-items: center; 16 | justify-content: center; 17 | font-size: calc(10px + 2vmin); 18 | color: white; 19 | } 20 | 21 | .App-link { 22 | color: #61dafb; 23 | } 24 | 25 | @keyframes App-logo-spin { 26 | from { 27 | transform: rotate(0deg); 28 | } 29 | to { 30 | transform: rotate(360deg); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import axios from 'axios'; 3 | 4 | const TITLE = 'React GraphQL GitHub Client'; 5 | 6 | const axiosGitHubGraphQL = axios.create({ 7 | baseURL: 'https://api.github.com/graphql', 8 | headers: { 9 | Authorization: `bearer ${ 10 | process.env.REACT_APP_GITHUB_PERSONAL_ACCESS_TOKEN 11 | }`, 12 | }, 13 | }); 14 | 15 | const GET_ISSUES_OF_REPOSITORY = ` 16 | query ($organization: String!, $repository: String!, $cursor: String) { 17 | organization(login: $organization) { 18 | name 19 | url 20 | repository(name: $repository) { 21 | id 22 | name 23 | url 24 | stargazers { 25 | totalCount 26 | } 27 | viewerHasStarred 28 | issues(first: 5, after: $cursor, states: [OPEN]) { 29 | edges { 30 | node { 31 | id 32 | title 33 | url 34 | reactions(last: 3) { 35 | edges { 36 | node { 37 | id 38 | content 39 | } 40 | } 41 | } 42 | } 43 | } 44 | totalCount 45 | pageInfo { 46 | endCursor 47 | hasNextPage 48 | } 49 | } 50 | } 51 | } 52 | } 53 | `; 54 | 55 | const ADD_STAR = ` 56 | mutation ($repositoryId: ID!) { 57 | addStar(input:{starrableId:$repositoryId}) { 58 | starrable { 59 | viewerHasStarred 60 | } 61 | } 62 | } 63 | `; 64 | 65 | const REMOVE_STAR = ` 66 | mutation ($repositoryId: ID!) { 67 | removeStar(input:{starrableId:$repositoryId}) { 68 | starrable { 69 | viewerHasStarred 70 | } 71 | } 72 | } 73 | `; 74 | 75 | const getIssuesOfRepository = (path, cursor) => { 76 | const [organization, repository] = path.split('/'); 77 | 78 | return axiosGitHubGraphQL.post('', { 79 | query: GET_ISSUES_OF_REPOSITORY, 80 | variables: { organization, repository, cursor }, 81 | }); 82 | }; 83 | 84 | const resolveIssuesQuery = (queryResult, cursor) => state => { 85 | const { data, errors } = queryResult.data; 86 | 87 | if (!cursor) { 88 | return { 89 | organization: data.organization, 90 | errors, 91 | }; 92 | } 93 | 94 | const { edges: oldIssues } = state.organization.repository.issues; 95 | const { edges: newIssues } = data.organization.repository.issues; 96 | const updatedIssues = [...oldIssues, ...newIssues]; 97 | 98 | return { 99 | organization: { 100 | ...data.organization, 101 | repository: { 102 | ...data.organization.repository, 103 | issues: { 104 | ...data.organization.repository.issues, 105 | edges: updatedIssues, 106 | }, 107 | }, 108 | }, 109 | errors, 110 | }; 111 | }; 112 | 113 | const addStarToRepository = repositoryId => { 114 | return axiosGitHubGraphQL.post('', { 115 | query: ADD_STAR, 116 | variables: { repositoryId }, 117 | }); 118 | }; 119 | 120 | const resolveAddStarMutation = mutationResult => state => { 121 | const { 122 | viewerHasStarred, 123 | } = mutationResult.data.data.addStar.starrable; 124 | 125 | const { totalCount } = state.organization.repository.stargazers; 126 | 127 | return { 128 | ...state, 129 | organization: { 130 | ...state.organization, 131 | repository: { 132 | ...state.organization.repository, 133 | viewerHasStarred, 134 | stargazers: { 135 | totalCount: totalCount + 1, 136 | }, 137 | }, 138 | }, 139 | }; 140 | }; 141 | 142 | const removeStarFromRepository = repositoryId => { 143 | return axiosGitHubGraphQL.post('', { 144 | query: REMOVE_STAR, 145 | variables: { repositoryId }, 146 | }); 147 | }; 148 | 149 | const resolveRemoveStarMutation = mutationResult => state => { 150 | const { 151 | viewerHasStarred, 152 | } = mutationResult.data.data.removeStar.starrable; 153 | 154 | const { totalCount } = state.organization.repository.stargazers; 155 | 156 | return { 157 | ...state, 158 | organization: { 159 | ...state.organization, 160 | repository: { 161 | ...state.organization.repository, 162 | viewerHasStarred, 163 | stargazers: { 164 | totalCount: totalCount - 1, 165 | }, 166 | }, 167 | }, 168 | }; 169 | }; 170 | 171 | class App extends Component { 172 | state = { 173 | path: 'the-road-to-learn-react/the-road-to-learn-react', 174 | organization: null, 175 | errors: null, 176 | }; 177 | 178 | componentDidMount() { 179 | this.onFetchFromGitHub(this.state.path); 180 | } 181 | 182 | onChange = event => { 183 | this.setState({ path: event.target.value }); 184 | }; 185 | 186 | onSubmit = event => { 187 | this.onFetchFromGitHub(this.state.path); 188 | 189 | event.preventDefault(); 190 | }; 191 | 192 | onFetchFromGitHub = (path, cursor) => { 193 | getIssuesOfRepository(path, cursor).then(queryResult => 194 | this.setState(resolveIssuesQuery(queryResult, cursor)), 195 | ); 196 | }; 197 | 198 | onFetchMoreIssues = () => { 199 | const { 200 | endCursor, 201 | } = this.state.organization.repository.issues.pageInfo; 202 | 203 | this.onFetchFromGitHub(this.state.path, endCursor); 204 | }; 205 | 206 | onStarRepository = (repositoryId, viewerHasStarred) => { 207 | if (viewerHasStarred) { 208 | removeStarFromRepository(repositoryId).then(mutationResult => 209 | this.setState(resolveRemoveStarMutation(mutationResult)), 210 | ); 211 | } else { 212 | addStarToRepository(repositoryId).then(mutationResult => 213 | this.setState(resolveAddStarMutation(mutationResult)), 214 | ); 215 | } 216 | }; 217 | 218 | render() { 219 | const { path, organization, errors } = this.state; 220 | 221 | return ( 222 |
223 |

{TITLE}

224 | 225 |
226 | 229 | 236 | 237 |
238 | 239 |
240 | 241 | {organization ? ( 242 | 248 | ) : ( 249 |

No information yet ...

250 | )} 251 |
252 | ); 253 | } 254 | } 255 | 256 | const Organization = ({ 257 | organization, 258 | errors, 259 | onFetchMoreIssues, 260 | onStarRepository, 261 | }) => { 262 | if (errors) { 263 | return ( 264 |

265 | Something went wrong: 266 | {errors.map(error => error.message).join(' ')} 267 |

268 | ); 269 | } 270 | 271 | return ( 272 |
273 |

274 | Issues from Organization: 275 | {organization.name} 276 |

277 | 282 |
283 | ); 284 | }; 285 | 286 | const Repository = ({ 287 | repository, 288 | onFetchMoreIssues, 289 | onStarRepository, 290 | }) => ( 291 |
292 |

293 | In Repository: 294 | {repository.name} 295 |

296 | 297 | 306 | 307 | 320 | 321 |
322 | 323 | {repository.issues.pageInfo.hasNextPage && ( 324 | 325 | )} 326 |
327 | ); 328 | 329 | export default App; 330 | -------------------------------------------------------------------------------- /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 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 5 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 6 | sans-serif; 7 | -webkit-font-smoothing: antialiased; 8 | -moz-osx-font-smoothing: grayscale; 9 | } 10 | 11 | code { 12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 13 | monospace; 14 | } 15 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: http://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read http://bit.ly/CRA-PWA. 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit http://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | installingWorker.onstatechange = () => { 64 | if (installingWorker.state === 'installed') { 65 | if (navigator.serviceWorker.controller) { 66 | // At this point, the updated precached content has been fetched, 67 | // but the previous service worker will still serve the older 68 | // content until all client tabs are closed. 69 | console.log( 70 | 'New content is available and will be used when all ' + 71 | 'tabs for this page are closed. See http://bit.ly/CRA-PWA.' 72 | ); 73 | 74 | // Execute callback 75 | if (config && config.onUpdate) { 76 | config.onUpdate(registration); 77 | } 78 | } else { 79 | // At this point, everything has been precached. 80 | // It's the perfect time to display a 81 | // "Content is cached for offline use." message. 82 | console.log('Content is cached for offline use.'); 83 | 84 | // Execute callback 85 | if (config && config.onSuccess) { 86 | config.onSuccess(registration); 87 | } 88 | } 89 | } 90 | }; 91 | }; 92 | }) 93 | .catch(error => { 94 | console.error('Error during service worker registration:', error); 95 | }); 96 | } 97 | 98 | function checkValidServiceWorker(swUrl, config) { 99 | // Check if the service worker can be found. If it can't reload the page. 100 | fetch(swUrl) 101 | .then(response => { 102 | // Ensure service worker exists, and that we really are getting a JS file. 103 | if ( 104 | response.status === 404 || 105 | response.headers.get('content-type').indexOf('javascript') === -1 106 | ) { 107 | // No service worker found. Probably a different app. Reload the page. 108 | navigator.serviceWorker.ready.then(registration => { 109 | registration.unregister().then(() => { 110 | window.location.reload(); 111 | }); 112 | }); 113 | } else { 114 | // Service worker found. Proceed as normal. 115 | registerValidSW(swUrl, config); 116 | } 117 | }) 118 | .catch(() => { 119 | console.log( 120 | 'No internet connection found. App is running in offline mode.' 121 | ); 122 | }); 123 | } 124 | 125 | export function unregister() { 126 | if ('serviceWorker' in navigator) { 127 | navigator.serviceWorker.ready.then(registration => { 128 | registration.unregister(); 129 | }); 130 | } 131 | } 132 | --------------------------------------------------------------------------------