Skip to content

Commit

Permalink
Merge pull request #38 from BrockAltug/feature
Browse files Browse the repository at this point in the history
Feature
  • Loading branch information
BrockAltug authored Jan 12, 2025
2 parents 026690c + defa464 commit 5dead0d
Show file tree
Hide file tree
Showing 14 changed files with 11,624 additions and 103 deletions.
190 changes: 155 additions & 35 deletions 17-mern/06-typedefs-resolvers/README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,165 @@
# 🏗️ Update Professors to Include Their Classes

Work with a partner to implement the following user story:

* As a developer, I want to be able to query for professors and their classes at the same time.

## Acceptance Criteria

* It's done when a query in the Apollo Sandbox is able to return data similar to the following:

```json
"professors": [
{
"name": "Rebecca Watts",
"classes": [
{
"name": "Introduction to Computational Thinking"
},
{
"name": "User Interface Design"
},
{
"name": "Internet Architecture and Protocols"
}
]
}
]
```
# MERN Typedefs and Resolvers

## Overview

This project demonstrates how to build and implement a GraphQL API using the MERN stack. It focuses on creating and using typedefs and resolvers for managing a relational data structure involving schools, classes, and professors.

---

## Key Features

1. **GraphQL API**:

- Implements a GraphQL API using `ApolloServer`.
- Defines queries to fetch data for schools, classes, and professors.

2. **Typedefs**:

- Defines the data schema using GraphQL syntax.
- Supports relational fields for nested queries.

3. **Resolvers**:

- Implements resolver functions to handle GraphQL queries.
- Uses MongoDB models to fetch data and populate nested relationships.

4. **Database Integration**:
- Connects to a MongoDB database to store and retrieve data.
- Supports nested population of related documents.

---

## 💡 Hints
## Setup Instructions

1. **Install Dependencies**:

```bash
npm install
```

2. **Start the Database**:

* What back-end components make up a complete GraphQL schema?
- Ensure MongoDB is running on your local machine or cloud.
- Update the connection string in the `config/connection.js` file if necessary.

3. **Start the Server**:

```bash
npm run develop
```

4. **Access the API**:
- Open [http://localhost:3001/graphql](http://localhost:3001/graphql) in your browser or a GraphQL client to interact with the API.

---

## 🏆 Bonus
## Application Structure

If you have completed this activity, work through the following challenge with your partner to further your knowledge:
### **Server Configuration** (`server.js`)

- Sets up the Express server.
- Integrates `ApolloServer` with typedefs and resolvers.
- Exposes a `/graphql` endpoint for API interactions.

### **Typedefs** (`schemas/typeDefs.js`)

- Defines the GraphQL schema for:
- **School**: Includes fields like `name`, `location`, and a list of classes.
- **Class**: Includes fields like `name`, `building`, and the professor teaching the class.
- **Professor**: Includes fields like `name`, `officeHours`, and a list of classes.

Example:

```graphql
type School {
_id: ID
name: String
location: String
studentCount: Int
classes: [Class]
}
```

### **Resolvers** (`schemas/resolvers.js`)

- Implements the logic for fetching data as per the GraphQL schema.
- Uses Mongoose models to query the database and populate nested relationships.

Example:

```javascript
const resolvers = {
Query: {
schools: async () => {
return await School.find({}).populate("classes").populate({
path: "classes",
populate: "professor",
});
},
classes: async () => {
return await Class.find({}).populate("professor");
},
professors: async () => {
return await Professor.find({}).populate("classes");
},
},
};
```

---

## Example Queries

1. **Fetch All Schools**:

```graphql
query {
schools {
name
location
studentCount
classes {
name
creditHours
}
}
}
```

2. **Fetch All Classes**:

```graphql
query {
classes {
name
building
creditHours
professor {
name
officeHours
}
}
}
```

3. **Fetch All Professors**:
```graphql
query {
professors {
name
officeLocation
classes {
name
building
}
}
}
```

---

* What other types can be used in a GraphQL schema?
## Summary

Use [Google](https://www.google.com) or another search engine to research this.
This project demonstrates how to effectively use GraphQL typedefs and resolvers with the MERN stack to create a relational API. It covers key concepts like nested population, schema definition, and efficient querying using MongoDB and Apollo Server.

---
© 2024 edX Boot Camps LLC. Confidential and Proprietary. All Rights Reserved.
Loading

0 comments on commit 5dead0d

Please sign in to comment.