mswjs / data

Data modeling and relation library for testing JavaScript applications.

Home Page:https://npm.im/@mswjs/data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support null value types

olivierwilkinson opened this issue Β· comments

Hi πŸ‘‹

I'm loving the project, I've just finished integrating it into our gql mocking with msw. It's really simplified how we handle mutations so already a huge win!

I am hoping to add support for null property values. We use Apollo server, which represents missing values for optional types as null, so for a GQL type:

type Account {
  id: String!
  firstName: String
}

The resulting Account type would be represented as:

type Account = {
  id: string;
  firstName: string | null;
}

Ideally I'd want to use a factory function to create realistic defaults, and then set certain properties to null when necessary:

const db = factory({
  account: {
    id: primaryKey(String),
    firstName: () => faker.name.firstName() as string | null,
  }
})

const account = db.account.create()
const namelessAccount = db.account.create({
  firstName: null,
  lastName: null,
})

However I am unable to do that since null is not a valid return type from the factory functions. Instead I made sure the optional types always had a value.

const db = factory({
  account: {
    id: primaryKey(String),
    firstName: faker.name.firstName,
  }
})

This means our mock responses don't perfectly match our production responses because I can't initialise or update accounts to have a non-string firstName.

I will raise a PR with a naive implementation now, at least to clarify what I'm talking about. Looking forwards to hearing what you think.

All the best, the project is awesome! πŸ™Œ

Hey, @olivierwilkinson. Thank you for suggesting this.

We've been thinking about nullable values in the team recently and want to improve how they are handled at the moment. Let me think about this and check your pull request and come back to this issue once I'm done. Thanks.