A Node.js GraphQL API for managing products. This project allows users to perform CRUD operations on a MongoDB database using GraphQL.
- Features
- Getting Started
- Installation
- Usage
- API Endpoints
- Project Structure
- Dependencies
- Contributing
- License
- CRUD functionality for products (Create, Read, Update, Delete).
- GraphQL queries and mutations for managing product data.
- MongoDB integration with Mongoose.
- Built with Apollo Server and GraphQL.
Ensure you have the following installed:
-
Clone the repository:
git clone https://github.com/solarluiso/graphql-products-api.git cd graphql-products-api
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and add the following environment variables:MONGO_URI=your_mongodb_connection_string PORT=4000
Start the server in development mode with hot-reloading:
npm run dev
Or, start the server in production mode:
npm start
The server will run at http://localhost:4000
.
This API uses GraphQL. Use /graphql
to query and mutate data. Below are some examples:
-
Get all products:
query { products { id title category price inStock } }
-
Get a single product by ID:
query { product(id: "1") { title category price } }
-
Create a product:
mutation { createProduct( title: "Laptop" category: "Electronics" price: 999.99 inStock: true ) { id title } }
-
Update a product:
mutation { updateProduct(id: "1", price: 799.99) { id title price } }
-
Delete a product:
mutation { deleteProduct(id: "1") }
src/models
: Contains the Mongoose schemas.src/resolvers
: Includes GraphQL resolvers for handling queries and mutations.src/schema
: Defines the GraphQL schema.src/server.js
: Entry point for the application.
Contributions are welcome! Feel free to submit a pull request or open an issue.
This project is licensed under the ISC License.