benawad / type-graphql-series

Typescript GraphQL Server built with TypeGraphQL
326 stars 129 forks source link

GraphQLError { message: 'Cannot read property \'req\' of undefined' .... #11

Open CoonHouse opened 5 years ago

CoonHouse commented 5 years ago

Hello Ben, Great tutorial! My jest tests are failing with the following error:

GraphQLError { message: 'Cannot read property \'req\' of undefined' .....

It seems to go wrong on @Ctx() { req }: IMyContext, in the resolvers. Like req doesn't exist in Ctx. It only happens with tests. Running the application everything is working fine.

Thanks and regards,

Wilco

benawad commented 5 years ago

are you creating a dummy req here https://github.com/benawad/type-graphql-series/blob/master/src/test-utils/gCall.ts#L24

CoonHouse commented 5 years ago

No I didn't! Wonder how I missed that one, sorry. Think I should watch the video again. ;)

Thanks for your help!

CoonHouse commented 5 years ago

I have another question. I have some enum values in my resolvers. Do you know how I can pass those into the 'data' object in the test?

Thanks and regards,

Wilco

benawad commented 5 years ago

what trouble are you having passing an enum value in?

CoonHouse commented 5 years ago

I have the following test:

  it('Create new Arena', async () => {
    const result = await GraphQLCall({
      source: createArenaMutation,
      variableValues: {
        data: {
          .............
          location: ArenaLocation.Indoor,
          ...........
        },
      },
    })

I get the following error:

Expected type ArenaLocation at value.location; did you mean Indoor or Outdoor?

I also tried passing the values directly like

location: "Indoor",

and

location: "indoor",

The enum is:

export enum ArenaLocation {
  Indoor = 'indoor',
  Outdoor = 'outdoor',
}

Regards

benawad commented 5 years ago

so you have a typescript enum not a graphql one?

CoonHouse commented 5 years ago

Ehh, I don't know, I followed the documentation from Type-GraphQL

benawad commented 5 years ago

and I assume your calling registerEnumType

I would think passing it in as a string would work

CoonHouse commented 5 years ago

Yes, I am calling registerEnumType and when I debug it's actually hit. Passing in strings didn't work, I'm going to do some more research. Thanks for your time!

regards

CoonHouse commented 5 years ago

This way it's working!

  it('Create new Arena', async () => {
    const result = await GraphQLCall({
      source: createArenaMutation,
      variableValues: {
        data: {
          .............
          location: "Indoor" as ArenaLocation,
          ...........
        },
      },
    })

regards

Wilco

benawad commented 5 years ago

Nice!