Skip to content

Latest commit

 

History

History
144 lines (119 loc) · 10.7 KB

File metadata and controls

144 lines (119 loc) · 10.7 KB

Build and Deploy a GraphQL API using NodeJS

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to GraphQL and Tutorial Overview

  • Summary: The video focuses on building a GraphQL API from scratch using Node.js, emphasizing server-side fundamentals. It contrasts with client-side consumption and previous tutorials using StepZen for quick setups. The goal is to understand the underlying mechanics by creating a DIY API with Node.js, GraphQL HTTP, and MongoDB, then deploying to AWS EC2.
  • Key Takeaway/Example: GraphQL allows defining data shapes and querying specific fields, avoiding over-fetching like in REST APIs. For complex data, one GraphQL query can fetch related info (e.g., player, teams, matches) versus multiple REST endpoints.
  • Link for More Details: Ask AI: GraphQL Fundamentals

GraphQL Theory Basics

  • Summary: GraphQL describes data via types and uses resolvers to fetch it. Benefits include predictable results and single queries for nested data, unlike REST's multiple endpoints.
  • Key Takeaway/Example: Types define fields (e.g., Query with hello: String), resolvers are functions returning data (e.g., () => 'Hello World').
  • Link for More Details: Ask AI: GraphQL Types and Resolvers

Setting Up the Node.js Project and Basic Schema

  • Summary: Initialize a Node.js project, install GraphQL, and build a simple schema with a query returning hardcoded data.
  • Key Takeaway/Example: Use buildSchema for type definitions and root resolvers.
const schema = buildSchema(`
  type Query {
    hello: String
    age: Int
  }
`);
const root = { hello: () => 'Hello world!', age: () => 25 };

Serving GraphQL over HTTP with Express

  • Summary: Transform the script into an HTTP server using Express and graphql-http, mounting the endpoint at /graphql.
  • Key Takeaway/Example: Nodemon for auto-restarts; query via URL or tools.
const app = express();
app.use('/graphql', createHandler({ schema }));
app.listen(4000);

Using Ruru for API Exploration

  • Summary: Install and integrate Ruru (a GraphiQL variant) for visual querying and schema docs.
  • Key Takeaway/Example: Mount at root: app.get('/', ruruHTML({ endpoint: '/graphql' })); Use for named queries, variables, and schema refresh.
  • Link for More Details: Ask AI: Ruru GraphQL Interface

Basic Types, Lists, and Nullability

  • Summary: Cover scalar types (String, Int, Float, Boolean, ID), lists (arrays), and making fields non-nullable with !.
  • Key Takeaway/Example: Required array of required strings: [String!]!. Null values trigger errors if non-nullable.
  • Link for More Details: Ask AI: GraphQL Basic Types

Arguments and Variables

  • Summary: Fields can take arguments; use variables for reusability.
  • Key Takeaway/Example: Define arg in schema: hello(name: String!): String. Resolver: ({ name }) => \Hello ${name}`. Query with vars: query Hello($name: String!) { hello(name: $name) }`.
  • Link for More Details: Ask AI: GraphQL Arguments

Custom Object Types

  • Summary: Define custom types for complex data, nesting them for relationships.
  • Key Takeaway/Example: type User { id: Int, name: String }. Query: user: User. Resolver returns object.
  • Link for More Details: Ask AI: Custom Object Types

Switching to GraphQL Yoga

  • Summary: Move to Yoga for modular schema with resolvers per field.
  • Key Takeaway/Example: Use createSchema with typeDefs and resolvers object.
const typeDefs = gql`type Query { hello: String }`;
const resolvers = { Query: { hello: () => 'Hello' } };

Mutations and Input Types

  • Summary: Mutations for writes; inputs group args.
  • Key Takeaway/Example: input NewUserInput { name: String! } mutation createUser(user: NewUserInput!): User.
  • Link for More Details: Ask AI: GraphQL Mutations

MongoDB Integration with Atlas

  • Summary: Set up MongoDB Atlas, connect via driver, provide DB context to resolvers.
  • Key Takeaway/Example: Export collections in setup; access in resolvers: const { users } = context;.
const client = new MongoClient(uri);
const db = client.db('sample_mflix');

CRUD Operations for Users

  • Summary: Implement read (list/single), create, update, delete for users using MongoDB queries.
  • Key Takeaway/Example: Create: users.insertOne(userInput). Update: users.updateOne({ _id: new ObjectId(id) }, { $set: update }).
  • Link for More Details: Ask AI: User CRUD in GraphQL

Model Relationships

  • Summary: Connect models (e.g., comments to users) via resolvers fetching related data.
  • Key Takeaway/Example: In Comment resolver: user: (obj, _, { users }) => users.findOne({ email: obj.email }).
  • Link for More Details: Ask AI: GraphQL Relationships

Deployment Overview

  • Summary: Deploy to AWS EC2; use PM2 for process management. Refer to prior tutorial for steps.
  • Key Takeaway/Example: Run pm2 start server.js on EC2 instance.
  • Link for More Details: Ask AI: Deploying GraphQL to EC2

About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: