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

fix: Fixes support for Mocking GraphQL Queries in Storybook when using GraphQL Fragments #10825

Merged
merged 13 commits into from
Jun 29, 2024

Conversation

dthyresson
Copy link
Contributor

@dthyresson dthyresson commented Jun 17, 2024

Fixes #10807

If one used GraphQL fragments, when mocking the GraphQL query for use in Storybook, the typename for the data object must be included otherwise Apollo client cannot properly map the data.

This PR

  • adds the typename to the cell generator templates
  • updates the testing and graphql mock and fragments documentation to show how properly defines mock data

From issue #10807

What's not working?

When mocking a graphql query inside a component to be able to render Cell children, it breaks when using fragments inside the Cell Query.

My query in ChatMessagesCell.tsx:

export const QUERY: TypedDocumentNode<
  ChatMessagesQuery,
  ChatMessagesQueryVariables
> = gql`
  query ChatMessagesQuery($chatRoomId: String!) {
    chatMessages(chatRoomId: $chatRoomId) {
      id
      body
      createdAt
      user {
        id
        displayName
      }
    }
  }
`

Then I mock this query inside a parent component Chatbox.stories.tsx:

    mockGraphQLQuery<ChatMessagesQuery, ChatMessagesQueryVariables>(
      'ChatMessagesQuery',
      (_variables) => {
        console.log('standard().chatMessages', standard().chatMessages)
        return {
          chatMessages: standard().chatMessages,
        }
      }
    )

This works as it should, mocks the list using the standard values

image

BUT THEN:

My query with fragments in ChatMessagesCell.tsx:

registerFragment(gql`
  fragment ChatMessageFragment on ChatMessage {
    id
    body
    createdAt
    user {
      id
      displayName
    }
  }
`)

export const QUERY: TypedDocumentNode<
  ChatMessagesQuery,
  ChatMessagesQueryVariables
> = gql`
  query ChatMessagesQuery($chatRoomId: String!) {
    chatMessages(chatRoomId: $chatRoomId) {
      ...ChatMessageFragment
    }
  }
`

With the same mock. The list is created from the standard() function but the inner objects are empty

image

@dthyresson dthyresson added the release:fix This PR is a fix label Jun 17, 2024
@dthyresson dthyresson added this to the next-release milestone Jun 17, 2024
@dthyresson dthyresson self-assigned this Jun 17, 2024
@dthyresson dthyresson changed the title fix: Fixes support for Mocking GraphQL Queries in Storybook twhene using GraphQL Fragments fix: Fixes support for Mocking GraphQL Queries in Storybook whene using GraphQL Fragments Jun 17, 2024
@dthyresson dthyresson changed the title fix: Fixes support for Mocking GraphQL Queries in Storybook whene using GraphQL Fragments fix: Fixes support for Mocking GraphQL Queries in Storybook when using GraphQL Fragments Jun 17, 2024
@dthyresson dthyresson marked this pull request as ready for review June 19, 2024 14:17
@dthyresson
Copy link
Contributor Author

Am running into some type issues having added __typename to mocks when yarn rw type-check:

Error: 25hsrc/components/AuthorCell/AuthorCell.test.tsx(39,23): error TS2322: Type '{ __typename: string; id: number; email: string; fullName: string; }' is not assignable to type '{ __typename?: "User"; email: string; fullName: string; }'.
  Types of property '__typename' are incompatible.
    Type 'string' is not assignable to type '"User"'.

Perhaps due to

/**
 * @params TData = Type of data based on your graphql query. This can be imported from 'types/graphql'
 * @example
 * import type { FindPosts } from 'types/graphql'
 *
 * const { post }: CellSuccessData<FindPosts> = props
 */
export type CellSuccessData<TData = any> = ConditionallyGuaranteed<
  Omit<TData, '__typename'>
>

Investigating.

Copy link
Collaborator

@Josh-Walker-GM Josh-Walker-GM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple minor comments but otherwise looks good. We paired on this and I seen how this change fixed the issue.

packages/cli/src/commands/generate/cell/cell.js Outdated Show resolved Hide resolved

### Mock Data

To satisfy the query as well as the fragment needs your mock data should include the `Book` typename:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if this just needs a comma somewhere but I can't wrap my head around this sentence sorry.

docs/docs/testing.md Outdated Show resolved Hide resolved
dthyresson and others added 2 commits June 28, 2024 14:58
Co-authored-by: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com>
Co-authored-by: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com>
@dthyresson dthyresson dismissed Josh-Walker-GM’s stale review June 28, 2024 19:03

Will re-review docs for clarity.

@dthyresson dthyresson enabled auto-merge (squash) June 29, 2024 00:15
@dthyresson dthyresson merged commit 15dd2c9 into main Jun 29, 2024
50 checks passed
@dthyresson dthyresson deleted the dt-mock-graphql-with-fragments branch June 29, 2024 00:18
Josh-Walker-GM added a commit that referenced this pull request Jul 23, 2024
…g GraphQL Fragments (#10825)

Fixes #10807

If one used GraphQL fragments, when mocking the GraphQL query for use in
Storybook, the `typename` for the data object must be included otherwise
Apollo client cannot properly map the data.

This PR 

* adds the typename to the cell generator templates
* updates the testing and graphql mock and fragments documentation to
show how properly defines mock data


--- 

From issue #10807


### What's not working?

When mocking a graphql query inside a component to be able to render
Cell children, it breaks when using fragments inside the Cell Query.

My query in `ChatMessagesCell.tsx`:
```typescript
export const QUERY: TypedDocumentNode<
  ChatMessagesQuery,
  ChatMessagesQueryVariables
> = gql`
  query ChatMessagesQuery($chatRoomId: String!) {
    chatMessages(chatRoomId: $chatRoomId) {
      id
      body
      createdAt
      user {
        id
        displayName
      }
    }
  }
`
```

Then I mock this query inside a parent component `Chatbox.stories.tsx`:
```tsx
    mockGraphQLQuery<ChatMessagesQuery, ChatMessagesQueryVariables>(
      'ChatMessagesQuery',
      (_variables) => {
        console.log('standard().chatMessages', standard().chatMessages)
        return {
          chatMessages: standard().chatMessages,
        }
      }
    )
```

This works as it should, mocks the list using the standard values


![image](https://github.com/redwoodjs/redwood/assets/47559427/01e5701c-f873-4ecf-97f3-40b9da665c32)


BUT THEN:

My query with fragments in `ChatMessagesCell.tsx`:
```typescript
registerFragment(gql`
  fragment ChatMessageFragment on ChatMessage {
    id
    body
    createdAt
    user {
      id
      displayName
    }
  }
`)

export const QUERY: TypedDocumentNode<
  ChatMessagesQuery,
  ChatMessagesQueryVariables
> = gql`
  query ChatMessagesQuery($chatRoomId: String!) {
    chatMessages(chatRoomId: $chatRoomId) {
      ...ChatMessageFragment
    }
  }
`
```

With the same mock. The list is created from the `standard()` function
but the inner objects are empty


![image](https://github.com/redwoodjs/redwood/assets/47559427/0fd4cb6c-73b6-442d-aeec-815069d63a28)

---------

Co-authored-by: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com>
Josh-Walker-GM added a commit that referenced this pull request Jul 27, 2024
**Problem**
The rebuild-test-project-fixture script was broken locally - and
probably on CI too.

**Changes**
1. Move generation of user sdl ahead of dbauth setup
2. Updates general version bumps in fixure
3. Updates for fragment type fix (on or around #10825
4. Updates for useBlocker in contact us form #10873
5. Updates to remove unused imports and have lint check complete
successfully
@Josh-Walker-GM Josh-Walker-GM removed this from the next-release milestone Sep 4, 2024
@Josh-Walker-GM Josh-Walker-GM added this to the v8.0.0 milestone Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug?]: MockGraphQLQuery not working when original query uses fragments
2 participants