This project aims to create a Twitter clone API using GraphQL in PHP8. The API will provide all the necessary queries and mutations to replicate essential features of Twitter. It enables users to perform actions such as creating tweets, following other users, liking tweets, retweeting, and more.
-Before running the Twitter Clone API, ensure you have the following installed:
- PHP 8 or higher
- Composer (Dependency Manager for PHP)
- MySQL or any compatible database server
Clone the repository:
git clone https://github.com/BaseMax/TwitterGraphQLPHP.git
cd TwitterGraphQLPHP
Install dependencies:
composer install
Configure the environment:
Copy the .env.example
file to .env
and set the required environment variables such as database credentials and Redis configuration.
Set up the database:
Run database migrations to set up the required tables:
php migrate.php
Start the development server:
php -S localhost:8000 public/index.php
The Twitter Clone API provides the following queries and mutations:
user(username: String!): User
: Get a user's profile information by their username.tweet(id: ID!): Tweet
: Get a specific tweet by its ID.timeline(userId: ID!): [Tweet!]!
: Get the user's timeline (tweets from the users they follow).
createUser(input: CreateUserInput!): User
: Create a new user account.createTweet(input: CreateTweetInput!): Tweet
: Create a new tweet.followUser(userId: ID!): User
: Follow another user.unfollowUser(userId: ID!): User
: Unfollow a previously followed user.likeTweet(tweetId: ID!, userId: ID!): Tweet
: Like a tweet.unlikeTweet(tweetId: ID!, userId: ID!): Tweet
: Remove a like from a previously liked tweet.retweet(tweetId: ID!, userId: ID!): Tweet
: Retweet a tweet.deleteTweet(tweetId: ID!): Boolean
: Delete a tweet.
To perform certain mutations, such as creating tweets, following users, liking tweets, or retweeting, you need to authenticate your requests. Please include an authentication token in the request headers using the following format:
Authorization: Bearer YOUR_AUTH_TOKEN
The API uses JWT (JSON Web Tokens) for authentication. To obtain an authentication token, you can use the login mutation, passing your username and password as input, and it will return a token that you can use for subsequent requests.
The API handles errors using GraphQL's built-in error handling mechanisms. If an error occurs, the API will provide meaningful error messages with relevant details.
To improve performance, the API utilizes Redis caching for certain queries and data. Cached data will be served when available, reducing the load on the database.
The API comes with a suite of unit tests and integration tests. To run the tests, execute the following command:
phpunit
type User {
id: ID!
username: String!
name: String!
}
type Tweet {
id: ID!
text: String!
author: User!
}
input CreateUserInput {
username: String!
password: String!
name: String!
}
input CreateTweetInput {
text: String!
}
type Query {
user(username: String!): User
tweet(id: ID!): Tweet
timeline(userId: ID!): [Tweet!]!
}
type Mutation {
createUser(input: CreateUserInput!): User
createTweet(input: CreateTweetInput!): Tweet
followUser(userId: ID!): User
unfollowUser(userId: ID!): User
likeTweet(tweetId: ID!): Tweet
unlikeTweet(tweetId: ID!): Tweet
retweet(tweetId: ID!): Tweet
deleteTweet(tweetId: ID!): Boolean
}
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"{ user(username: \"example_user\") { id username name } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"{ tweet(id: \"example_tweet_id\") { id text author { id username } } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"{ timeline(userId: \"example_user_id\") { id text author { id username } } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -d '{"query":"mutation { createUser(input: { username: \"new_user\", password: \"password123\", name: \"New User\" }) { id username name } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"mutation { createTweet(input: { text: \"This is a new tweet!\" }) { id text author { id username } } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"mutation { followUser(userId: \"user_to_follow_id\") { id username } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"mutation { unfollowUser(userId: \"user_to_unfollow_id\") { id username } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"mutation { likeTweet(tweetId: \"tweet_to_like_id\") { id text author { id username } } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"mutation { unlikeTweet(tweetId: \"tweet_to_unlike_id\") { id text author { id username } } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"mutation { retweet(tweetId: \"tweet_to_retweet_id\") { id text author { id username } } }"}' YOUR_API_URL
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_AUTH_TOKEN" -d '{"query":"mutation { deleteTweet(tweetId: \"tweet_to_delete_id\") }"}' YOUR_API_URL
Make sure to replace YOUR_API_URL with the actual URL of your GraphQL API endpoint and YOUR_AUTH_TOKEN with a valid authentication token obtained from the login mutation, if required.
For more information on the API's schema, queries, and mutations, you can access the GraphQL documentation tool at http://localhost:8000/graphql-playground
after starting the development server.
Contributions to the Twitter Clone API project are welcome.
Authors:
- Ali Ahmadi
- Max Base
Copyright 2023, Max Base