Skip to content

Commit 6b56723

Browse files
authored
fix: remove deprecated Apollo patterns to stop log spam
* fix: remove deprecated Apollo onError/onCompleted callbacks to stop log spam * fix: remove deprecated addTypename option from InMemoryCache * fix: remove deprecated addTypename prop from MockedProvider in tests
1 parent 39561e7 commit 6b56723

6 files changed

Lines changed: 271 additions & 196 deletions

File tree

src/components/basecamp/OrganizationForm.tsx

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function OrganizationForm ({ existingOrg, onClose }: Organization
4545
const [addOrganization] = useMutation<{ addOrganization: OrganizationType }, { input: AddOrganizationProps }>(
4646
MUTATION_ADD_ORGANIZATION, {
4747
client: graphqlClient,
48-
onError: (error) => toast.error(`Unexpected error: ${error.message}`),
48+
errorPolicy: 'none',
4949
refetchQueries: [{
5050
query: QUERY_ORGANIZATIONS,
5151
variables: {
@@ -58,7 +58,7 @@ export default function OrganizationForm ({ existingOrg, onClose }: Organization
5858
const [updateOrganization] = useMutation<{ updateOrganization: OrganizationType }, { input: UpdateOrganizationProps }>(
5959
MUTATION_UPDATE_ORGANIZATION, {
6060
client: graphqlClient,
61-
onError: (error) => toast.error(`Unexpected error: ${error.message}`),
61+
errorPolicy: 'none',
6262
refetchQueries: [{
6363
query: QUERY_ORGANIZATIONS,
6464
variables: {
@@ -119,34 +119,46 @@ export default function OrganizationForm ({ existingOrg, onClose }: Organization
119119
...(dirtyFields?.hardwareReportLink === true && { hardwareReportLink }),
120120
...(dirtyFields?.description === true && { description })
121121
}
122-
if (existingOrg == null) {
123-
const input: AddOrganizationProps = {
124-
orgType,
125-
...dirtyEditableFields
126-
}
127-
await addOrganization({
128-
variables: { input },
129-
context: {
130-
headers: {
131-
authorization: `Bearer ${session?.data?.accessToken as string ?? ''}`
122+
try {
123+
if (existingOrg == null) {
124+
const input: AddOrganizationProps = {
125+
orgType,
126+
...dirtyEditableFields
127+
}
128+
const res = await addOrganization({
129+
variables: { input },
130+
context: {
131+
headers: {
132+
authorization: `Bearer ${session?.data?.accessToken as string ?? ''}`
133+
}
132134
}
135+
})
136+
if (res.errors != null) {
137+
toast.error(`Unexpected error: ${res.errors[0]?.message ?? 'Unknown error'}`)
138+
return
133139
}
134-
})
135-
} else {
136-
const input: UpdateOrganizationProps = {
137-
orgId: existingOrg.orgId,
138-
...dirtyEditableFields
139-
}
140-
await updateOrganization({
141-
variables: { input },
142-
context: {
143-
headers: {
144-
authorization: `Bearer ${session?.data?.accessToken as string ?? ''}`
140+
} else {
141+
const input: UpdateOrganizationProps = {
142+
orgId: existingOrg.orgId,
143+
...dirtyEditableFields
144+
}
145+
const res = await updateOrganization({
146+
variables: { input },
147+
context: {
148+
headers: {
149+
authorization: `Bearer ${session?.data?.accessToken as string ?? ''}`
150+
}
145151
}
152+
})
153+
if (res.errors != null) {
154+
toast.error(`Unexpected error: ${res.errors[0]?.message ?? 'Unknown error'}`)
155+
return
146156
}
147-
})
157+
}
158+
onClose()
159+
} catch (error) {
160+
toast.error(`Unexpected error: ${error instanceof Error ? error.message : 'Unknown error'}`)
148161
}
149-
onClose()
150162
}
151163

152164
return (

src/components/users/__tests__/ImportFromMtnProj.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ jest.mock('react-toastify', () => ({
2727
describe('<ImportFromMtnProj />', () => {
2828
it('renders without crashing', () => {
2929
render(
30-
<MockedProvider mocks={[]} addTypename={false}>
30+
<MockedProvider mocks={[]}>
3131
<ImportFromMtnProj username='testuser' />
3232
</MockedProvider>
3333
)
3434
})
3535

3636
it('renders modal on button click', async () => {
3737
render(
38-
<MockedProvider mocks={[]} addTypename={false}>
38+
<MockedProvider mocks={[]}>
3939
<ImportFromMtnProj username='testuser' />
4040
</MockedProvider>
4141
)

src/js/graphql/Client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const errorLink = onError(({ graphQLErrors, networkError, ...rest }) => {
3232
export const graphqlClient = new ApolloClient({
3333
link: from([errorLink, httpLinkPro]),
3434
cache: new InMemoryCache({
35-
addTypename: true,
3635
typePolicies: {
3736
CragsNear: {
3837
keyFields: ['placeId', '_id']

src/js/hooks/useMediaCmd.tsx

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export default function useMediaCmd (): UseMediaCmdReturn {
5353
QUERY_USER_MEDIA, {
5454
client: graphqlClient,
5555
errorPolicy: 'none',
56-
onError: error => toast.error(error.message),
5756
fetchPolicy: 'network-only'
5857
}
5958
)
@@ -67,6 +66,10 @@ export default function useMediaCmd (): UseMediaCmdReturn {
6766
after
6867
}
6968
})
69+
if (res.error != null) {
70+
toast.error(res.error.message)
71+
return null
72+
}
7073
return res.data?.getUserMediaPagination.mediaConnection ?? null
7174
} catch {
7275
return null
@@ -75,8 +78,7 @@ export default function useMediaCmd (): UseMediaCmdReturn {
7578

7679
const [getMediaByIdGGL] = useLazyQuery<{ media: MediaWithTags }, { id: string }>(QUERY_MEDIA_BY_ID, {
7780
client: graphqlClient,
78-
fetchPolicy: 'network-only',
79-
onError: () => toast.error('Unexpected error. Please try again.')
81+
fetchPolicy: 'network-only'
8082
})
8183

8284
/**
@@ -87,6 +89,10 @@ export default function useMediaCmd (): UseMediaCmdReturn {
8789
const getMediaById: GetMediaByIdCmd = async (id) => {
8890
try {
8991
const res = await getMediaByIdGGL({ variables: { id } })
92+
if (res.error != null) {
93+
toast.error('Unexpected error. Please try again.')
94+
return null
95+
}
9096
return res.data?.media ?? null
9197
} catch {
9298
return null
@@ -96,32 +102,7 @@ export default function useMediaCmd (): UseMediaCmdReturn {
96102
const [addMediaObjects] = useMutation<AddMediaObjectsReturn, AddNewMediaObjectsArgs>(
97103
MUTATION_ADD_MEDIA_OBJECTS, {
98104
client: graphqlClient,
99-
errorPolicy: 'none',
100-
onError: console.error,
101-
onCompleted: (data) => {
102-
/**
103-
* Now update the data store to trigger UserGallery re-rendering.
104-
*/
105-
data.addMediaObjects.forEach(media => {
106-
void getMediaById(media.id)
107-
addNewMediaToUserGallery({
108-
edges: [
109-
{
110-
node: media,
111-
/**
112-
* We don't care about setting cursor because newer images are added to the front
113-
* of the list.
114-
*/
115-
cursor: ''
116-
}
117-
],
118-
pageInfo: {
119-
hasNextPage: true,
120-
endCursor: '' // not supported
121-
}
122-
})
123-
})
124-
}
105+
errorPolicy: 'none'
125106
}
126107
)
127108

@@ -132,14 +113,35 @@ export default function useMediaCmd (): UseMediaCmdReturn {
132113
},
133114
context: apolloClientContext(jwtToken)
134115
})
116+
if (res.errors != null) {
117+
console.error(res.errors)
118+
return null
119+
}
120+
if (res.data != null) {
121+
// Update the data store to trigger UserGallery re-rendering
122+
res.data.addMediaObjects.forEach(media => {
123+
void getMediaById(media.id)
124+
addNewMediaToUserGallery({
125+
edges: [
126+
{
127+
node: media,
128+
cursor: ''
129+
}
130+
],
131+
pageInfo: {
132+
hasNextPage: true,
133+
endCursor: ''
134+
}
135+
})
136+
})
137+
}
135138
return res.data?.addMediaObjects ?? null
136139
}
137140

138141
const [deleteOneMediaObject] = useMutation<DeleteOneMediaObjectReturn, DeleteOneMediaObjectArgs>(
139142
MUTATION_DELETE_ONE_MEDIA_OBJECT, {
140143
client: graphqlClient,
141-
errorPolicy: 'none',
142-
onError: console.error
144+
errorPolicy: 'none'
143145
})
144146

145147
/**
@@ -171,11 +173,7 @@ export default function useMediaCmd (): UseMediaCmdReturn {
171173
const [addEntityTagGQL] = useMutation<AddEntityTagMutationReturn, AddEntityTagProps>(
172174
MUTATION_ADD_ENTITY_TAG, {
173175
client: graphqlClient,
174-
errorPolicy: 'none',
175-
onError: error => toast.error(error.message),
176-
onCompleted: () => {
177-
toast.success('Tag added 🎉')
178-
}
176+
errorPolicy: 'none'
179177
}
180178
)
181179

@@ -190,6 +188,13 @@ export default function useMediaCmd (): UseMediaCmdReturn {
190188
context: apolloClientContext(jwtToken)
191189
})
192190

191+
if (res.errors != null) {
192+
toast.error(res.errors[0]?.message ?? 'Unexpected error')
193+
return [null, null]
194+
}
195+
196+
toast.success('Tag added 🎉')
197+
193198
// refetch the media object to update local cache
194199
const mediaRes = await getMediaById(mediaId)
195200

@@ -208,11 +213,7 @@ export default function useMediaCmd (): UseMediaCmdReturn {
208213

209214
const [removeEntityTagGQL] = useMutation<RemoveEntityTagMutationReturn, RemoveEntityTagMutationProps>(
210215
MUTATION_REMOVE_ENTITY_TAG, {
211-
client: graphqlClient,
212-
onCompleted: () => toast.success('Tag removed.'),
213-
onError: () => {
214-
toast.error(<span>Error deleting tag. <button className='btn btn-xs' onClick={() => window.location.reload()}>Refresh page</button> the browser</span>)
215-
}
216+
client: graphqlClient
216217
}
217218
)
218219

@@ -230,8 +231,12 @@ export default function useMediaCmd (): UseMediaCmdReturn {
230231
})
231232

232233
if (res.errors != null) {
233-
throw new Error('Unexpected API error.')
234+
toast.error(<span>Error deleting tag. <button className='btn btn-xs' onClick={() => window.location.reload()}>Refresh page</button></span>)
235+
return [false, null]
234236
}
237+
238+
toast.success('Tag removed.')
239+
235240
// refetch the media object to update local cache
236241
const mediaRes = await getMediaById(mediaId)
237242

@@ -244,6 +249,7 @@ export default function useMediaCmd (): UseMediaCmdReturn {
244249

245250
return [res.data?.removeEntityTag ?? false, mediaRes]
246251
} catch {
252+
toast.error(<span>Error deleting tag. <button className='btn btn-xs' onClick={() => window.location.reload()}>Refresh page</button></span>)
247253
return [false, null]
248254
}
249255
}

0 commit comments

Comments
 (0)