From 1e08ccc1c314104da0fac65fa187e8c833c8d9ed Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 25 Oct 2020 12:53:50 +0100 Subject: [PATCH] query data using graphql --- src/containers/home/TodoList.tsx | 2 +- src/containers/home/index.tsx | 20 ++++++++++++++++---- src/core/models/todo.ts | 2 +- src/core/services/todo.ts | 4 ++-- src/index.tsx | 24 ++++++------------------ 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/containers/home/TodoList.tsx b/src/containers/home/TodoList.tsx index 68b90d0..ce509c3 100644 --- a/src/containers/home/TodoList.tsx +++ b/src/containers/home/TodoList.tsx @@ -10,7 +10,7 @@ const TodoList: FC = ({ todos }) => (
    {todos.map((todo) => ( -
  • +
  • {todo.title}, status: {todo.isDone ? 'Done' : 'In Progress'}
  • ))} diff --git a/src/containers/home/index.tsx b/src/containers/home/index.tsx index 1d368cb..2db5824 100644 --- a/src/containers/home/index.tsx +++ b/src/containers/home/index.tsx @@ -1,20 +1,32 @@ import React, { FC } from 'react'; +import { gql, useQuery } from '@apollo/client'; import TodoList from './TodoList'; import { Button } from '../../components/button'; import { Container } from '../../components/container'; -import Todo from '../../core/models/todo'; -import { listTodos } from '../../core/services/todo'; +const LIST_TODOS = gql` + query ListAll { + listTodos { + todoId + title + isDone + } + } +`; export const Home: FC = () => { - const todos: Todo[] = listTodos(); + // const todos: Todo[] = listTodos(); + const { loading, error, data } = useQuery(LIST_TODOS); + if (error) { + return

    Sorry...

    ; + } return (

    Your tasks

    Hi there!

    - + {!loading && } diff --git a/src/core/models/todo.ts b/src/core/models/todo.ts index 227207a..f4b251a 100644 --- a/src/core/models/todo.ts +++ b/src/core/models/todo.ts @@ -1,5 +1,5 @@ export default interface Todo { - id: string; + todoId: string; title: string; isDone: boolean; } diff --git a/src/core/services/todo.ts b/src/core/services/todo.ts index 64d2364..b75a63b 100644 --- a/src/core/services/todo.ts +++ b/src/core/services/todo.ts @@ -3,12 +3,12 @@ import Todo from '../models/todo'; export const listTodos = (): Todo[] => { return [ { - id: '1', + todoId: '1', title: 'test', isDone: false }, { - id: '2', + todoId: '2', title: 'second', isDone: true } diff --git a/src/index.tsx b/src/index.tsx index a0d197a..f6c426a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,33 +6,21 @@ import App from './App'; import * as serviceWorker from './serviceWorker'; import { ThemeProvider } from 'styled-components'; import { myTheme } from './core/theme'; -import { ApolloClient, gql, InMemoryCache } from '@apollo/client'; +import { ApolloClient, ApolloProvider, InMemoryCache } from '@apollo/client'; const client = new ApolloClient({ uri: 'http://127.0.0.1:8000/', cache: new InMemoryCache() }); -client - .query({ - query: gql` - query ListAll { - listTodos { - todoId - title - isDone - } - } - ` - }) - .then((res) => console.log(res)); - ReactDOM.render( - - - + + + + + , document.getElementById('root')