Building a Simple CRUD Service with Pylon

Nico Schett - Jul 15 - - Dev Community

In the dynamic world of web development, creating robust and scalable APIs is essential. The challenge lies in managing schema definitions and ensuring consistency as the application evolves. Pylon simplifies this process by automating GraphQL schema generation based on TypeScript functions. In this article, we’ll walk through building a simple CRUD (Create, Read, Update, Delete) service using Pylon.

What is Pylon?

Pylon is a tool that automates the generation of GraphQL schemas directly from TypeScript functions. It leverages TypeScript’s type annotations to create a GraphQL schema that is always in sync with your application logic. This eliminates manual schema definitions, reducing errors and improving maintainability.

Setting Up Pylon

Step 1: Install Pylon CLI

First, install the Pylon CLI globally using npm:

npm install -g @getcronit/pylon-cli
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a New Pylon Project

Initialize a new Pylon project:

pylon new my-crud-project
Enter fullscreen mode Exit fullscreen mode

Building the CRUD Service

We’ll create a simple service to manage a list of books. Each book will have an ID, title, author, and publication year.

Step 3: Define TypeScript Functions

In your Pylon project, define a class to manage the book records. Annotate the functions with Pylon directives to specify GraphQL queries and mutations.

import { defineService } from "@getcronit/pylon";

class BookStore {
  private books = [
    { id: 1, title: "1984", author: "George Orwell", year: 1949 },
    { id: 2, title: "Brave New World", author: "Aldous Huxley", year: 1932 },
  ];

  constructor() {
    this.getBook = this.getBook.bind(this);
    this.getBooks = this.getBooks.bind(this);
    this.addBook = this.addBook.bind(this);
    this.updateBook = this.updateBook.bind(this);
    this.deleteBook = this.deleteBook.bind(this);
  }

  getBook(id: number) {
    return this.books.find((book) => book.id === id);
  }

  getBooks() {
    return this.books;
  }

  addBook(title: string, author: string, year: number) {
    const id = this.books.length + 1;
    const newBook = { id, title, author, year };
    this.books.push(newBook);
    return newBook;
  }

  updateBook(id: number, title?: string, author?: string, year?: number) {
    const book = this.getBook(id);
    if (book) {
      if (title) book.title = title;
      if (author) book.author = author;
      if (year) book.year = year;
      return book;
    }
    return null;
  }

  deleteBook(id: number) {
    const book = this.getBook(id);
    if (book) {
      this.books = this.books.filter((b) => b.id !== id);
      return book;
    }
    return null;
  }
}

const store = new BookStore();

export default defineService({
  Query: {
    book: store.getBook,
    books: store.getBooks,
  },
  Mutation: {
    addBook: store.addBook,
    updateBook: store.updateBook,
    deleteBook: store.deleteBook,
  },
});
Enter fullscreen mode Exit fullscreen mode

In this example, BookStore manages the CRUD operations for books. The defineService function from Pylon defines GraphQL queries and mutations based on the methods of BookStore.

Step 4: Run the Development Server

Start the development server to launch your GraphQL API with the generated schema:

bun run develop
Enter fullscreen mode Exit fullscreen mode

Exploring Your CRUD API

With the development server running, you can interact with your API using GraphQL Playground or any other GraphQL client.

  • GraphQL Playground: Access at http://localhost:3000/graphql to run queries and mutations.
  • GraphQL Viewer: Visualize the schema at http://localhost:3000/viewer.

Example Queries and Mutations

Query: Get All Books

query {
  books {
    id
    title
    author
    year
  }
}
Enter fullscreen mode Exit fullscreen mode

Mutation: Add a New Book

mutation {
  addBook(title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925) {
    id
    title
    author
    year
  }
}
Enter fullscreen mode Exit fullscreen mode

Mutation: Update a Book

mutation {
  updateBook(id: 1, title: "Nineteen Eighty-Four", year: 1949) {
    id
    title
    author
    year
  }
}
Enter fullscreen mode Exit fullscreen mode

Mutation: Delete a Book

mutation {
  deleteBook(id: 2) {
    id
    title
    author
    year
  }
}
Enter fullscreen mode Exit fullscreen mode

Advanced Features

Pylon also supports advanced features such as:

  • Built-in Authentication and Authorization: Pylon provides built-in support for authentication and authorization, allowing you to secure your GraphQL API with ease. By integrating with ZITADEL, Pylon simplifies user management and access control.
  • Logging and Monitoring: Pylon integrates with Sentry to provide real-time error monitoring and alerting for your Pylon service. Check out the guide on logging and monitoring for more details.
  • Context Management: Pylon allows you to access the request context anywhere in your service functions, enabling you to implement custom logic based on the incoming request. Learn more about context management in the Pylon documentation.
  • Deployment: To deploy your Pylon service, follow the deployment guide in the official documentation to get your service up and running in production.

Conclusion

Explore Pylon to enhance your GraphQL development process and make your projects more efficient and scalable. For more information, check out the official documentation or the GitHub repository.

. . .
Terabox Video Player