2

I am starting building my first API with Nest. I am trying to add GraphQL and got an issue straight at the very beggining.

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)

I have so far just UsersModule and in its folder I have users.graphql file:

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

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

I also have simple resolver:

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);
  }
}

My 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 if it matters:

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 versions:

"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 should be generated from my graphql files right? Or I am missing something? Documentation is not 100% clear about that, but it looks like resolver and graphql with types are required and it should be fine. I will really appreciate any help.

6
  • Please add the stack trace of the error to the question. Commented Sep 29, 2018 at 0:39
  • done. Sorry, I was pretty sure I did it Commented Sep 29, 2018 at 4:54
  • Looks like something is going wrong with the graphql module loading. I tried briefly to reproduce the problem and was unsuccessful. Can you share a repository that reproduces the problem for you? Commented Sep 29, 2018 at 22:10
  • @MattMcCutchen I've same problem here. Commented Sep 30, 2018 at 0:05
  • @AbhinandanN.M. OK; if you can provide a repository, I'll be able to investigate. Commented Sep 30, 2018 at 0:08

2 Answers 2

1

Change the filename of typings to something other than graphql.ts.

Like this

definitions: {
    path: join(process.cwd(), 'src/gql.ts'),
},
Sign up to request clarification or add additional context in comments.

Comments

0

I think changing output to class fixed my problem:

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

(and delete old autogenerated files before start server with new code)

another thing to mention is to add this file to nodemon exceptions (at nodemon.json in root folder) to avoid reloading loop in development mode (when you use npm run start:dev)

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

1 Comment

No. Changing the name from graphql.ts to graphql.schema.ts fixed your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.