Skip to content

MohiFayden/mock-graphql-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mock GraphQL Server

This project is a simple mock GraphQL server built with Node.js and Apollo Server. It allows you to experiment with GraphQL queries, mutations, and advanced concepts like fragments, inline fragments, and unions. The server includes a mock dataset of books and authors, making it easy to test GraphQL functionalities.

Getting Started

Prerequisites

  • Node.js: Make sure you have Node.js installed on your computer. You can download it from Node.js Official Site.

Installation

  1. Clone the Repository:

    git clone git@github.com:MohiFayden/mock-graphql-server.git
    cd mock-graphql-server
  2. Install Dependencies:

    npm install

Running the Server

To start the server, run:

node index.js

By default, the server will run on http://localhost:4000.

Accessing the GraphQL Playground

Once the server is running, you can open the GraphQL Playground by navigating to:

http://localhost:4000

Available Queries and Mutations

Queries

  • Get All Books:
    query {
      books {
        id
        title
        genre
        price
        author {
          name
        }
      }
    }
  • Get All Authors:
    query {
      authors {
        id
        name
        bio
        books {
          title
        }
      }
    }
  • Search Books and Authors:
    query {
      search(keyword: "Fantasy") {
        ...on Book {
          title
          price
        }
        ...on Author {
          name
          bio
        }
      }
    }

Mutations

  • Add a Book:
    mutation {
      addBook(title: "The Hobbit", authorId: "3", genre: "Fantasy", price: 19.99) {
        id
        title
      }
    }
  • Update a Book:
    mutation {
      updateBook(id: "1", title: "Updated Title", price: 29.99) {
        id
        title
        price
      }
    }
  • Delete a Book:
    mutation {
      deleteBook(id: "2")
    }

Modifying the Schema

The schema is defined in typeDefs.js and can be easily modified to include additional types, fields, or mutations.

Troubleshooting

  • Port Already in Use: If you encounter an error about the port being in use, make sure no other server is running on port 4000 or modify the port in index.js.

  • GraphQL Playground Not Loading: Ensure the server is running without errors and that you’re accessing the correct URL (http://localhost:4000).

License

This project is open-source and available under the MIT License.

Author

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published