gpt4 book ai didi

graphql - Nestjs/GraphQL - Playground 返回查询的空错误。我的解析器?

转载 作者:行者123 更新时间:2023-12-02 16:58:55 26 4
gpt4 key购买 nike

我浏览器中的 Playground 很好地显示了 Nestjs 创建的模式,但查询返回 null。我的代码有问题吗?

"errors": [
{
"message": "Cannot return null for non-nullable field Query.getUsers.",
"locations": [
{
"line": 2,
"column": 3
}

这意味着没有找到数据。

schema.graphql:

type UsersGQL {
User_id: ID!
first_name: String!
last_name: String!
main_skill_title: String!
user_name: String!
....
}

type Query {
getUser(user_id: ID!): UsersGQL!
getUsers: [UsersGQL!]!
}

在 Nestjs 中使用 GraphQL 编译为 graphql.schema.ts

export class UsersGQL {
user_id: string;
first_name: string;
last_name: string;
main_skill_title: string;
user_name: string;
...
}

export abstract class IQuery {
abstract getUser(user_id: string): UsersGQL | Promise<UsersGQL>;

abstract getUsers(): UsersGQL[] | Promise<UsersGQL[]>;

abstract temp__(): boolean | Promise<boolean>;
}

用户.resolvers.ts

import { Query, Resolver } from '@nestjs/graphql';
import { UsersService } from './users.service';

import { UsersGQL } from '../graphql.schema';
// import { UsersDTO } from './users.dto';


@Resolver('UsersGQL')
export class UsersResolvers {
constructor(
private readonly userService: UsersService
) {}

@Query()
async getUsers() {
return await this.userService.findAll();
}
}

该服务适用于我的 Nestjs REST API。数据库是 Postgres。

用户.服务.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, getManager, getRepository } from 'typeorm';
import { Members } from './members.entity';

@Injectable()
export class UsersService {

private entityManager = getManager();

constructor(
@InjectRepository(Users)
private readonly usersRepository: Repository<Users>
) {}

async findAll(): Promise<Users[]> {
return await this.usersRepository.find();
}
}

Playground 查询:

{
getUsers {
first_name
last_name
}
}

Playground 返回的错误:

{
"errors": [
{
"message": "Cannot return null for non-nullable field Query.getUsers.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"getUsers"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
...
],
"data": null
}

编辑 - 添加了 users.module.ts、app.module.ts 和 ormconfig.json。整个模块是延迟加载的。 REST 和 GraphQL 在模块中并列。我还分离了 REST 和 GQL 组件。

用户.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

// REST
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { Users } from './users.entity';

// GraphQL
import { UsersResolvers } from './users.resolvers';

@Module({
imports: [
TypeOrmModule.forFeature([
Users
]),
],
providers: [
UsersService,
UsersResolvers
],
controllers: [UsersController],
})

export class UsersModule {}

应用程序模块.ts

import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';

import { TypeOrmModule } from '@nestjs/typeorm';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';

import { LoggerMiddleware } from './logger.middleware';

import { UsersModule } from './users/users.module';

import { UsersController } from './users/users.controller';



@Module({
imports: [
TypeOrmModule.forRoot(),
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
definitions: {
path: join(process.cwd(), 'src/graphql.schema.ts'),
outputAs: 'class',
},
debug: true,
}),
UsersModule
],
controllers: [
],
exports: [
],
providers: [
]
})

export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(LoggerMiddleware)
.with('AppModule')
.forRoutes(
UsersController
)};
}

ormconfig.json

...
"entities": [
"src/**/**.entity{.ts,.js}",
// "src/graphql.schema.ts" This doesn't work. Must use REST entity.
],
...

最佳答案

您可能从 '@nestjs/common' 而不是 '@nestjs/graphql' 导入了 @Query

确保具备:

import { Query } from '@nestjs/graphql';

关于graphql - Nestjs/GraphQL - Playground 返回查询的空错误。我的解析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55068946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com