Skip to content

Commit

Permalink
Fix bug when inserting data into abstract list (#1096)
Browse files Browse the repository at this point in the history
Co-authored-by: Seppe Dekeyser <[email protected]>
  • Loading branch information
AlecAivazis and SeppahBaws committed May 25, 2023
1 parent d7df9ca commit 5daf4c4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-years-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini': patch
---

Fix bug when inserting data into abstract list
4 changes: 2 additions & 2 deletions packages/houdini/src/codegen/transforms/lists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ test('cannot use list directive if id is not a valid field', async function () {
} catch (error: unknown) {
nbError++
// @ts-ignore
expect(error[0].description).toMatchInlineSnapshot(
'"@list on [32mLegend[37m[0m as a configuration issue. Object identification missing: \\"[33mid[37m[0m\\". Check \'Custom IDs\' if needed."'
expect(error[0].message).toMatchInlineSnapshot(
'"@list on [32mLegend[37m[0m has a configuration issue: Legend dos not have a valid key. Please check this link for more information: https://houdinigraphql.com/guides/caching-data#custom-ids"'
)
}
expect(nbError).toBe(1)
Expand Down
15 changes: 7 additions & 8 deletions packages/houdini/src/codegen/validators/typeCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logGreen, logYellow } from '@kitql/helper'
import { logGreen } from '@kitql/helper'
import * as graphql from 'graphql'

import {
Expand Down Expand Up @@ -263,16 +263,15 @@ export default async function typeCheck(config: Config, docs: Document[]): Promi
.filter((fieldName) => !targetType.getFields()[fieldName])

if (missingIDFields.length > 0) {
const message = `@${config.listDirective} on ${logGreen(
targetType.name
)} as a configuration issue. Object identification missing: ${missingIDFields
.map((c) => `"${logYellow(c)}"`)
.join(', ')}. Check 'Custom IDs' if needed.`
errors.push(
new HoudiniError({
filepath: filename,
message,
description: message,
message:
`@${config.listDirective} on ${logGreen(
targetType.name
)} has a configuration issue: ` +
`${targetType} dos not have a valid key. ` +
`Please check this link for more information: https://houdinigraphql.com/guides/caching-data#custom-ids`,
})
)
return
Expand Down
14 changes: 13 additions & 1 deletion packages/houdini/src/runtime/cache/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ export class List {
updates: ['append', 'prepend'],
selection: {
fields: {
__typename: {
keyRaw: '__typename',
type: 'String',
},
node: {
type: listType,
keyRaw: 'node',
Expand All @@ -284,7 +288,15 @@ export class List {
}
insertData = {
newEntry: {
edges: [{ node: { ...data, __typename: listType } }],
edges: [
{
__typename: listType + 'Edge',
node: {
...data,
__typename: listType,
},
},
],
},
}
} else {
Expand Down
43 changes: 43 additions & 0 deletions packages/houdini/src/runtime/cache/tests/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,11 @@ test('append in connection', function () {
keyRaw: 'edges',
selection: {
fields: {
__typename: {
type: 'String',
visible: true,
keyRaw: '__typename',
},
node: {
type: 'Node',
visible: true,
Expand Down Expand Up @@ -1346,6 +1351,7 @@ test('append in connection', function () {
friends: {
edges: [
{
__typename: 'UserEdge',
node: {
__typename: 'User',
id: '2',
Expand Down Expand Up @@ -1389,13 +1395,15 @@ test('append in connection', function () {
friends: {
edges: [
{
__typename: 'UserEdge',
node: {
__typename: 'User',
id: '2',
firstName: 'jane',
},
},
{
__typename: 'UserEdge',
node: {
__typename: 'User',
id: '3',
Expand All @@ -1406,6 +1414,41 @@ test('append in connection', function () {
},
},
})

// make sure we set an typename on the edge (so it has a value when we read back)
expect(
cache.read({
selection,
})
).toEqual({
data: {
viewer: {
id: '1',
friends: {
edges: [
{
__typename: 'UserEdge',
node: {
__typename: 'User',
id: '2',
firstName: 'jane',
},
},
{
__typename: 'UserEdge',
node: {
__typename: 'User',
id: '3',
firstName: 'mary',
},
},
],
},
},
},
partial: false,
stale: false,
})
})

test('inserting data with an update overwrites a record inserted with list.append', function () {
Expand Down

0 comments on commit 5daf4c4

Please sign in to comment.