Giter Site home page Giter Site logo

basemax / musictsgraphql Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 856 KB

This project is a GraphQL API built with TypeScript and Fastify, using the Mercurius plugin. It provides a backend server for a music site, allowing you to perform various operations such as managing songs and albums, retrieving information, adding comments, rating songs/albums, and more.

License: GNU General Public License v3.0

JavaScript 0.51% Dockerfile 0.51% TypeScript 97.91% Shell 1.08%
graphql graphql-ts graphql-typescript music music-api song songs ts ts-graphql typescript

musictsgraphql's Introduction

Music TS GraphQL

This project is a GraphQL API built with TypeScript and Fastify, using the Mercurius plugin. It provides a backend server for a music site, allowing you to perform various operations such as managing songs and albums, retrieving information, adding comments, rating songs/albums, and more.

Features

  • Song and album management
  • Comment addition and rating
  • Information retrieval

Stack

This project is built using the following technologies:

  • Language: TypeScript (Type-safe superset of JavaScript)
  • Database: Prisma (ORM for database access)
  • Web Framework: Fastify (High-performance web framework)
  • GraphQL Library: Apollo Server (GraphQL implementation)
  • Testing: Jest (Testing framework)
  • Code Quality: ESLint (Linting), Prettier (Code formatting)

Authentication

Authentication is done through an Authorization Header using a token. The token can be obtained by calling the login and register mutations.

Once the token is obtained, it needs to be included in the Authorization Header of all subsequent GraphQL requests. The app validates the token and restricts access to certain mutations and queries based on the token's permissions.

Some mutations and queries require higher privileges, such as a specific role, to be executed. These operations can only be performed by users with the required privileges.

Example Header:

Authorization: <token>

File upload

This app supports file upload through a dedicated endpoint at /upload/<type>/<name-prefix>.extension. If the uploaded file is an image it will be resized to a specific size based on the chosen type. Currently, the supported types and sizes are:

  • cover: { width: 600, height: 600 }
  • avatar: { width: 600, height: 600 }

To upload a file, you should make a POST request to the /upload/<type>/<name-prefix> endpoint with the file contents as the request body. The request body should be a binary file, not multipart form data.

The <type> parameter specifies the type of the uploaded file, while the <name-prefix> parameter is used to generate a unique filename for the uploaded file. The uploaded file will be resized and saved to the server, and the response will contain a JSON object with the URL of the uploaded file. For example: additionally there is a route to upload Musics something like : /upload/music/<name-prefix>.mp3

Example curl request:

curl http://localhost:3000/upload/music/something.mp3 -F "[email protected]" -H 'Authorization: <token>'
{"url":"/poster/testtt-cliiy9tkb000015ln9byqftw4.jpeg"}

You can use the returned URL wherever a file is required in your application.

Usage

To use this project, follow these steps:

  1. Clone this repository:

    git clone https://github.com/basemax/MusicTSGraphQL
  2. Install dependencies:

    npm install
  3. Run the app using Docker Compose:

    sudo docker compose -p ci -f docker-compose.base-dev.yml -f docker-compose.dev.yml up --build

    This will start the app in development mode, with hotreloading enabled. The GraphQL playground will be available at http://localhost:3000/graphql. โ†—

  4. Attach to the container:

    sudo docker exec -it ci-app-1 bash
  5. Create an admin account:

    node dist/create-admin.js

    It will prompt you for an email, password, and name to create the superuser account.

Testing

Run the following command:

sudo ./test.sh

It will automatically do everything and exit with 0 status if everything work well

Examples

Here are some example GraphQL queries:

  1. Query all genres:

    query {
        genres {
            id
            name
        }
    }
  2. Query a specific album:

    query {
        album(id: "<album_id>") {
            id
            title
            releaseDate
            cover
            musics {
                id
                name
                duration
            }
        }
    }
  3. Query all unapproved comments:

    query {
        unapprovedComments(pagination: { limit: 10 }) {
            id
            text
            isApproved
            user {
                id
                name
            }
            music {
                id
                name
            }
        }
    }
  4. Create a new album:

    mutation {
        createAlbum(input: {
            title: "New Album",
            cover: "...",
            releaseDate: "2021-07-12",
            musics: ["<music_id_1>", "<music_id_2>"]
        }) {
            id
            title
            releaseDate
            cover
            musics {
                id
                name
                duration
            }
        }
    }
  5. Update an existing album:

    mutation {
        updateAlbum(input: {
            id: "<album_id>",
            title: "Updated Album",
            cover: "...",
            releaseDate: "2021-07-15",
            musics: ["<music_id_1>", "<music_id_2>", "<music_id_3>"]
        }) {
            id
            title
            releaseDate
            cover
            musics {
                id
                name
                duration
            }
        }
    }
  6. Delete a comment:

    mutation {
        deleteComment(id: "<comment_id>")
    }

API Documentation

The following table describes all the mutations and queries available in the API:

Query/Mutation Description
album(id: String!) Query a specific album by its ID
genres Query all genres
genre(id: String!) Query a specific genre by its ID
music(id: String!) Query a specific song by its ID
searchAlbums(input: SearchAlbumInput!) Search for albums based on specific criteria
searchMusic(input: SearchMusicInput!) Search for songs based on specific criteria
searchSingers(input: SearchSingerInput!) Search for singers based on specific criteria
singer(id: String!) Query a specific singer by its ID
unapprovedComments(pagination: PaginationInput!) Query all unapproved comments
user Query the current user
approveComment(id: String!) Approve a specific comment
changeRole(role: UserRole!, userId: String!) Change the role of a specific user
createAlbum(input: CreateAlbumInput!) Create a new album
createComment(input: CreateCommentInput!) Create a new comment
createGenre(input: CreateGenreInput!) Create a new genre
createMusic(input: CreateMusicInput!) Create a new song
createSinger(input: CreateSingerInput!) Create a new singer
deleteComment(id: String!) Delete a specific comment
deleteMusic(id: String!) Delete a specific song
deleteSinger(id: String!) Delete a specific singer
login(input: LoginUserInput!) Log in a user
register(input: RegisterUserInput!) Register a new user
updateAlbum(input: UpdateAlbumInput!) Update an existing album
updateComment(input: UpdateCommentInput!) Update an existing comment
updateGenre(input: UpdateGenreInput!) Update an existing genre
updateMusic(input: UpdateMusicInput!) Update an existing song
updateSinger(input: UpdateSingerInput!) Update an existing singer

License

This project is licensed under the GPL-3 license. Contributions are welcome, please follow the guidelines in the CONTRIBUTING.md file.

Copyright 2023, Max Base

musictsgraphql's People

Contributors

basemax avatar dependabot[bot] avatar ehsan2003 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.