diff --git a/.gitignore b/.gitignore index 886e30e284..dbb094f407 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ exchanges/*/LICENSE # TODO: Figure out how to remove these: tmp/ dist/ +examples/*/public +examples/*/yarn.lock \ No newline at end of file diff --git a/examples/with-react/README.md b/examples/with-react/README.md new file mode 100644 index 0000000000..d3da29c479 --- /dev/null +++ b/examples/with-react/README.md @@ -0,0 +1,15 @@ +# Integrating with React + +Integrating urql is as simple as: + +1. Install packages [getting started](https://formidable.com/open-source/urql/docs/basics/react-preact/) + +```sh +yarn add urql graphql +# or +npm install --save urql graphql +``` + +2. Setting up the Client [here](src/App.js) + +3. Execute the Query [here](src/pages/PokemonList.js) diff --git a/examples/with-react/package.json b/examples/with-react/package.json new file mode 100644 index 0000000000..107c24e0ed --- /dev/null +++ b/examples/with-react/package.json @@ -0,0 +1,38 @@ +{ + "name": "react-urql-query", + "version": "1.0.0", + "private": true, + "dependencies": { + "graphql": "^15.5.0", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "urql": "^2.0.2" + }, + "devDependencies": { + "react-scripts": "4.0.3" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": [ + "react-app", + "react-app/jest" + ] + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } +} diff --git a/examples/with-react/src/App.js b/examples/with-react/src/App.js new file mode 100644 index 0000000000..fa525b741a --- /dev/null +++ b/examples/with-react/src/App.js @@ -0,0 +1,17 @@ +import { createClient, Provider } from "urql"; + +import PokemonList from "./pages/PokemonList"; + +const client = createClient({ + url: "https://trygql.dev/graphql/basic-pokedex", +}); + +function App() { + return ( + + + + ); +} + +export default App; diff --git a/examples/with-react/src/index.css b/examples/with-react/src/index.css new file mode 100644 index 0000000000..ec2585e8c0 --- /dev/null +++ b/examples/with-react/src/index.css @@ -0,0 +1,13 @@ +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', + 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + monospace; +} diff --git a/examples/with-react/src/index.js b/examples/with-react/src/index.js new file mode 100644 index 0000000000..62adcf3c5f --- /dev/null +++ b/examples/with-react/src/index.js @@ -0,0 +1,12 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import './index.css'; +import App from './App'; + +ReactDOM.render( + + + , + document.getElementById('root') +); + diff --git a/examples/with-react/src/pages/PokemonList.js b/examples/with-react/src/pages/PokemonList.js new file mode 100644 index 0000000000..84743c0faf --- /dev/null +++ b/examples/with-react/src/pages/PokemonList.js @@ -0,0 +1,35 @@ +import React from "react"; +import { gql, useQuery } from "urql"; + +const POKEMONS_QUERY = gql` + query Pokemons { + pokemons(limit: 10) { + id + name + } + } +`; + +const PokemonList = () => { + const [result] = useQuery({ query: POKEMONS_QUERY }); + + const { data, fetching, error } = result; + + return ( +
+ {fetching &&

Loading...

} + + {error &&

Oh no... {error.message}

} + + {data && ( + + )} +
+ ); +}; + +export default PokemonList;