.cursorrules TypeScript Apollo Backend

GraphQL + Apollo

Apollo ServerのGraphQL API開発cursorrules。スキーマ設計、リゾルバー、DataLoader、サブスクリプション、キャッシュのガイド。

.cursorrules · 82 lines
You are an expert GraphQL engineer building type-safe APIs with Apollo Server.

## Schema Design Principles

- GraphQL schema is a contract — never make breaking changes without versioning.
- Use descriptive field names: `createdAt` not `created`.
- Connections pattern for pagination: `UserConnection { edges { node cursor } pageInfo }`.
- Input types for mutations: `CreateUserInput`, `UpdateUserInput`.
- Enums for finite sets: `Status { ACTIVE INACTIVE PENDING }`.

## Schema Example

```graphql
type Query {
  user(id: ID!): User
  users(first: Int, after: String): UserConnection!
}

type Mutation {
  createUser(input: CreateUserInput!): CreateUserPayload!
  deleteUser(id: ID!): Boolean!
}

type User {
  id: ID!
  email: String!
  name: String
  posts(first: Int): PostConnection!
  createdAt: DateTime!
}

input CreateUserInput {
  email: String!
  name: String!
}

type CreateUserPayload {
  user: User!
  errors: [UserError!]!
}
```

## Resolvers

```typescript
const resolvers = {
  Query: {
    user: async (_, { id }, { dataSources }) => {
      return dataSources.userAPI.findById(id);
    },
  },
  User: {
    posts: async (user, args, { dataSources }) => {
      return dataSources.postAPI.findByUserId(user.id, args);
    },
  },
};
```

## DataLoader (N+1 Prevention)

```typescript
const userLoader = new DataLoader(async (userIds: string[]) => {
  const users = await db.user.findMany({
    where: { id: { in: userIds } },
  });
  // Return in same order as input IDs
  return userIds.map(id => users.find(u => u.id === id) ?? null);
});
```

## Error Handling

- Use `GraphQLError` with error codes.
- Payload pattern: `{ data, errors }` instead of throwing.
- Never expose internal errors to clients.

## Persisted Queries

- Enable for production to reduce request size.
- APQ (Automatic Persisted Queries) with `apollo-server`.
- `@apollo/client` handles APQ automatically.
Share on X

こちらもおすすめ

Backend カテゴリの他のルール

もっとルールを探す

CLAUDE.md、.cursorrules、AGENTS.md、Image Prompts の全 223 ルールをチェック。