⚙️ Backend

GraphQL API Development

Last updated: 2025-09-25 12:47:03

GraphQL API Development

GraphQL provides a flexible query language for APIs, allowing clients to request exactly the data they need.

Schema Definition

// schema.graphql
type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
  content: String!
  author: User!
  createdAt: String!
}

type Query {
  users: [User!]!
  user(id: ID!): User
  posts: [Post!]!
  post(id: ID!): Post
}

type Mutation {
  createUser(name: String!, email: String!): User!
  createPost(title: String!, content: String!, authorId: ID!): Post!
  updatePost(id: ID!, title: String, content: String): Post
  deletePost(id: ID!): Boolean!
}

type Subscription {
  postAdded: Post!
  userOnline(userId: ID!): User!
}

Resolvers with Node.js

const { ApolloServer, gql } = require('apollo-server-express');
const express = require('express');

// Mock data
let users = [
  { id: '1', name: 'John Doe', email: 'john@example.com' },
  { id: '2', name: 'Jane Smith', email: 'jane@example.com' }
];

let posts = [
  { id: '1', title: 'GraphQL Basics', content: 'Learning GraphQL...', authorId: '1', createdAt: new Date().toISOString() }
];

const resolvers = {
  Query: {
    users: () => users,
    user: (_, { id }) => users.find(user => user.id === id),
    posts: () => posts,
    post: (_, { id }) => posts.find(post => post.id === id),
  },
  
  Mutation: {
    createUser: (_, { name, email }) => {
      const newUser = {
        id: String(users.length + 1),
        name,
        email
      };
      users.push(newUser);
      return newUser;
    },
    
    createPost: (_, { title, content, authorId }) => {
      const newPost = {
        id: String(posts.length + 1),
        title,
        content,
        authorId,
        createdAt: new Date().toISOString()
      };
      posts.push(newPost);
      return newPost;
    },
    
    updatePost: (_, { id, title, content }) => {
      const post = posts.find(p => p.id === id);
      if (!post) throw new Error('Post not found');
      
      if (title !== undefined) post.title = title;
      if (content !== undefined) post.content = content;
      
      return post;
    },
    
    deletePost: (_, { id }) => {
      const index = posts.findIndex(p => p.id === id);
      if (index === -1) return false;
      
      posts.splice(index, 1);
      return true;
    },
  },
  
  User: {
    posts: (user) => posts.filter(post => post.authorId === user.id),
  },
  
  Post: {
    author: (post) => users.find(user => user.id === post.authorId),
  },
};

Client Queries

// Query example
query GetUserWithPosts($userId: ID!) {
  user(id: $userId) {
    id
    name
    email
    posts {
      id
      title
      content
      createdAt
    }
  }
}

// Mutation example
mutation CreatePost($title: String!, $content: String!, $authorId: ID!) {
  createPost(title: $title, content: $content, authorId: $authorId) {
    id
    title
    content
    author {
      name
    }
    createdAt
  }
}

// Subscription example
subscription {
  postAdded {
    id
    title
    author {
      name
    }
  }
}