Skip to content

Custom scalar is mocked as a type #17

@mdazy

Description

@mdazy

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions