Skip to content

Commit

Permalink
change log to error, add errorLink to subgraph apollo client
Browse files Browse the repository at this point in the history
  • Loading branch information
L03TJ3 committed Feb 2, 2024
1 parent cf236a4 commit 5bd2def
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
16 changes: 2 additions & 14 deletions packages/app/src/hooks/apollo/useCreateMongoDbApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { useEffect, useState } from 'react';
import * as Realm from 'realm-web';
import { InvalidationPolicyCache, RenewalPolicy } from '@nerdwallet/apollo-cache-policies';
import { ApolloClient, from, HttpLink, NormalizedCacheObject } from '@apollo/client';
import { onError } from '@apollo/client/link/error';
import { RetryLink } from '@apollo/client/link/retry';
import { AsyncStorageWrapper, persistCache } from 'apollo3-cache-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';

import { errorLink } from '../../utils/errorLink';

const APP_ID = 'wallet_prod-obclo';
const mongoDbUri = `https://realm.mongodb.com/api/client/v2.0/app/${APP_ID}/graphql`;

Expand Down Expand Up @@ -44,18 +44,6 @@ export const useCreateMongoDbApolloClient = (): ApolloClient<any> | undefined =>
storage: new AsyncStorageWrapper(AsyncStorage),
});

// ref:https://www.apollographql.com/docs/react/data/error-handling/#advanced-error-handling-with-apollo-link
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
);
if (networkError) {
console.error(`[Network error]: ${networkError}`);
throw networkError;
}
});

const httpLink = new HttpLink({
uri: mongoDbUri,
fetch: async (uri, options) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useEffect, useState } from 'react';
import { ApolloClient, InMemoryCache, NormalizedCacheObject } from '@apollo/client';
import { ApolloClient, from, InMemoryCache, NormalizedCacheObject } from '@apollo/client';
import { AsyncStorageWrapper, persistCache } from 'apollo3-cache-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';

import { errorLink } from '../../utils/errorLink';

const subgraphUri = 'https://api.thegraph.com/subgraphs/name/gooddollar/goodcollective';

export const useCreateSubgraphApolloClient = (): ApolloClient<any> | undefined => {
Expand All @@ -17,6 +19,7 @@ export const useCreateSubgraphApolloClient = (): ApolloClient<any> | undefined =
});
const client = new ApolloClient({
uri: subgraphUri,
link: from([errorLink]),
cache,
defaultOptions: {
watchQuery: {
Expand Down
13 changes: 13 additions & 0 deletions packages/app/src/utils/errorLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { onError } from '@apollo/client/link/error';

// ref:https://www.apollographql.com/docs/react/data/error-handling/#advanced-error-handling-with-apollo-link
export const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) =>
console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
);
if (networkError) {
console.error(`[Network error]: ${networkError}`);
throw networkError;
}
});

0 comments on commit 5bd2def

Please sign in to comment.