Skip to content

Commit

Permalink
fix(nodeId): #71 adjust nodeId fetch/delete logic WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Sep 11, 2021
1 parent 227ba66 commit 0f61a77
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
54 changes: 47 additions & 7 deletions src/__tests__/e2e/dataProvider.query.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,53 @@ describe('dataProvider', () => {

describe('Primary keys', () => {
it('should handle non-id field primary keys with `nodeId`', async () => {
expect(
await dataProvider.getList('favoriteBook', {
sort: { field: 'isbn', order: 'ASC' },
filter: {},
pagination: { perPage: 10, page: 1 },
})
).toMatchSnapshot()
const favoritesList = await dataProvider.getList('favoriteBook', {
sort: { field: 'isbn', order: 'ASC' },
filter: {},
pagination: { perPage: 10, page: 1 },
})
expect(favoritesList).toMatchSnapshot()
})

it('should fetch a single item by nodeId', async () => {
const favoritesList = await dataProvider.getList('favoriteBook', {
sort: { field: 'isbn', order: 'ASC' },
filter: {},
pagination: { perPage: 10, page: 1 },
})

const singleBook = await dataProvider.getOne('favoriteBook', {
id: favoritesList?.data[0]?.id,
})
expect(singleBook?.data?.isbn).toEqual('3221123')
})

it('should fetch many items by nodeId', async () => {
const favoritesList = await dataProvider.getList('favoriteBook', {
sort: { field: 'isbn', order: 'ASC' },
filter: {},
pagination: { perPage: 10, page: 1 },
})

const manyBooks = await dataProvider.getMany('favoriteBook', {
ids: favoritesList?.data.map((data) => data.isbn),
})
console.log(manyBooks)
})

it('should be able to delete for non-id field primary keys with `deleteByNodeId`', async () => {
const data = await dataProvider.getList('favoriteBook', {
pagination: { perPage: 10, page: 1 },
filter: {
isbn: '3221123',
},
sort: { field: 'isbn', order: 'ASC' },
})
const deletedBook = await dataProvider.delete('favoriteBook', {
id: data?.data[0].id,
previousData: data?.data[0],
})
console.log(deletedBook)
})
})

Expand Down
38 changes: 20 additions & 18 deletions src/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const buildQuery = (introspectionResults: IntrospectionResult, factory: F
id: mapType(primaryKeyType, (params as GetOneParams).id),
},
parseResponse: (response: Response) => {
return { data: response.data[singleLowerResourceName] }
return { data: response.data[getResourceName] }
},
}
case GET_MANY:
Expand Down Expand Up @@ -244,33 +244,35 @@ export const buildQuery = (introspectionResults: IntrospectionResult, factory: F
return {
variables: {
input: {
id: mapType(primaryKeyType, (params as DeleteParams).id),
[primaryKey.idKeyName]: mapType(primaryKeyType, (params as DeleteParams).id),
},
},
query: gql`
mutation ${deleteResourceName}($input: Delete${resourceTypename}Input!) {
mutation ${deleteResourceName}($input: ${capitalize(deleteResourceName)}Input!) {
${deleteResourceName}(input: $input) {
${singleLowerResourceName} {
${createQueryFromType(
resourceTypename,
typeMap,
typeMapConfiguration,
primaryKey,
DELETE
)}
}
}
${singleLowerResourceName} {
${createQueryFromType(
resourceTypename,
typeMap,
typeMapConfiguration,
primaryKey,
DELETE
)}
}
}
}
`,
parseResponse: (response: Response) => ({
data: response.data[`${deleteResourceName}`][singleLowerResourceName],
}),
parseResponse: (response: Response) => {
return {
data: response?.data[deleteResourceName][singleLowerResourceName],
}
},
}
}
case DELETE_MANY: {
const thisIds = (params as UpdateManyParams).ids
const deletions = thisIds.map((id) => ({
id: mapType(primaryKeyType, id),
[primaryKey.idKeyName]: mapType(primaryKeyType, id),
clientMutationId: id.toString(),
}))
return {
Expand All @@ -284,7 +286,7 @@ export const buildQuery = (introspectionResults: IntrospectionResult, factory: F
query: gql`
mutation deleteMany${resourceTypename}(
${thisIds
.map((id) => `$arg${escapeIdType(id)}: Delete${resourceTypename}Input!`)
.map((id) => `$arg${escapeIdType(id)}: ${capitalize(deleteResourceName)}Input!`)
.join(',')}
) {
${thisIds.map(
Expand Down

0 comments on commit 0f61a77

Please sign in to comment.