refactor: migrate to graphql #1
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactor: Migrate to GraphQL
This pull request migrates the existing REST API implementation to use GraphQL, leveraging Apollo Server on the backend and Apollo Client on the frontend. The main objective of this refactor is to address the limitations we encountered with the REST approach and improve the efficiency and flexibility of data fetching in our application.
Summary of Changes
Backend Changes
Replaced Express REST API with Apollo Server GraphQL API:
@apollo/server), eliminating the need for Express or other frameworks.gqlfunction fromgraphql-tag.Data Handling:
postsandcomments) as in the REST API to ensure consistency../datamodule.Frontend Changes
Installed and Configured Apollo Client:
@apollo/clientandgraphqlpackages.ApolloProvider.Refactored Components to Use GraphQL:
useQueryanduseMutationhooks.PostList: Fetches and displays the list of posts.PostDetail: Fetches and displays a single post with its comments, and handles adding and deleting comments.CreatePost: Provides a form to create new posts.Organized GraphQL Queries and Mutations:
src/graphql/queries.tsandsrc/graphql/mutations.tsto define and export GraphQL operations.Why We Migrated to GraphQL
Limitations of the REST Approach
Multiple Requests for Related Data:
/posts/:idand/posts/:id/comments).Inefficient Data Refetching:
Benefits of GraphQL
Fetch Multiple Resources in a Single Request:
Efficient Data Fetching:
Improved Performance:
Simplified Client Logic:
Detailed Implementation
Backend
Server Setup:
startStandaloneServerfrom@apollo/server/standaloneto create a standalone GraphQL server without additional frameworks.Schema Definition:
Post,Comment,Query, andMutationusing GraphQL SDL (Schema Definition Language).gqlfunction to parse the schema.Resolvers:
Post.comments).Frontend
Apollo Client Configuration:
InMemoryCachefor caching query results.Component Refactoring:
PostListComponent:useQueryhook to fetch posts.useMutationhook for deleting posts, withrefetchQueriesto update the list after deletion.PostDetailComponent:useQueryto fetch a post and its comments.useMutationfor creating and deleting comments, withrefetchQueriesto refresh the data.CreatePostComponent:useMutationhook to create new posts.GraphQL Operations Organization:
How This Improves the Application
Simplifies Data Fetching:
Reduces Network Overhead:
Enhances Developer Experience:
useQuery,useMutation) provide a declarative approach to data fetching and state management.Improves Performance:
How to Test the Changes
Start the GraphQL Server:
cd server npx ts-node src/index.tsStart the React Application:
cd client npm startVerify Functionality:
Future Improvements
Type Generation:
graphql-code-generatorto generate TypeScript types from GraphQL schema for better type safety.Optimistic UI Updates:
Subscriptions:
Data Persistence:
Conclusion
This refactor demonstrates the advantages of using GraphQL over REST in scenarios where efficient data fetching and flexibility are important. By migrating to GraphQL, we've simplified the data flow, reduced network overhead, and improved the overall developer and user experience.
Thank you for reviewing this pull request. Please let me know if you have any questions or suggestions!