NestJS和GraphQL-TypeError:graphql_1.parse不是设置最少的函数

时间:2018-09-28 20:11:53

标签: javascript typescript graphql apollo-server nestjs

我开始用Nest构建我的第一个API。 我正尝试添加GraphQL并在开始时遇到问题。

TypeError: graphql_1.parse is not a function
    at Object.<anonymous> (C:\Users\marek\dev\eparafie\api-nest\node_modules\apollo-server-core\node_modules\graphql-tools\src\stitching\introspectSchema.ts:7:48)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\marek\dev\eparafie\api-nest\node_modules\apollo-server-core\node_modules\graphql-tools\src\stitching\index.ts:2:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)

到目前为止,我只有UsersModule,并且在其文件夹中有users.graphql文件:

type Mutation {
    createUser(createUserInput: CreateUserInput): User
}

type User {
    id: Int
    email: String
    password: String
}
input CreateUserInput {
    email: String
    password: String
}

我也有简单的解析器:

import { UserService } from './user.service';
import { Mutation, Resolver } from '@nestjs/graphql';
import { Body } from '@nestjs/common';
import { CreateUserDto } from '../dto/CreateUserDto';

@Resolver('User')
export class UserResolver {
  constructor(
    private readonly userService: UserService,
  ) {}

  @Mutation()
  async createUser(@Body() createUserDto: CreateUserDto) {
    return await this.userService.create(createUserDto);
  }
}

我的app.module.ts:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GraphQLModule } from '@nestjs/graphql';
import { Connection } from 'typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserService } from './user/user.service';
import { RolesService } from './auth/roles.service';
import { MembershipsService } from './auth/memberships.service';
import { UserModule } from './user/user.module';
import { join } from 'path';

@Module({
  imports: [
    TypeOrmModule.forRoot(),
    GraphQLModule.forRoot({
      typePaths: ['./**/*.graphql'],
      definitions: {
        path: join(process.cwd(), 'src/graphql.ts'),
      },
    }),
    UserModule,
  ],
  controllers: [AppController],
  providers: [AppService,
    UserService,
    RolesService,
    MembershipsService,
  ],
})
export class AppModule {
  constructor(private readonly connection: Connection) {}
}

UserModule,如果重要的话:

import { Module } from '@nestjs/common';
import { User } from './user.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserService } from './user.service';
import { UserResolver } from './user.resolver';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  providers: [UserService, UserResolver],
  controllers: [],
})
export class UserModule {}

依赖版本:

"dependencies": {
    "@nestjs/common": "^5.3.9",
    "@nestjs/core": "^5.3.10",
    "@nestjs/graphql": "^5.4.0",
    "@nestjs/typeorm": "^5.2.2",
    "apollo-server-express": "^2.1.0",
    "graphql": "^14.0.2",
    "graphql-tools": "^4.0.0",
    "pg": "^7.4.3",
    "reflect-metadata": "^0.1.12",
    "rxjs": "^6.2.2",
    "typeorm": "^0.2.7",
    "typescript": "^3.1.1"
  },
  "devDependencies": {
    "@nestjs/testing": "^5.1.0",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.1",
    "@types/node": "^10.7.1",
    "@types/supertest": "^2.0.5",
    "jest": "^23.5.0",
    "nodemon": "^1.18.3",
    "prettier": "^1.14.2",
    "rimraf": "^2.6.2",
    "supertest": "^3.1.0",
    "ts-jest": "^23.1.3",
    "ts-loader": "^4.4.2",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.5.0",
    "tslint": "5.11.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-node-externals": "^1.7.2"
  },

src / graphql.ts应该从我的graphql文件中生成吗?还是我想念什么? 文档尚不是100%清楚,但是看起来像带有类型的resolver和graphql是必需的,应该没问题。 我将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

将键入的文件名更改为graphql.ts以外的名称。

definitions: {
    path: join(process.cwd(), 'src/gql.ts'),
},

答案 1 :(得分:0)

我认为将输出更改为类可以解决我的问题:

definitions: {
  path: join(process.cwd(), 'src/graphql.schema.ts'),
  outputAs: 'class',
},

(并在使用新代码启动服务器之前删除旧的自动生成的文件)

要提及的另一件事是将此文件添加到nodemon异常(在根文件夹中的nodemon.json处),以避免在开发模式下(使用npm run start:dev时重新加载循环)

{
  ...
  "ignore": ["src/**/*.spec.ts", "src/graphql.schema.ts"],
  ...
}