deno-graphql-backend

sail-sail's avatarfrom sail-sail

创建 Deno GraphQL 后端接口的完整开发指南。包含三层架构(GraphQL/Resolver/Service)、DAO 函数使用、事务处理、认证权限、错误处理。当需要创建、修改后端 API 接口时使用此技能。

⭐11stars🔀5forks📁View on GitHub🕐Updated Dec 30, 2025

When & Why to Use This Skill

This Claude skill serves as a comprehensive development guide for building robust Deno-based GraphQL backends. It streamlines the creation and modification of API interfaces by enforcing a standardized three-layer architecture (GraphQL, Resolver, and Service), ensuring consistent implementation of database transactions, authentication, and error handling. By providing ready-to-use templates and core architectural rules, it helps developers maintain high code quality and accelerate the backend development lifecycle in Deno environments.

Use Cases

  • Rapidly scaffolding new GraphQL API endpoints with predefined templates for Schema, Resolvers, and Services.
  • Implementing complex business logic with built-in support for database transactions and multi-layer validation.
  • Standardizing backend architecture across a Deno project to improve maintainability and team collaboration.
  • Configuring authentication permissions and custom error handling for secure and user-friendly API responses.
  • Refactoring legacy backend code into a structured DAO-based service layer for better scalability.
namedeno-graphql-backend
descriptionDeno GraphQL 后端接口的完整开发指南. 当需要创建、修改后端 API 接口时使用此技能
compatibilityRequires Deno runtime
version"1.0"

Deno GraphQL 后端开发

三层架构

src/{mod}/{table}/
├── {table}.graphql.ts   # Schema 定义
├── {table}.model.ts     # Model 定义(可选)
├── {table}.resolver.ts  # 参数处理、事务/认证设置
└── {table}.service.ts   # 业务逻辑、调用 DAO

快速模板

1. GraphQL Schema

import { defineGraphql } from "/lib/context.ts";
import * as resolver from "./{table}.resolver.ts";

defineGraphql(resolver, /* GraphQL */ `
  
  type {Table}Model {
    id: {Table}Id!
    lbl: String!
  }
  
  input {Table}Input {
    lbl: String!
  }
  
  input {Table}Search {
    lbl: String
  }
  
  type Query {
    "查询"
    get{Table}(id: {Table}Id!): {Table}Model
    "列表"
    findAll{Table}(search: {Table}Search): [{Table}Model!]!
  }
  
  type Mutation {
    "创建"
    create{Table}(input: {Table}Input!): {Table}Id!
    "更新"
    update{Table}(id: {Table}Id!, input: {Table}Input!): Boolean!
    "删除"
    delete{Table}(ids: [{Table}Id!]!): Int!
  }
`);

2. Resolver

import {
  useContext,
} from "/lib/context.ts";

import type {
  {Table}Id,
  {Table}Input,
  {Table}Search,
} from "/gen/types.ts";

// 查询 - 不需要事务
export async function get{Table}(
  id: {Table}Id,
) {
  
  const {
    get{Table},
  } = await import("./{table}.service.ts");
  
  return await get{Table}(id);
}

// 修改 - 需要事务
export async function create{Table}(input: {Table}Input) {
  
  const {
    create{Table},
  } = await import("./{table}.service.ts");
  
  const context = useContext();
  context.is_tran = true;  // 开启事务
  
  return await create{Table}(input);
}

3. Service

import {
  isEmpty,
} from "/lib/util/string_util.ts";

import {
  findById{Table},
  create{Table},
} from "/gen/{mod}/{table}/{table}.dao.ts";

import type {
  {Table}Id,
  {Table}Input
} from "/gen/types.ts";

export async function create{Table}(
  input: {Table}Input,
) {
  if (isEmpty(input.lbl)) {
    throw "名称不能为空";  // 业务错误用中文
  }
  return await create{Table}(input);
}

4. 注册模块

在 src/{mod}/graphql.ts 添加:

import "./{table}/{table}.graphql.ts";

核心规则

规则 说明
事务 增删改 context.is_tran = true,查询不设置
认证 默认需登录,公开接口设 context.notVerifyToken = true
错误 throw "中文提示" 业务错误,throw new Error() 系统错误
DAO 从 gen/{mod}/{table}/{table}.dao.ts 导入

详细参考