question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

3.4.* introduces a regression on the `merge` policy

See original GitHub issue

Intended outcome: The 3.4.* versions introduce a regression on the merge policy

Actual outcome: The merge custom policy should receive correct parameters

How to reproduce the issue:

Here a unit test:

import { gql, InMemoryCache, useQuery } from '@apollo/client'
import { MockedProvider } from '@apollo/client/testing'
import { render, screen, waitFor } from '@testing-library/react'
import faker from 'faker'

const cache = new InMemoryCache({
  typePolicies: {
    Country: {
      fields: {
        cities: {
          keyArgs: ['size'],
          merge(existing, incoming, { args }) {
            console.log(existing, incoming, { args })
            if (!args) return incoming

            const items = existing ? existing.slice(0) : []

            const offset = args.offset ?? 0
            for (let i = 0; i < incoming.length; ++i) {
              items[offset + i] = incoming[i]
            }

            return items
          },
        },
      },
    },
    CityInfo: {
      merge: true,
    },
  },
})

const GET_COUNTRIES = gql`
  query GetCountries {
    countries {
      id
      ...WithSmallCities
      ...WithAirQuality
    }
  }

  fragment WithSmallCities on Country {
    biggestCity {
      id
    }
    smallCities: cities(size: SMALL) {
      id
    }
  }

  fragment WithAirQuality on Country {
    biggestCity {
      id
      info {
        airQuality
      }
    }
  }
`

function Test() {
  const { data } = useQuery(GET_COUNTRIES)
  return <div data-testid="countryNumber">{data?.countries.length}</div>
}

it('should render without error', async () => {
  const countries = [
    {
      __typename: 'Country',
      id: faker.datatype.uuid(),
      biggestCity: {
        __typename: 'City',
        id: faker.datatype.uuid(),
        info: {
          __typename: 'CityInfo',
          airQuality: faker.datatype.number(),
        },
      },
      smallCities: [
        {
          __typename: 'City',
          id: faker.datatype.uuid(),
        },
      ],
    },
  ]

  const mock = { request: { query: GET_COUNTRIES }, result: { data: { countries } } }

  render(
    <MockedProvider cache={cache} mocks={[mock]} resolvers={{}}>
      <Test />
    </MockedProvider>,
  )

  await waitFor(() => {
    const element = screen.getByTestId('countryNumber')
    expect(element).toHaveTextContent(countries.length.toString())
  })
})

If you try to run this test, you will have this error Uncaught [TypeError: existing.slice is not a function], since we receive these arguments into the merge function: { __ref: 'City:a44cfd2e-c50f-4fcf-924d-d8e275c8df07' } as existing { id: 'a44cfd2e-c50f-4fcf-924d-d8e275c8df07', __typename: 'City', info: { __typename: 'CityInfo', airQuality: 20807 } } as incoming

We should have: undefined as existing { __ref: 'City:a44cfd2e-c50f-4fcf-924d-d8e275c8df07' } as incoming

FYI If we delete CityInfo: { merge: true } from the cache configuration, it works.

Versions It works with 3.3.21 but not with the new 3.4.* versions

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
benjamncommented, Aug 9, 2021

@Grmiade (and @anark) Thanks for the reproduction! This should be fixed in @apollo/client@3.4.7 (just published).

1reaction
Grmiadecommented, Aug 10, 2021

It also works on our side. Thanks a lot for the reactivity 💪

Read more comments on GitHub >

github_iconTop Results From Across the Web

3.4.1. Merging — LeDataSciFi-2022
What variable (or variables) should you be merging on? For example: Should you merge based on the firm, or the firm AND the...
Read more >
13 Merging | Data Wrangling with R
A simple merge adds the data values in one data set as new variables to the observations in another data set. Consider the...
Read more >
LilyPond Contributor's Guide: 3.3 Lifecycle of a merge request
1. Introduction to contributing · 1.1 Help us · 2. Quick start · 2.1 LilyDev · 3. Working with source code · 3.1...
Read more >
Streaming statistical models via Merge & Reduce | SpringerLink
In this paper, we propose a method called Merge & Reduce as a technique to address these scalability limitations in regression analysis. Merge ......
Read more >
Merger Policy at the Margin: Western Refining's Acquisition of ...
Merger Policy at the Margin: Western Refining' s Acquisition... 73 ... Albuquerque- El Paso 1.7 3.6 -4.7 15.1 -1.1 3.4 -10.2 10.1.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found