ardeois / graphql-codegen-typescript-mock-data

[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.
MIT License
133 stars 47 forks source link

Custom scalar is mocked as a type #17

Closed mdazy closed 4 years ago

mdazy commented 4 years ago

My schema specifies two scalars:

scalar Date
scalar AnyObject
input ServiceParametersInput {
  date: Date!
  serviceName: String!
  data: AnyObject
}

However, the AnyObject field appears to be mocked as an actual type, without any mock function being generated for it, whereas the Date field is mocked "as expected" - although it's also presented as a typical custom scalar in the graphql doc:

export const aServiceParametersInput = (overrides?: Partial<ServiceParametersInput>): ServiceParametersInput => {
    return {
        date: overrides && overrides.hasOwnProperty('date') ? overrides.date! : '1970-01-13T19:39:08.227Z',
        serviceName: overrides && overrides.hasOwnProperty('serviceName') ? overrides.serviceName! : 'praesentium',
        data: overrides && overrides.hasOwnProperty('data') ? overrides.data! : anAnyObject(),
    };
};

This results into a compile-time error when using the generated mocks file:

    schema/lib/mocks.tsx:221:81 - error TS2304: Cannot find name 'anAnyObject'.

    221         data: overrides && overrides.hasOwnProperty('data') ? overrides.data! : anAnyObject(),
                                                                                        ~~~~~~~~~~~

For now I'm working around this by adding a dummy definition:

  ./schema/lib/mocks.tsx:
    plugins:
      - "graphql-codegen-typescript-mock-data":
          typesFile: "./clientTypes.tsx"
          addTypename: true
      # work around issue with mocking of custom scalars
      - add: const anAnyObject = ():any => null;

Thanks.

ardeois commented 4 years ago

Thanks for the issue, would you be able to create a little reproduction repository? I'll try to check that during the week if I find some time

mdazy commented 4 years ago

You can clone this one, I think I can't make it any shorter :) Thanks.

ardeois commented 4 years ago

perfect thanks !