├── client ├── src │ ├── react-app-env.d.ts │ ├── components │ │ ├── Posts.tsx │ │ └── Picker.tsx │ ├── index.tsx │ ├── __generated__ │ │ └── GetSubreddit.ts │ └── App.tsx ├── .gitignore ├── apollo.config.js ├── __generated__ │ └── globalTypes.ts ├── tsconfig.json ├── public │ └── index.html ├── package.json └── README.md ├── server ├── .gitignore ├── package.json └── index.js ├── README.md └── .vscode ├── tasks.json └── settings.json /client/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | node_modules 5 | 6 | # production 7 | build 8 | 9 | # misc 10 | .DS_Store 11 | npm-debug.log 12 | -------------------------------------------------------------------------------- /server/.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | node_modules 5 | 6 | # production 7 | build 8 | 9 | # misc 10 | .DS_Store 11 | npm-debug.log 12 | -------------------------------------------------------------------------------- /client/apollo.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | client: { 3 | service: { 4 | name: 'redux-to-graphql', 5 | // url: 'http://localhost:4000', 6 | url: 'https://m3507x64l8.sse.codesandbox.io/', 7 | includes: ['src/**/*.{ts,tsx,js,jsx}'], 8 | }, 9 | }, 10 | }; 11 | -------------------------------------------------------------------------------- /client/src/components/Posts.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import * as ApolloTypes from '../__generated__/GetSubreddit'; 3 | 4 | type Props = { 5 | posts: Array; 6 | }; 7 | 8 | const Posts: React.FC = ({ posts }) => ( 9 |
    10 | {posts.map(post => post &&
  • {`${post.title}`}
  • )} 11 |
12 | ); 13 | 14 | export default Posts; 15 | -------------------------------------------------------------------------------- /server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-to-graphql-server", 3 | "version": "1.0.0", 4 | "description": "A GraphQL server for the Reddit API", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "Peggy Rayzis", 11 | "license": "ISC", 12 | "dependencies": { 13 | "apollo-server": "^2.4.8", 14 | "graphql": "^14.2.1", 15 | "node-fetch": "^2.3.0" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /client/src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from 'react-dom'; 3 | import ApolloClient from 'apollo-boost'; 4 | import { ApolloProvider } from '@apollo/react-hooks'; 5 | 6 | import App from './App'; 7 | 8 | const client = new ApolloClient({ 9 | uri: 'https://m3507x64l8.sse.codesandbox.io/', 10 | resolvers: {}, 11 | }); 12 | 13 | render( 14 | 15 | 16 | , 17 | document.getElementById('root'), 18 | ); 19 | -------------------------------------------------------------------------------- /client/__generated__/globalTypes.ts: -------------------------------------------------------------------------------- 1 | /* tslint:disable */ 2 | /* eslint-disable */ 3 | // This file was automatically generated and should not be edited. 4 | 5 | //============================================================== 6 | // START Enums and Input Objects 7 | //============================================================== 8 | 9 | //============================================================== 10 | // END Enums and Input Objects 11 | //============================================================== 12 | -------------------------------------------------------------------------------- /client/src/components/Picker.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | type Props = { 4 | options: Array; 5 | value: string; 6 | onChange: any; 7 | }; 8 | 9 | const Picker: React.FC = ({ value, onChange, options }) => ( 10 | 11 |

{value}

12 | 19 |
20 | ); 21 | 22 | export default Picker; 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Redux to GraphQL 2 | 3 | The [Redux tutorial app](https://redux.js.org/advanced/example-reddit-api), built with GraphQL & Apollo! 4 | 5 | ## Development 6 | 7 | ### Server 8 | 9 | ```bash 10 | cd server && npm i && npm start 11 | ``` 12 | 13 | ### Client 14 | 15 | Make sure the server is running before you start up the client. To get the full experience, install the [Apollo VS Code extension](https://marketplace.visualstudio.com/items?itemName=apollographql.vscode-apollo) for some extra magic. ✨ 16 | 17 | ```bash 18 | cd client && npm i && npm start 19 | ``` 20 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "type": "npm", 8 | "script": "start", 9 | "path": "server/", 10 | "problemMatcher": [] 11 | }, 12 | { 13 | "type": "npm", 14 | "script": "codegen:watch", 15 | "path": "client/", 16 | "runOptions": { "runOn": "folderOpen" } 17 | }, 18 | { 19 | "type": "npm", 20 | "script": "start", 21 | "path": "client/" 22 | } 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /client/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "module": "esnext", 16 | "moduleResolution": "node", 17 | "resolveJsonModule": true, 18 | "isolatedModules": true, 19 | "noEmit": true, 20 | "jsx": "preserve" 21 | }, 22 | "include": [ 23 | "src" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Redux Async Example 7 | 8 | 9 |
10 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /client/src/__generated__/GetSubreddit.ts: -------------------------------------------------------------------------------- 1 | /* tslint:disable */ 2 | /* eslint-disable */ 3 | // This file was automatically generated and should not be edited. 4 | 5 | // ==================================================== 6 | // GraphQL query operation: GetSubreddit 7 | // ==================================================== 8 | 9 | export interface GetSubreddit_subreddit_posts { 10 | __typename: "Post"; 11 | title: string; 12 | } 13 | 14 | export interface GetSubreddit_subreddit { 15 | __typename: "Subreddit"; 16 | posts: (GetSubreddit_subreddit_posts | null)[]; 17 | lastUpdated: string; 18 | } 19 | 20 | export interface GetSubreddit { 21 | subreddit: GetSubreddit_subreddit | null; 22 | } 23 | 24 | export interface GetSubredditVariables { 25 | name: string; 26 | } 27 | -------------------------------------------------------------------------------- /server/index.js: -------------------------------------------------------------------------------- 1 | const { ApolloServer, gql } = require('apollo-server'); 2 | const fetch = require('node-fetch'); 3 | 4 | const typeDefs = gql` 5 | type Query { 6 | subreddit(name: String!): Subreddit 7 | } 8 | 9 | type Subreddit { 10 | posts: [Post]! 11 | } 12 | 13 | type Post { 14 | title: String! 15 | author: String! 16 | """ 17 | The upvotes a post has received 18 | """ 19 | ups: Int! 20 | } 21 | `; 22 | 23 | const resolvers = { 24 | Query: { 25 | subreddit: (root, { name }) => { 26 | return fetch(`https://www.reddit.com/r/${name}.json`) 27 | .then(res => res.json()) 28 | .then(({ data }) => data); 29 | }, 30 | }, 31 | Subreddit: { 32 | posts: subreddit => 33 | subreddit ? subreddit.children.map(child => child.data) : [], 34 | }, 35 | }; 36 | 37 | const server = new ApolloServer({ typeDefs, resolvers }); 38 | 39 | server.listen().then(({ url }) => { 40 | console.log(`🚀 Server ready at ${url}`); 41 | }); 42 | -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-to-graphql-client", 3 | "version": "0.0.1", 4 | "private": true, 5 | "devDependencies": { 6 | "@types/graphql": "^14.2.0", 7 | "@types/node": "^11.13.4", 8 | "@types/react": "^16.8.13", 9 | "@types/react-dom": "^16.8.4", 10 | "apollo": "^2.8.3", 11 | "cross-env": "^5.2.0", 12 | "react-scripts": "2.1.8", 13 | "typescript": "^3.4.3" 14 | }, 15 | "dependencies": { 16 | "@apollo/react-hooks": "0.1.0-beta.2", 17 | "apollo-boost": "^0.3.1", 18 | "graphql": "^14.2.1", 19 | "react": "^16.8.6", 20 | "react-dom": "^16.8.6" 21 | }, 22 | "scripts": { 23 | "start": "cross-env PORT=3001 react-scripts start", 24 | "build": "react-scripts build", 25 | "eject": "react-scripts eject", 26 | "test": "react-scripts test --env=node --passWithNoTests", 27 | "codegen:watch": "npx apollo client:codegen --target=typescript --watch" 28 | }, 29 | "browserslist": [ 30 | ">0.2%", 31 | "not dead", 32 | "not ie <= 11", 33 | "not op_mini all" 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "peacock.affectActivityBar": true, 3 | "peacock.elementAdjustments": { 4 | "activityBar": "none", 5 | "statusBar": "none", 6 | "titleBar": "none" 7 | }, 8 | "editor.fontWeight": "500", 9 | "browser-preview.startUrl": "http://localhost:3001/", 10 | "workbench.colorCustomizations": { 11 | "list.activeSelectionForeground": "#80CBC4", 12 | "list.inactiveSelectionForeground": "#80CBC4", 13 | "list.highlightForeground": "#80CBC4", 14 | "scrollbarSlider.activeBackground": "#80CBC450", 15 | "editorSuggestWidget.highlightForeground": "#80CBC4", 16 | "textLink.foreground": "#80CBC4", 17 | "progressBar.background": "#80CBC4", 18 | "pickerGroup.foreground": "#80CBC4", 19 | "tab.activeBorder": "#80CBC4", 20 | "activityBar.background": "#10cbc4", 21 | "activityBar.foreground": "#15202b", 22 | "activityBar.inactiveForeground": "#15202b99", 23 | "activityBarBadge.background": "#290b9f", 24 | "activityBarBadge.foreground": "#e7e7e7", 25 | "titleBar.activeBackground": "#10cbc4", 26 | "titleBar.inactiveBackground": "#10cbc499", 27 | "titleBar.activeForeground": "#15202b", 28 | "titleBar.inactiveForeground": "#15202b99" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /client/src/App.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import { QueryResult } from '@apollo/react-common'; 3 | import { useQuery } from '@apollo/react-hooks'; 4 | import { gql } from 'apollo-boost'; 5 | 6 | import Picker from './components/Picker'; 7 | import Posts from './components/Posts'; 8 | 9 | import * as ApolloTypes from './__generated__/GetSubreddit'; 10 | 11 | const clientSchema = gql` 12 | extend type Subreddit { 13 | lastUpdated: String! 14 | } 15 | `; 16 | 17 | const resolvers = { 18 | Subreddit: { 19 | lastUpdated: () => new Date(Date.now()).toLocaleTimeString(), 20 | }, 21 | }; 22 | 23 | const GET_SUBREDDIT = gql` 24 | query GetSubreddit($name: String!) { 25 | subreddit(name: $name) { 26 | posts { 27 | title 28 | } 29 | lastUpdated @client 30 | } 31 | } 32 | `; 33 | 34 | const App: React.FC = () => { 35 | const [selectedSubreddit, setSelectedSubreddit] = useState('reactjs'); 36 | const { 37 | data, 38 | loading, 39 | error, 40 | refetch, 41 | networkStatus, 42 | client, 43 | }: QueryResult< 44 | ApolloTypes.GetSubreddit, 45 | ApolloTypes.GetSubredditVariables 46 | > = useQuery(GET_SUBREDDIT, { 47 | variables: { name: selectedSubreddit }, 48 | notifyOnNetworkStatusChange: true, 49 | }); 50 | 51 | client.addResolvers(resolvers); 52 | 53 | const refetching = networkStatus === 4; 54 | 55 | if (loading && !refetching) return

Loading...

; 56 | if (error) return

{`Error: ${error}`}

; 57 | 58 | return ( 59 |
60 | 65 |

66 | 67 | {data && 68 | `Last updated at ${data.subreddit && data.subreddit.lastUpdated}.`} 69 | 70 | {!loading && } 71 |

72 |
73 | 74 |
75 |
76 | ); 77 | }; 78 | 79 | export default App; 80 | -------------------------------------------------------------------------------- /client/README.md: -------------------------------------------------------------------------------- 1 | # Redux Async Example 2 | 3 | This project template was built with [Create React App](https://github.com/facebookincubator/create-react-app), which provides a simple way to start React projects with no build configuration needed. 4 | 5 | Projects built with Create-React-App include support for ES6 syntax, as well as several unofficial / not-yet-final forms of Javascript syntax such as Class Properties and JSX. See the list of [language features and polyfills supported by Create-React-App](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#supported-language-features-and-polyfills) for more information. 6 | 7 | ## Available Scripts 8 | 9 | In the project directory, you can run: 10 | 11 | ### `npm start` 12 | 13 | Runs the app in the development mode.
14 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 15 | 16 | The page will reload if you make edits.
17 | You will also see any lint errors in the console. 18 | 19 | ### `npm run build` 20 | 21 | Builds the app for production to the `build` folder.
22 | It correctly bundles React in production mode and optimizes the build for the best performance. 23 | 24 | The build is minified and the filenames include the hashes.
25 | Your app is ready to be deployed! 26 | 27 | ### `npm run eject` 28 | 29 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 30 | 31 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 32 | 33 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 34 | 35 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 36 | --------------------------------------------------------------------------------