Apollo Server GraphQL

Introduction

The Apollo GraphQL Server Template is a starter template designed to simplify the process of building GraphQL APIs using Apollo Server and Express. This template provides a foundational structure with sample data models for books and authors, making it easy to configure and extend for your own server logic.

Features

  • Apollo Server: A robust implementation of a GraphQL server.
  • Express Integration: Seamless integration with Express for middleware support.
  • Sample Data Models: Predefined models for books and authors to kickstart development.
  • Introspection Support: Built-in support for GraphQL introspection queries.
  • GraphQL Playground: Interactive API exploration interface.

Technologies Used

  • Node.js: JavaScript runtime for building scalable network applications.
  • Express: Web framework for Node.js.
  • Apollo Server: GraphQL server for Node.js.
  • TypeScript: Typed superset of JavaScript for improved development experience.
  • UUID: Library for generating unique identifiers.

Installation Steps

To utilize the Apollo GraphQL Server Template with Universal-Box, follow these steps:

  1. Initialize the project:
    universal-box init
    
  2. Select the Apollo GraphQL Server Template: Select the following from the menu:
     API > Apollo-Server-GraphQL
    
  3. Install dependencies: Navigate to your project directory and run:
    npm install
    
    # Compile TypeScript
    npm run build
    
    # Start the server
    npm start
    
  4. Access the GraphQL Playground your browser and navigate to http://localhost:4000/graphql.

Queries

Here are the available queries and mutations you can use to interact with the API:

Fetch All Authors

query {
  authors {
    id
    name
    dateOfBirth
  }
}

Fetch-All-Authors

Fetch a Specific Author by ID

query {
  author(id: "YOUR_AUTHOR_ID") {
    id
    name
    dateOfBirth
  }
}

Fetch-a-Specific-Author-by-ID

Fetch All Books

query {
  books {
    id
    title
    author {
      id
      name
    }
  }
}

Fetch-All-Books

Create a New Author

mutation {
  createAuthor(data: { name: "New Author", dateOfBirth: "1990-01-01" }) {
    id
    name
    dateOfBirth
  }
}

Create-a-New-Author

Create a New Book

mutation {
  createBook(data: { title: "New Book Title", authorId: "YOUR_AUTHOR_ID" }) {
    id
    title
    authorId
  }
}

Create-a-New-Book

Querying the Schema Directives

query {
  __schema {
    directives {
      name
      description
      locations
      args {
        name
        description
        type {
          kind
          name
          ofType {
            kind
            name
          }
        }
        defaultValue
      }
    }
  }
}

Querying-the-Schema-Directives

Visit codebase here