type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

Two mounted apollo servers (resolvers with inhertance) – second one lacks ArgsType in query

Open Alex0007 opened this issue 2 years ago • 3 comments

Describe the Bug We are creating two schemas, 2 initializing 2 apollo servers (apollo-server-express), then trying to mount them on two different paths: /graphql and /cci

To Reproduce This example includes parts of code

  @ArgsType()
  export class PaginationArgs {
    @Field((type) => Int, { defaultValue: 0 })
    offset!: number;
  
    @Field((type) => Int)
    take!: number;
  }
  
  @ArgsType()
  export class OrderArgs {
    @Field({ defaultValue: "_id" })
    sortBy!: string;
  
    // @Field((type) => SortOrder, { defaultValue: SortOrder.desc })
    // order!: SortOrder;
  }

  @Resolver((of) => itemType, { isAbstract: false })
  class AbstractResolverClass {
    @Query(returns => Task)
    ['tasks'+Math.floor(Math.random() *1000)](
      @Args() { offset, take }: PaginationArgs,
      @Args() { sortBy }: OrderArgs,
    ) {
      
    }

    @FieldResolver()
    ['field'+Math.floor(Math.random() *1000)](
      @Args() { offset, take }: PaginationArgs,
      @Args() { sortBy }: OrderArgs,
    ): number {
      return Math.random()
    }
  }
  
  // example of build function
  export const buildCciApolloServer = async (httpServer: http.Server) => {
  const schema = await buildSchema({
    resolvers: [
      TaskResolver,
      // TestResolver
    ],
    nullableByDefault: true,
    scalarsMap: [{ type: Types.ObjectId, scalar: ID }],
    globalMiddlewares: [TypegooseMiddleware],
    authChecker: null
  });

  const server = new ApolloServer<ApolloContext>({
    schema,
    plugins: [
      ApolloServerPluginDrainHttpServer({ httpServer }),
      ComplexityPlugin({
        schema,
        maxComplexity: 100,
        estimators: [simpleEstimator({ defaultComplexity: 1 })],
      }),
    ],
    context: graphqlAuthContext,
  });

  return server;
};
  
  // init
  const cciApolloServer = await buildCciApolloServer(httpServer)
  const apiApolloServer = await buildApiApolloServer(httpServer)

  await cciApolloServer.start()
  await apiApolloServer.start()

  cciApolloServer.applyMiddleware({ app, path: '/cci' })
  apiApolloServer.applyMiddleware({ app, path: '/graphql' });

As the result, second built server don't have args that are defined via @ArgsType()

First server: args are here Screenshot 2022-08-03 at 22 09 49

Second server: no args available Screenshot 2022-08-03 at 22 10 12

Alex0007 avatar Aug 03 '22 18:08 Alex0007

Please create a minimal reproducible code example repository. So that I can run it and debug what's happening. Your code snippets are incomplete.

MichalLytek avatar Aug 04 '22 07:08 MichalLytek

Hey @MichalLytek Check this out: https://github.com/Alex0007/graphql-argstype-bug

Alex0007 avatar Aug 05 '22 00:08 Alex0007

I can reproduce and confirm the issue.

It not happens when the resolver is build without inheritance (generic function). So the build function is mutating the metadata making it losses info about args. Need to debug and dig further.

MichalLytek avatar Aug 05 '22 07:08 MichalLytek