Logo di GraphQL API开发

GraphQL API开发

Organizzazione
microwind
GraphQL API开发

当开发GraphQL API时,分析查询策略,优化API性能,解决数据获取问题。验证GraphQL架构,设计查询模式,和最佳实践。

Panoramica

Pubblicato damicrowind
Repositoryai-skills
Nome della skillGraphQL API开发
Stelle
93
Fork
25
File inclusi
2
LicenzaMIT
Link
  • Istruzioni in Markdown

    Un file SKILL.md che il modello carica quando serve, consumando token solo quando una richiesta è effettivamente pertinente.

  • Funziona con qualsiasi LLM

    Le skill IA sono in semplice Markdown, non in codice specifico di un fornitore, quindi funzionano con GPT, Claude, Gemini, Grok o un modello locale.

  • File inclusi: 2

    Script, template e riferimenti che il modello può leggere mentre lavora. I file sono di sola lettura e non vengono mai eseguiti.

  • Open source

    Pubblicata da microwind su GitHub. Leggi i sorgenti prima di installarla.

Installazione

Installa la skill IA GraphQL API开发 in TypingMind per usarla con qualsiasi LLM, oppure copiala in un altro agente in grado di leggere SKILL.md.

1

Installa in TypingMind

TypingMind installa una skill direttamente dalla sua cartella GitHub: legge SKILL.md, raccoglie i file delle risorse e salva il risultato in locale.

  1. Apri l'app e vai su Plugin → Skill.
  2. Scegli "Installa da GitHub".
  3. Incolla l'URL della cartella della skill riportato qui sotto e conferma.
    https://github.com/microwind/ai-skills/tree/main/backend/graphql-api
  4. Attiva la skill in ogni chat in cui vuoi averla a disposizione.
Plugin → Skill → Aggiungi skill → Da URL GitHub, poi incolla l'URL della cartella e premi Continua.
2

Installa in un altro agente

Qualsiasi agente che legge il formato Agent Skills può usare questa skill: copia la cartella della skill nella directory delle skill di quell'agente.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/backend/graphql-api .claude/skills/microwind-graphql-api
Dopo la copia, riavvia Claude Code per caricare la nuova skill.

Usala in TypingMind

Attiva GraphQL API开发 in qualsiasi chat di TypingMind e al resto pensa il modello. Il nome e la descrizione della skill sono nel prompt di sistema e, non appena arriva una richiesta pertinente, il modello carica da solo le istruzioni complete: non devi mai richiamarla manualmente e non consuma token finché non viene effettivamente usata.

Il modello carica automaticamente GraphQL API开发 non appena riceve una richiesta pertinente.

Funziona con qualsiasi modello IA

Le skill IA sono semplici istruzioni in Markdown, non codice specifico di un fornitore, quindi GraphQL API开发 non è vincolata al modello per cui è stata scritta. Installala una sola volta in TypingMind e usala con GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama o un modello locale che esegui tu, sempre con le tue chiavi API.

  • Caricata solo quando serve

    Il prompt di sistema contiene solo il nome e la descrizione. Le istruzioni vengono recuperate alla prima richiesta pertinente, quindi una skill inutilizzata non costa nulla.

  • Cambia modello durante la chat

    Poiché la skill è composta da istruzioni e non da codice, continua a funzionare anche se cambi modello: il nuovo modello legge lo stesso file SKILL.md.

Istruzioni della skill

Questo è il contenuto di SKILL.md che il modello carica. Leggilo prima dell'installazione: una skill è un insieme di istruzioni che il tuo modello seguirà.

GraphQL API开发技能

概述

GraphQL是一种现代化的API查询语言,提供了比REST更灵活的数据获取方式。不当的GraphQL设计会导致性能问题、安全漏洞和复杂性增加。需要建立完善的GraphQL开发规范。

核心原则: 好的GraphQL设计应该高效、安全、易于维护。坏的GraphQL设计会导致N+1查询问题、性能下降和安全风险。

何时使用

始终:

  • 设计新GraphQL API时
  • 优化GraphQL查询性能时
  • 解决N+1查询问题时
  • 设计GraphQL schema时
  • 处理GraphQL安全问题时
  • 实现GraphQL订阅时

触发短语:

  • "GraphQL查询太慢了"
  • "如何设计GraphQL schema?"
  • "N+1查询问题怎么解决?"
  • "GraphQL安全怎么保证?"
  • "如何实现GraphQL订阅?"
  • "GraphQL和REST怎么选?"

GraphQL API技能功能

Schema设计分析

  • 类型定义设计
  • 查询和变更定义
  • 订阅类型设计
  • 接口和联合类型
  • 输入类型设计

查询优化策略

  • N+1查询问题解决
  • 数据加载器优化
  • 查询复杂度分析
  • 分页策略设计
  • 缓存机制实现

安全检查机制

  • 查询深度限制
  • 查询复杂度限制
  • 查询超时设置
  • 权限验证
  • 敏感数据保护

常见GraphQL设计问题

N+1查询问题

问题:
查询列表时,每个项目都触发额外的数据库查询

后果:
- 数据库压力大
- 响应时间长
- 性能线性下降
- 服务器资源浪费

解决方案:
- 数据加载器(DataLoader)
- 批量查询优化
- 预加载关联数据
- 查询合并策略

查询复杂度问题

问题:
客户端发送过于复杂的嵌套查询

后果:
- 服务器性能下降
- 内存占用过高
- 响应超时
- 拒绝服务攻击风险

解决方案:
- 查询深度限制
- 复杂度分析
- 查询超时设置
- 白名单字段限制

Schema设计问题

问题:
Schema设计过于复杂或不一致

后果:
- 开发困难
- 维护成本高
- 客户端困惑
- 版本管理困难

解决方案:
- 简化Schema设计
- 统一命名规范
- 模块化设计
- 版本控制策略

GraphQL Schema设计

基础Schema结构

graphql
# 用户类型
type User {
  id: ID!
  username: String!
  email: String!
  profile: Profile
  posts: [Post!]!
  createdAt: DateTime!
}

# 用户资料类型
type Profile {
  id: ID!
  firstName: String!
  lastName: String!
  avatar: String
  bio: String
}

# 文章类型
type Post {
  id: ID!
  title: String!
  content: String!
  author: User!
  comments: [Comment!]!
  tags: [String!]!
  createdAt: DateTime!
}

# 查询根类型
type Query {
  user(id: ID!): User
  users(limit: Int, offset: Int): [User!]!
  post(id: ID!): Post
  posts(limit: Int, offset: Int): [Post!]!
  search(query: String!): [SearchResult!]!
}

# 变更根类型
type Mutation {
  createUser(input: CreateUserInput!): User!
  updateUser(id: ID!, input: UpdateUserInput!): User!
  createPost(input: CreatePostInput!): Post!
  deletePost(id: ID!): Boolean!
}

# 订阅根类型
type Subscription {
  postCreated: Post!
  userUpdated(userId: ID!): User!
}

# 输入类型
input CreateUserInput {
  username: String!
  email: String!
  password: String!
  profile: ProfileInput
}

input ProfileInput {
  firstName: String!
  lastName: String!
  avatar: String
  bio: String
}

# 联合类型
union SearchResult = User | Post | Comment

# 枚举类型
enum UserRole {
  ADMIN
  MODERATOR
  USER
}

# 标量类型
scalar DateTime
scalar Upload

接口设计

graphql
# 可评论接口
interface Commentable {
  id: ID!
  comments: [Comment!]!
}

# 实现接口
type Post implements Commentable {
  id: ID!
  title: String!
  content: String!
  comments: [Comment!]!
}

type Video implements Commentable {
  id: ID!
  title: String!
  url: String!
  duration: Int!
  comments: [Comment!]!
}

代码实现示例

Python GraphQL实现

python
import graphene
from graphene_django import DjangoObjectType
from django.contrib.auth.models import User
from datetime import datetime
import promise

class ProfileType(graphene.ObjectType):
    id = graphene.ID()
    first_name = graphene.String()
    last_name = graphene.String()
    avatar = graphene.String()
    bio = graphene.String()

class UserType(DjangoObjectType):
    profile = graphene.Field(ProfileType)
    posts = graphene.List('PostType')
    
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'date_joined')
    
    def resolve_profile(self, info):
        if hasattr(self, 'profile'):
            return self.profile
        return None
    
    def resolve_posts(self, info):
        # 使用DataLoader解决N+1问题
        return info.context.loaders.post_loader.load(self.id)

class PostType(graphene.ObjectType):
    id = graphene.ID()
    title = graphene.String()
    content = graphene.String()
    author = graphene.Field(UserType)
    comments = graphene.List('CommentType')
    tags = graphene.List(graphene.String)
    created_at = graphene.DateTime()
    
    def resolve_author(self, info):
        return info.context.loaders.user_loader.load(self.author_id)
    
    def resolve_comments(self, info):
        return info.context.loaders.comment_loader.load(self.id)

class Query(graphene.ObjectType):
    user = graphene.Field(UserType, id=graphene.ID(required=True))
    users = graphene.List(UserType, limit=graphene.Int(), offset=graphene.Int())
    post = graphene.Field(PostType, id=graphene.ID(required=True))
    posts = graphene.List(PostType, limit=graphene.Int(), offset=graphene.Int())
    
    def resolve_user(self, info, id):
        return info.context.loaders.user_loader.load(int(id))
    
    def resolve_users(self, info, limit=None, offset=None):
        queryset = User.objects.all()
        if offset:
            queryset = queryset[offset:]
        if limit:
            queryset = queryset[:limit]
        return queryset
    
    def resolve_post(self, info, id):
        return info.context.loaders.post_loader.load(int(id))
    
    def resolve_posts(self, info, limit=None, offset=None):
        queryset = Post.objects.all()
        if offset:
            queryset = queryset[offset:]
        if limit:
            queryset = queryset[:limit]
        return queryset

class CreateUser(graphene.Mutation):
    user = graphene.Field(UserType)
    success = graphene.Boolean()
    errors = graphene.List(graphene.String)
    
    class Arguments:
        username = graphene.String(required=True)
        email = graphene.String(required=True)
        password = graphene.String(required=True)
        first_name = graphene.String()
        last_name = graphene.String()
    
    def mutate(self, info, username, email, password, first_name=None, last_name=None):
        try:
            user = User.objects.create_user(
                username=username,
                email=email,
                password=password
            )
            
            # 创建用户资料
            if first_name or last_name:
                Profile.objects.create(
                    user=user,
                    first_name=first_name,
                    last_name=last_name
                )
            
            return CreateUser(user=user, success=True, errors=[])
        
        except Exception as e:
            return CreateUser(user=None, success=False, errors=[str(e)])

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()

# DataLoader实现
from promise import Promise
from promise.dataloader import DataLoader

class UserLoader(DataLoader):
    def batch_load_fn(self, keys):
        users = User.objects.filter(id__in=keys)
        user_dict = {user.id: user for user in users}
        return Promise.resolve([user_dict.get(key) for key in keys])

class PostLoader(DataLoader):
    def batch_load_fn(self, keys):
        posts = Post.objects.filter(author_id__in=keys)
        posts_dict = {}
        for post in posts:
            posts_dict.setdefault(post.author_id, []).append(post)
        return Promise.resolve([posts_dict.get(key, []) for key in keys])

# 查询复杂度限制
from graphql.validation.rules import QueryComplexityRule
from graphql import GraphQLSchema, validate

class QueryComplexityLimiter:
    def __init__(self, max_complexity=100):
        self.max_complexity = max_complexity
    
    def analyze_query(self, query, schema):
        complexity = 0
        
        def calculate_complexity(node, key=None):
            nonlocal complexity
            if hasattr(node, 'selection_set'):
                for selection in node.selection_set.selections:
                    complexity += 1
                    calculate_complexity(selection)
        
        try:
            import graphql
            ast = graphql.parse(query)
            calculate_complexity(ast)
        except Exception:
            pass
        
        return complexity <= self.max_complexity

# GraphQL视图
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
import json

@csrf_exempt
def graphql_view(request):
    if request.method == 'GET':
        return JsonResponse({'message': 'GraphQL API'})
    
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            query = data.get('query')
            variables = data.get('variables', {})
            
            # 查询复杂度检查
            complexity_limiter = QueryComplexityLimiter(max_complexity=100)
            if not complexity_limiter.analyze_query(query, schema):
                return JsonResponse({
                    'errors': [{'message': 'Query complexity too high'}]
                }, status=400)
            
            # 设置DataLoader
            loaders = {
                'user_loader': UserLoader(),
                'post_loader': PostLoader(),
                'comment_loader': CommentLoader()
            }
            
            context = {'loaders': loaders}
            result = schema.execute(query, variables=variables, context=context)
            
            return JsonResponse({
                'data': result.data,
                'errors': [str(error) for error in result.errors] if result.errors else None
            })
        
        except Exception as e:
            return JsonResponse({
                'errors': [{'message': str(e)}]
            }, status=500)

schema = graphene.Schema(query=Query, mutation=Mutation)

JavaScript Apollo Client实现

javascript
import { ApolloClient, InMemoryCache, gql, createHttpLink } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';

// HTTP链接
const httpLink = createHttpLink({
  uri: 'http://localhost:8000/graphql/',
});

// 认证链接
const authLink = setContext((_, { headers }) => {
  const token = localStorage.getItem('authToken');
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : '',
    }
  }
});

// Apollo客户端
const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
  defaultOptions: {
    watchQuery: {
      errorPolicy: 'all',
    },
    query: {
      errorPolicy: 'all',
    },
  },
});

// GraphQL查询
const GET_USER = gql`
  query GetUser($id: ID!) {
    user(id: $id) {
      id
      username
      email
      profile {
        firstName
        lastName
        avatar
      }
      posts {
        id
        title
        content
        createdAt
      }
    }
  }
`;

const GET_USERS = gql`
  query GetUsers($limit: Int, $offset: Int) {
    users(limit: $limit, offset: $offset) {
      id
      username
      email
      profile {
        firstName
        lastName
      }
    }
  }
`;

const CREATE_USER = gql`
  mutation CreateUser($input: CreateUserInput!) {
    createUser(input: $input) {
      id
      username
      email
      profile {
        firstName
        lastName
      }
    }
  }
`;

// 查询函数
export const getUser = async (id) => {
  try {
    const response = await client.query({
      query: GET_USER,
      variables: { id },
    });
    return response.data.user;
  } catch (error) {
    console.error('获取用户失败:', error);
    throw error;
  }
};

export const getUsers = async (variables = {}) => {
  try {
    const response = await client.query({
      query: GET_USERS,
      variables,
    });
    return response.data.users;
  } catch (error) {
    console.error('获取用户列表失败:', error);
    throw error;
  }
};

export const createUser = async (input) => {
  try {
    const response = await client.mutate({
      mutation: CREATE_USER,
      variables: { input },
    });
    return response.data.createUser;
  } catch (error) {
    console.error('创建用户失败:', error);
    throw error;
  }
};

// React Hook
import { useQuery, useMutation } from '@apollo/client';

export const useUser = (id) => {
  const { loading, error, data } = useQuery(GET_USER, {
    variables: { id },
    skip: !id,
  });
  
  return {
    user: data?.user,
    loading,
    error,
  };
};

export const useCreateUser = () => {
  const [createUserMutation, { loading, error }] = useMutation(CREATE_USER, {
    update: (cache, { data: { createUser } }) => {
      // 更新缓存
      cache.modify({
        fields: {
          users: (existing = []) => [...existing, createUser]
        }
      });
    },
  });
  
  const createUser = async (input) => {
    const result = await createUserMutation({
      variables: { input },
    });
    return result.data.createUser;
  };
  
  return {
    createUser,
    loading,
    error,
  };
};

// 分页Hook
export const useUsersPagination = (pageSize = 10) => {
  const [page, setPage] = useState(0);
  const [users, setUsers] = useState([]);
  
  const { loading, error, data, fetchMore } = useQuery(GET_USERS, {
    variables: {
      limit: pageSize,
      offset: page * pageSize,
    },
  });
  
  useEffect(() => {
    if (data?.users) {
      if (page === 0) {
        setUsers(data.users);
      } else {
        setUsers(prev => [...prev, ...data.users]);
      }
    }
  }, [data, page]);
  
  const loadMore = () => {
    if (!loading && data?.users?.length === pageSize) {
      setPage(prev => prev + 1);
    }
  };
  
  return {
    users,
    loading,
    error,
    loadMore,
    hasMore: data?.users?.length === pageSize,
  };
};

性能优化策略

DataLoader优化

  1. 批量查询: 将多个单独查询合并为批量查询
  2. 缓存机制: 在请求范围内缓存查询结果
  3. 预加载: 根据访问模式预加载相关数据
  4. 延迟加载: 按需加载数据,减少不必要查询

查询优化

  1. 字段选择: 只查询需要的字段
  2. 分页限制: 限制返回数据量
  3. 查询分析: 分析查询性能瓶颈
  4. 索引优化: 优化数据库索引

安全最佳实践

查询限制

  1. 深度限制: 限制查询嵌套深度
  2. 复杂度限制: 限制查询复杂度
  3. 超时设置: 设置查询执行超时
  4. 频率限制: 限制查询频率

权限控制

  1. 字段级权限: 控制字段访问权限
  2. 类型级权限: 控制类型访问权限
  3. 操作权限: 控制查询、变更、订阅权限
  4. 数据过滤: 过滤敏感数据

相关技能

  • api-validator - API接口验证和设计
  • database-query-analyzer - 数据库查询性能分析
  • security-scanner - 安全漏洞扫描
  • caching-strategies - 缓存策略和实现

File inclusi

Il modello legge questi file quando servono, mentre la skill è caricata. Sono accessibili in sola lettura e non vengono mai eseguiti.

Domande frequenti

Cosa fa la skill IA GraphQL API开发?

当开发GraphQL API时,分析查询策略,优化API性能,解决数据获取问题。验证GraphQL架构,设计查询模式,和最佳实践。

Perché usare GraphQL API开发 su TypingMind?

Perché la installi una volta e la usi con qualsiasi modello. GraphQL API开发 è scritta in semplice Markdown anziché in codice specifico di un fornitore, quindi la stessa skill funziona con GPT-5, Claude, Gemini, Grok o un modello locale, e puoi cambiare modello durante la chat senza che la skill smetta di funzionare. TypingMind usa le tue chiavi API, quindi paghi direttamente i fornitori anziché un abbonamento per posto utente, e le tue skill e chat restano nel tuo spazio di archiviazione.

Come installo GraphQL API开发 in TypingMind?

Apri Plugin → Skill → Installa da GitHub in TypingMind e incolla https://github.com/microwind/ai-skills/tree/main/backend/graphql-api. TypingMind legge il file SKILL.md, include i file associati e installa la skill, che puoi abilitare per ogni singola chat.

Quali modelli IA possono usare GraphQL API开发?

Qualsiasi modello che colleghi a TypingMind. Le skill IA sono semplici istruzioni in Markdown anziché codice specifico di un fornitore, quindi GPT, Claude, Gemini, Grok e i modelli locali possono tutti caricare questa skill quando una richiesta corrisponde al suo scopo.

Quanti modelli IA posso usare con GraphQL API开发?

Quanti ne vuoi. Se un modello supporta le skill, puoi usarlo con GraphQL API开发: GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama e molti altri, tutti su TypingMind con le tue chiavi API.

La skill IA GraphQL API开发 è gratuita?

Sì. È pubblicata su GitHub da microwind con licenza MIT. Paghi solo il tuo fornitore IA per i token che usi.

Cosa sono le skill IA?

Una skill IA è un pacchetto di istruzioni riutilizzabili che insegna a un modello IA come svolgere un'attività specifica. Segue il formato aperto Agent Skills: un file SKILL.md con un nome e una descrizione, più eventuali script, template o file di riferimento di cui il modello potrebbe aver bisogno. Il modello legge le istruzioni solo quando la tua richiesta corrisponde alla skill, quindi una skill installata non comporta costi finché non viene usata.

In cosa si differenziano le skill IA dai plugin o dai server MCP?

Un plugin o un server MCP fornisce a un modello nuovi strumenti da richiamare: codice che viene eseguito da qualche parte e restituisce un risultato. Una skill IA, invece, fornisce al modello conoscenze e procedure: come affrontare un'attività, quali passaggi seguire e quali caratteristiche deve avere un buon output. Le skill sono semplici istruzioni in Markdown, quindi non richiedono server, chiavi API né ambienti di esecuzione e funzionano con qualsiasi modello.

Elenco completo

Crea subito il tuo spazio di lavoro AI

Iscriviti alla nostra newsletter per ricevere aggiornamenti sulle nuove funzionalità e sulle prossime iniziative a premi 👇