Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate to graphql #1

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/client": "^3.11.8",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand All @@ -12,6 +13,7 @@
"@types/react-dom": "^18.3.0",
"@types/react-router-dom": "^5.3.3",
"axios": "^1.7.7",
"graphql": "^16.9.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.2",
Expand Down
8 changes: 8 additions & 0 deletions client/src/apolloClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApolloClient, InMemoryCache } from "@apollo/client";

const client = new ApolloClient({
uri: "http://localhost:3000/", // Your GraphQL server URL
cache: new InMemoryCache(),
});

export default client;
21 changes: 12 additions & 9 deletions client/src/components/CreatePost.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import React, { useState } from 'react';
import axios from 'axios';
import { useMutation } from '@apollo/client';
import { CREATE_POST } from '../graphql/mutations';
import { useNavigate, Link } from 'react-router-dom';

const CreatePost: React.FC = () => {
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const navigate = useNavigate();

// Mutation to create a new post
const [createPost] = useMutation(CREATE_POST, {
onCompleted: () => {
// Redirect to the home page after creation
navigate('/');
},
refetchQueries: ['GetPosts'],
});

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();

await axios.post('http://localhost:3000/posts', {
title,
content,
});

// Redirect to the home page after creation
navigate('/');
await createPost({ variables: { title, content } });
};

return (
Expand Down
Loading