@ granjs/ schema (英语)
GraphQL 计划并为 Grant 生成类型 .
概览
此套件包含:
- 图QL 计划定义
- 生成类型脚本
- 图QL 操作和解析器
- 其他特定平台类型
安装
npm install @grantjs/schema
使用量
基本类型
import { User, Role, Permission, Organization, Project } from '@grantjs/schema';
const user: User = {
id: '1',
email: '[email protected]',
// ... other properties
};
图QL 操作
import { GetUsersQuery, CreateUserMutation } from '@grantjs/schema';
// Use in your GraphQL operations
const GET_USERS = gql`
query GetUsers {
users {
id
email
roles {
id
name
}
}
}
`;
解析器
import { Resolvers } from '@grantjs/schema';
const resolvers: Resolvers = {
Query: {
users: () => {
// Your resolver implementation
},
},
};
发展
生成类型
# Generate types from GraphQL schema
pnpm generate
# Watch for changes and regenerate
pnpm dev
在下方生成输出 src/generated/ 承诺,必须与其
来源。 CI 运行 pnpm codegen:check (再生成,在任何 diff 上失败),所以编辑
.graphql 文档或 codegen.ts 从来没有 src/generated/ 直接说.
测试
pnpm test
对合并的SDL的结构性断言:它构建,每个操作文件
和无法接触到的种类数 Query编号 :Mutation
被钉住。 见 src/sdl-contract.test.ts。 。 。 .
构建软件包
pnpm build
Schema 结构
src/
├── schema/ # GraphQL SDL — one directory per domain, each with
│ │ # inputs/, types/ and (where the domain is on the graph)
│ │ # queries/ and mutations/
│ ├── base.graphql # Scalars, shared interfaces, Tenant/SortOrder, Scope
│ └── root.graphql # Query/Mutation roots the domains extend
├── operations/ # Client operation documents, one directory per domain
├── generated/ # Codegen output — committed, never hand-edited
│ ├── schema-types.ts # Schema types (owns every type name)
│ ├── graphql.ts # Operation types + typed document nodes
│ └── resolvers.ts # Resolver types; imports from schema-types
├── events/ # Event catalog and envelope types
├── cdm/, notifications/, # Hand-written contract types that are not codegen'd
│ webhooks/
└── index.ts # Public barrel
apps/api 装入每个文件 src/schema/ 输入 makeExecutableSchema这样
任何船只在船上都声明 是否收到询问.
捐款
修改 GraphQL 方案时:
- 更新相关的
.graphql文件在src/schema/ - 运行
pnpm generate重新生成类型 - 更新任何受影响的解析器或操作
- 测试更改
许可证
麻省理工学院执照 - 看 自由主义 详细情况.