A GraphQL API server built with TypeScript and Node.js, designed to provide data about NASA's Mars Rovers. This project leverages GraphQL for efficient data querying and follows modern software design practices for scalability and maintainability.
Explore the wonders of Mars with this GraphQL server! ๐ญ
๐ LIVE SERVER: https://nasa-marsrovers-graphql-server.onrender.com/
- ๐งโ๐ป Features
- ๐จ Technology Stack
- ๐๏ธ Project Structure
- ๐ How to Use This Repo
- ๐ Example Queries
- ๐ฎ Future Improvements
- ๐ License
- Fetch a list of Mars Rover photos and Filter photos by Martian Sol or Earth Date.
- Retrieve detailed information about a specific photo.
- Increment the view count for a photo.
- Query data efficiently using GraphQL.
Technology | Purpose |
---|---|
GraphQL | Provides a flexible and efficient API for querying data. |
TypeScript | Ensures type safety and better developer experience. |
Node.js | Powers the server runtime. |
Apollo Server | Simplifies the implementation of the GraphQL API. |
GraphQL Code Generator | Automatically generates TypeScript types and resolvers. |
RESTDataSource | Used to interact with NASA's REST API. |
- API First / Schema First: The GraphQL schema is defined first, serving as the contract for the API and ensuring consistency between the client and server. This also enables parallel development between client and server.
- Type-Safe Development: TypeScript is used throughout the project to reduce runtime errors and improve code quality.
- Separation of Concerns: The project is modular, with clear separation between schema definitions, resolvers, and data sources.
- Code Generation: GraphQL Code Generator is used to generate types and resolvers, ensuring consistency between the schema and implementation.
- Environment Variables: Sensitive data like API keys are managed using
.env
files.
The project is organized as follows:
src/
โโโ api-contract/ # GraphQL schema and example queries
โโโ context.ts # Context definition for Apollo Server
โโโ datasources/ # Data sources for interacting with external APIs
โโโ models/ # TypeScript models for domain entities
โโโ resolvers/ # GraphQL resolvers implementation
โโโ schema.ts # Schema loading and executable schema creation
โโโ types.ts # Auto-generated TypeScript types for GraphQL
โโโ index.ts # Entry point for the server
src/schema.ts
: Loads the GraphQL schema and creates an executable schema.src/resolvers/resolvers.ts
: Implements the logic for GraphQL queries and mutations.src/datasources/nasa-mars-api.ts
: Handles communication with NASA's Mars Rover API.src/types.ts
: Contains TypeScript types generated from the GraphQL schema.
Follow these steps to set up and run the server:
- Navigate to the
server
folder:cd server
- Install dependencies:
npm install
Run the server in development mode with live reloading:
npm run dev
Start the server in production mode:
npm start
This will launch the GraphQL API server, ready to handle requests. ๐
Here are some example queries you can use to interact with the API:
query GetMarsPhotos {
marsPhotos {
id
sol
camera {
name
full_name
}
img_src
earth_date
number_of_views
rover {
name
status
}
}
}
query GetFilteredMarsPhotos($sol: String, $earth_date: String) {
marsPhotos(sol: $sol, earth_date: $earth_date) {
id
sol
img_src
earth_date
rover {
name
status
}
}
}
{
"sol": "1000",
"earth_date": "2023-09-01"
}
query GetMarsPhoto($id: ID!, $sol: String) {
marsPhoto(id: $id, sol: $sol) {
id
sol
img_src
earth_date
camera {
name
full_name
}
rover {
name
landing_date
launch_date
}
}
}
{
"id": "m_1",
"sol": "1000"
}
This query allows you to fetch a specific Mars photo by its ID and optionally filter it by the Martian sol.
mutation IncrementMarsPhotoViews($id: ID!) {
incrementMarsPhotoViews(id: $id) {
code
success
message
marsPhoto {
id
number_of_views
}
}
}
This query allows you to filter Mars photos by either the Martian sol or the Earth date. You can provide one or both filters as needed.
- Expand the API to include data from other NASA missions.
- Use a dedicated database (e.g., MongoDB) to store application-specific data.
- Add more filtering and sorting options for photos.
- Implement user authentication for personalized features.
This project is licensed under the MIT License. See the LICENSE
file for details.