0

如何使用sequelize-typescript通过自定义 Apollo GraphQL 类型查询相关对象?我有以下没有打字稿的查询:

用户事件GraphQL 类型:

type User {
    id: Int
    login: String
    email: String
    role: Roles
    url_identity: String
}

type Event {
  id: Int
  event_name: String
  user: User
}

用户模型

import {HasOne, Model, Table} from "sequelize-typescript";
import Event from "./event.model";

@Table({
    timestamps: false,
    modelName: 'User',
})
export default class User extends Model {

    @HasOne(() => Event)
    event: Event
}

解析器:

static resolver() {
    return {
        Event: {
            user: event => event.$get('user')
    }
}

打字稿看不到event.$get('user')

4

1 回答 1

0

答案是将映射器添加到您的graphql-codegen配置中。您还可以为上下文传递接口(第三个参数):

schema: ./src/graphql/types/**/*.graphql
generates:
  ./src/graphql/resolvers-types.ts:
    config:
      useIndexSignature: true
      contextType: ./schema#IApolloServerContext
      mappers:
        Event: ../database/models/event.model#EventModel
        User: ../database/models/user.model#UserModel
    plugins:
      - typescript
      - typescript-resolvers

更多信息在这里:https ://graphql-code-generator.com/docs/plugins/typescript-resolvers#use-your-model-types-mappers

于 2021-01-12T17:48:21.943 回答