Type-graphql: Cooperation with graphql-faker

Created on 28 Mar 2019  Â·  6Comments  Â·  Source: MichalLytek/type-graphql

type-graphql is great

Is your feature request related to a problem? Please describe.
Cooperation with graphql-faker
https://github.com/APIs-guru/graphql-faker

Describe the solution you'd like
I want you to add fakerOption

Describe alternatives you've considered
...

Additional context

@ObjectType()
class Recipe {
  @Field({
    fakerOption: {
        fake: {},
        examples: {}
      }
  })
  title: string;
}

↓ generate

type Recipe {
  title: String @fake(type: ...)
}
Community Discussion Enhancement

Most helpful comment

We can support different faker modes - global (catch every null/error in resolver and replace with fake data), per type (fake all recipe fields), per query (fake the response object) and per field (fake the title and description fields).

All 6 comments

generate (...) @fake(type: ...)

It's not possible due to #77.

All we can do is to create a @Faker(...) decorator that will accept the options and use faker.js under the hood to return fake data from the resolvers.

You can implement it by yourself using middlewares and custom decorators feature. The only problem is that you have to read the type info from Reflect.metadata like type-graphql is doing internally, explicitly provide info about the primitive type to fake or read the field type from info parameter (GraphQLResolveInfo).

Response Thank you

The only problem is that you have to read the type info from

It may be possible with Nestjs UsePipes 😂
https://docs.nestjs.com/pipes

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

@Resolver()
class RecipeResolver {
  private recipesCollection: Recipe[] = [];

  @UsePipes(FakerPipe([Recipe])) // <- ★
  @Query(returns => [Recipe])
  async recipes() {
    return await this.recipesCollection;
  }
}

It may be possible with Nestjs UsePipes

With TypeGraphQL middlewares too:
https://typegraphql.ml/docs/middlewares.html

But the problem is with reading/parsing the GraphQLResolveInfo which is not convenient, but doable 😉

I missed it. I'm sorry 😹


The only problem is that you have to read the type info from

If you think carefully
I think partial Faker is strange

@ObjectType()
class Recipe {
  @Field({
    fakerOption: {
        fake: {},
        examples: {}
      }
  })
  title: string;
}


Is it better to use function units?

@Resolver()
export class RecipeResolver {
  @UseFakerMiddleware([Recipe]) // <- ★
  @Query(returns => [Recipe])
  async recipes() {
    return await this.recipesCollection;
  }
}

We can support different faker modes - global (catch every null/error in resolver and replace with fake data), per type (fake all recipe fields), per query (fake the response object) and per field (fake the title and description fields).

a

Was this page helpful?
0 / 5 - 0 ratings