Skip to content

Commit

Permalink
28-02-mutation-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
leo41271 committed Jul 30, 2024
1 parent e48121c commit 43a4114
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 48 deletions.
32 changes: 26 additions & 6 deletions 28 GraphQL/backend/graphql/schema.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
const { buildSchema } = require('graphql');

module.exports = buildSchema(`
type TestData {
text: String!
views: Int!
type Post {
_id: ID!
title: String!
content: String!
imageUrl: String!
creator: User!
createdAt: String!
updatedAt: String!
}

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

_id: ID!
ID 內建提供的 視為 id

type RootQuery {
hello: TestData!
type User {
_id: ID!
name: String!
email: String!
password: String
status: String!
posts: [Post!]!
}
input UserData {

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

input 專為 input 時使用的 關鍵字 (不要用type)

email: String!
name: String!
password: String!
}
type RootMutation {
createUser(userInput: UserData): User
}

This comment has been minimized.

Copy link
@leo41271

leo41271 Aug 30, 2024

Author Owner

createUser:這是突變的名稱。客戶端可以使用這個名稱來呼叫該突變操作。
userInput:這是參數的名稱,表示突變需要一些用戶輸入數據。
UserData:這是參數的類型,表示 userInput 需要符合 UserData 類型的結構。
: User:這部分定義了 createUser 突變的返回類型

schema {
query: RootQuery
mutation: RootMutation
}
`);
3 changes: 1 addition & 2 deletions 28 GraphQL/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.3",
"react-scripts": "^5.0.1",
"socket.io-client": "^4.7.5"
"react-scripts": "^5.0.1"
},
"scripts": {
"start": "react-scripts start",
Expand Down
40 changes: 0 additions & 40 deletions 28 GraphQL/frontend/src/pages/Feed/Feed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component, Fragment } from 'react';
import openSocket from 'socket.io-client';

import Post from '../../components/Feed/Post/Post';
import Button from '../../components/Button/Button';
Expand Down Expand Up @@ -40,47 +39,8 @@ class Feed extends Component {
.catch(this.catchError);

this.loadPosts();
const socket = openSocket('http://localhost:8080');
socket.on('posts', (data) => {
if (data.action === 'create') {
this.addPost(data.post);
} else if (data.action === 'update') {
this.updatePost(data.post);
} else if (data.action === 'delete') {
this.loadPosts();
}
});
}

addPost = (post) => {
this.setState((prevState) => {
const updatedPosts = [...prevState.posts];
if (prevState.postPage === 1) {
if (prevState.posts.length >= 2) {
updatedPosts.pop();
}
updatedPosts.unshift(post);
}
return {
posts: updatedPosts,
totalPosts: prevState.totalPosts + 1,
};
});
};

updatePost = (post) => {
this.setState((prevState) => {
const updatedPosts = [...prevState.posts];
const updatedPostIndex = updatedPosts.findIndex((p) => p._id === post._id);
if (updatedPostIndex > -1) {
updatedPosts[updatedPostIndex] = post;
}
return {
posts: updatedPosts,
};
});
};

loadPosts = (direction) => {
if (direction) {
this.setState({ postsLoading: true, posts: [] });
Expand Down

0 comments on commit 43a4114

Please sign in to comment.