Skip to content

How to deal with Fragments? #163

@Sam-Kruglov

Description

@Sam-Kruglov

Given I'm mocking a query like this:

const createTx: () => schema.Transaction = () =>  ({ value: 'txValue' })
mockRequestHandler.withResolvers({
    Query: {
      transactions: () => createTx()
    }
})

Then I'm reusing the createTx in my tests like this:

render(<Tx tx={createTx()}/>)
...userEvent, expect, etc

But there's an alias in my actual query:

transactions {
  ...Tx
}

fragment Tx on Transaction {
  value: myValue
}

So, while in the <Tx> component, I'm not getting myValue since createTx returns an object with value instead.

I am using graphql-codegen, so I have TxFragment type available, but I don't see how I can convert the raw type (schema.Transaction) into the fragment. What's the recommended approach?

Here's my workaround:

const createTx: () => schema.Transaction & Pick<TxFragment, 'myValue'> = () =>  ({ value: 'txValue', myValue: 'txValue' })

Just hardcoding specific differences so that type casting works in both places. But it's a hack

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions