nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.46k stars 396 forks source link

When I use the include property, it doesn't find the resolvers. #1106

Closed gcherem closed 4 years ago

gcherem commented 4 years ago

I'm submitting a...


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When I use the include property, it doesn't find the resolvers.

BTW, I am using schema first approach.

Both endpoints appear at the Playground, but when I run any query, I get the following error:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.user.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Query.user.",
            "    at completeValue (/Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/graphql/execution/execute.js:595:13)",
            "    at completeValueCatchingError (/Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/graphql/execution/execute.js:530:19)",
            "    at resolveField (/Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/graphql/execution/execute.js:461:10)",
            "    at executeFields (/Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/graphql/execution/execute.js:297:18)",
            "    at executeOperation (/Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/graphql/execution/execute.js:241:122)",
            "    at executeImpl (/Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/graphql/execution/execute.js:119:14)",
            "    at Object.execute (/Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/graphql/execution/execute.js:63:35)",
            "    at /Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/apollo-server-core/src/requestPipeline.ts:548:22",
            "    at Generator.next (<anonymous>)",
            "    at /Users/gcherem/Source/nestjs/graphql-schema-first-problem/node_modules/apollo-server-core/dist/requestPipeline.js:8:71"
          ]
        }
      }
    }
  ],
  "data": null
}

If I comment out any GraphQLModule.forRoot and the include property of the other one, it works (but one endpoint with the whole schema).

Expected behavior

All queries should work.

Minimal reproduction of the problem with instructions

Create two GraphQLModule.forRoot with include property.

app.module.ts

import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';

import { AdminModule } from './admin/admin.module';
import { PublicModule } from './public/public.module';

@Module({
  imports: [
    AdminModule,
    PublicModule,

    GraphQLModule.forRoot({
      debug: true,
      playground: true,
      path: '/admin',
      typePaths: ['src/admin/**/*.gql'],
      include: [AdminModule], // <--- here
      definitions: {
        path: join(process.cwd(), 'src/admin/graphql.schema.generated.ts'),
        outputAs: 'class',
      },
    }),

    GraphQLModule.forRoot({
      debug: true,
      playground: true,
      path: '/public',
      typePaths: ['src/public/**/*.gql'],
      include: [PublicModule], // <--- here
      definitions: {
        path: join(process.cwd(), 'src/public/graphql.schema.generated.ts'),
        outputAs: 'class',
      },
    }),

  ],
  controllers: [],
})
export class AppModule {}

admin.module.ts

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

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

@Module({
  imports: [ UsersModule ],
})
export class AdminModule {}

public.module.ts

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

import { ProjectsModule } from './projects/projects.module';

@Module({
  imports: [ProjectsModule],
})
export class PublicModule {}

users.gql

type User {
  id: ID!
  name: String!
  email: String!
  password: String!
}

type Query {
  user(id: ID!): User!
}

users.module.ts

import { Module } from '@nestjs/common';
import { UsersResolver } from './users.resolver';

@Module({
  providers: [UsersResolver]
})
export class UsersModule {}

users.resolver.ts

import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';

import { User } from '../graphql.schema.generated';

@Resolver('User')
export class UsersResolver {

  @Query()
  user(@Args('id') id: string) {
    console.log('This line never runs!')
    return { id: '1', name: 'worked', email: 'worked', password: 'worked' };
  }
}

projects.gql

type Project {
  id: ID!
  name: String!
}

type Query {
  project(id: ID!): Project!
}

projects.module.ts

import { Module } from '@nestjs/common';
import { ProjectsResolver } from './projects.resolver';

@Module({
  providers: [ProjectsResolver]
})
export class ProjectsModule {}

projects.resolver.ts

import { Args, Query, Resolver } from '@nestjs/graphql';

@Resolver('Project')
export class ProjectsResolver {

  @Query()
  project(@Args('id') id: string) {
    console.log('This line never runs!')
    return { id: '1', name: 'worked' };
  }
}

Environment


Nest version: 7.4.2


For Tooling issues:
- Node version: v12.18.2
- Platform:  Mac

Others:

kamilmysliwiec commented 4 years ago

Please provide a minimum reproduction repository.

gcherem commented 4 years ago

@kamilmysliwiec , here it is: https://github.com/gcherem/nest-problem-with-include

gcherem commented 4 years ago

Hi @kamilmysliwiec, any update on it?

gcherem commented 4 years ago

After more tests I became it was due nested modules and I opened a more specific issue [#1148]

Please close this issue, thanks