mirror of
https://github.com/rjNemo/react_template
synced 2026-06-06 10:56:48 +00:00
query data using graphql
This commit is contained in:
parent
baaf610afd
commit
1e08ccc1c3
5 changed files with 26 additions and 26 deletions
|
|
@ -10,7 +10,7 @@ const TodoList: FC<Props> = ({ todos }) => (
|
||||||
<div>
|
<div>
|
||||||
<ul>
|
<ul>
|
||||||
{todos.map((todo) => (
|
{todos.map((todo) => (
|
||||||
<li key={todo.id}>
|
<li key={todo.todoId}>
|
||||||
{todo.title}, status: {todo.isDone ? 'Done' : 'In Progress'}
|
{todo.title}, status: {todo.isDone ? 'Done' : 'In Progress'}
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,32 @@
|
||||||
import React, { FC } from 'react';
|
import React, { FC } from 'react';
|
||||||
|
import { gql, useQuery } from '@apollo/client';
|
||||||
|
|
||||||
import TodoList from './TodoList';
|
import TodoList from './TodoList';
|
||||||
import { Button } from '../../components/button';
|
import { Button } from '../../components/button';
|
||||||
import { Container } from '../../components/container';
|
import { Container } from '../../components/container';
|
||||||
|
|
||||||
import Todo from '../../core/models/todo';
|
const LIST_TODOS = gql`
|
||||||
import { listTodos } from '../../core/services/todo';
|
query ListAll {
|
||||||
|
listTodos {
|
||||||
|
todoId
|
||||||
|
title
|
||||||
|
isDone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
export const Home: FC = () => {
|
export const Home: FC = () => {
|
||||||
const todos: Todo[] = listTodos();
|
// const todos: Todo[] = listTodos();
|
||||||
|
const { loading, error, data } = useQuery(LIST_TODOS);
|
||||||
|
if (error) {
|
||||||
|
return <p>Sorry...</p>;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container>
|
<Container>
|
||||||
<h1>Your tasks</h1>
|
<h1>Your tasks</h1>
|
||||||
<p>Hi there!</p>
|
<p>Hi there!</p>
|
||||||
<TodoList todos={todos} />
|
{!loading && <TodoList todos={data.listTodos} />}
|
||||||
<Button primary onClick={() => console.log('Clicked Normal Button')}>
|
<Button primary onClick={() => console.log('Clicked Normal Button')}>
|
||||||
Normal Button
|
Normal Button
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
export default interface Todo {
|
export default interface Todo {
|
||||||
id: string;
|
todoId: string;
|
||||||
title: string;
|
title: string;
|
||||||
isDone: boolean;
|
isDone: boolean;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,12 @@ import Todo from '../models/todo';
|
||||||
export const listTodos = (): Todo[] => {
|
export const listTodos = (): Todo[] => {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
id: '1',
|
todoId: '1',
|
||||||
title: 'test',
|
title: 'test',
|
||||||
isDone: false
|
isDone: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '2',
|
todoId: '2',
|
||||||
title: 'second',
|
title: 'second',
|
||||||
isDone: true
|
isDone: true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,33 +6,21 @@ import App from './App';
|
||||||
import * as serviceWorker from './serviceWorker';
|
import * as serviceWorker from './serviceWorker';
|
||||||
import { ThemeProvider } from 'styled-components';
|
import { ThemeProvider } from 'styled-components';
|
||||||
import { myTheme } from './core/theme';
|
import { myTheme } from './core/theme';
|
||||||
import { ApolloClient, gql, InMemoryCache } from '@apollo/client';
|
import { ApolloClient, ApolloProvider, InMemoryCache } from '@apollo/client';
|
||||||
|
|
||||||
const client = new ApolloClient({
|
const client = new ApolloClient({
|
||||||
uri: 'http://127.0.0.1:8000/',
|
uri: 'http://127.0.0.1:8000/',
|
||||||
cache: new InMemoryCache()
|
cache: new InMemoryCache()
|
||||||
});
|
});
|
||||||
|
|
||||||
client
|
|
||||||
.query({
|
|
||||||
query: gql`
|
|
||||||
query ListAll {
|
|
||||||
listTodos {
|
|
||||||
todoId
|
|
||||||
title
|
|
||||||
isDone
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
})
|
|
||||||
.then((res) => console.log(res));
|
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<ThemeProvider theme={myTheme}>
|
<ThemeProvider theme={myTheme}>
|
||||||
|
<ApolloProvider client={client}>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<App />
|
<App />
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
|
</ApolloProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
</React.StrictMode>,
|
</React.StrictMode>,
|
||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue