Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cooperation with graphql-faker #293

Open
baronTommy opened this issue Mar 28, 2019 · 6 comments
Open

Cooperation with graphql-faker #293

baronTommy opened this issue Mar 28, 2019 · 6 comments
Labels
Community 👨‍👧 Something initiated by a community Discussion 💬 Brainstorm about the idea Enhancement 🆕 New feature or request

Comments

@baronTommy
Copy link

baronTommy commented Mar 28, 2019

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: ...)
}
@MichalLytek
Copy link
Owner

MichalLytek commented Mar 28, 2019

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).

@MichalLytek MichalLytek added Enhancement 🆕 New feature or request Community 👨‍👧 Something initiated by a community Discussion 💬 Brainstorm about the idea labels Mar 28, 2019
@MichalLytek MichalLytek added this to the Future release milestone Mar 28, 2019
@baronTommy
Copy link
Author

baronTommy commented Mar 28, 2019

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

@MichalLytek
Copy link
Owner

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 😉

@baronTommy
Copy link
Author

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

@MichalLytek
Copy link
Owner

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).

@baronTommy
Copy link
Author

a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Discussion 💬 Brainstorm about the idea Enhancement 🆕 New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants