1- import ApolloClient from 'apollo-boost ' ;
1+ import { ApolloClient , ApolloLink , HttpLink , InMemoryCache } from '@ apollo/client/core ' ;
22import debug from 'debug' ;
33import gql from 'graphql-tag' ;
44import { flatten , pick , uniqBy } from 'lodash' ;
@@ -25,10 +25,30 @@ const getGraphqlUrl = ({ version = 'v1' } = {}) => {
2525
2626let client ;
2727
28+ /**
29+ * @returns {ApolloClient }
30+ */
2831function getClient ( ) {
2932 if ( ! client ) {
30- // client = new GraphQLClient(getGraphqlUrl(), { headers });
31- client = new ApolloClient ( { fetch, uri : getGraphqlUrl ( { version : 'v1' } ) } ) ;
33+ client = new ApolloClient ( {
34+ cache : new InMemoryCache ( ) ,
35+ link : ApolloLink . from ( [
36+ new HttpLink ( {
37+ fetch,
38+ uri : getGraphqlUrl ( { version : 'v1' } ) ,
39+ } ) ,
40+ ] ) ,
41+ defaultOptions : {
42+ watchQuery : {
43+ fetchPolicy : 'no-cache' ,
44+ errorPolicy : 'all' ,
45+ } ,
46+ query : {
47+ fetchPolicy : 'no-cache' ,
48+ errorPolicy : 'all' ,
49+ } ,
50+ } ,
51+ } ) ;
3252 }
3353 return client ;
3454}
@@ -38,9 +58,7 @@ function graphqlRequest(query, variables) {
3858 // return getClient().request(query, variables);
3959
4060 // With ApolloClient as client
41- return getClient ( )
42- . query ( { query, variables, fetchPolicy : 'network-only' } )
43- . then ( ( result ) => result . data ) ;
61+ return getClient ( ) . query ( { query, variables, fetchPolicy : 'no-cache' } ) ;
4462}
4563
4664/*
@@ -59,16 +77,20 @@ export async function fetchCollective(collectiveSlug) {
5977 backgroundImage
6078 isGuest
6179 parentCollective {
80+ id
6281 image
6382 backgroundImage
6483 }
6584 }
6685 }
6786 ` ;
6887
69- const result = await graphqlRequest ( query , { collectiveSlug } ) ;
88+ const { data, errors } = await graphqlRequest ( query , { collectiveSlug } ) ;
89+ if ( errors ?. length ) {
90+ throw new Error ( errors [ 0 ] . message ) ;
91+ }
7092
71- return result . Collective ;
93+ return data . Collective ;
7294}
7395
7496export async function fetchCollectiveWithCache ( collectiveSlug , options = { } ) {
@@ -94,7 +116,9 @@ export async function fetchMembersStats(variables) {
94116 query = gql `
95117 query fetchMembersStats($collectiveSlug: String) {
96118 Collective(slug: $collectiveSlug) {
119+ id
97120 stats {
121+ id
98122 backers {
99123 all
100124 users
@@ -122,7 +146,9 @@ export async function fetchMembersStats(variables) {
122146 query = gql `
123147 query fetchMembersStatsForTier($collectiveSlug: String, $tierSlug: String) {
124148 Collective(slug: $collectiveSlug) {
149+ id
125150 tiers(slug: $tierSlug) {
151+ id
126152 slug
127153 name
128154 stats {
@@ -143,8 +169,11 @@ export async function fetchMembersStats(variables) {
143169 } ;
144170 } ;
145171 }
146- const result = await graphqlRequest ( query , variables ) ;
147- const count = processResult ( result ) ;
172+ const { data, errors } = await graphqlRequest ( query , variables ) ;
173+ if ( errors ?. length ) {
174+ throw new Error ( errors [ 0 ] . message ) ;
175+ }
176+ const count = processResult ( data ) ;
148177 return count ;
149178}
150179
@@ -218,9 +247,13 @@ export async function fetchMembers({ collectiveSlug, tierSlug, backerType, isAct
218247 query = gql `
219248 query fetchMembersWithTier($collectiveSlug: String, $tierSlug: [String], $isActive: Boolean) {
220249 Collective(slug: $collectiveSlug) {
250+ id
221251 tiers(slugs: $tierSlug) {
252+ id
222253 orders(isActive: $isActive, isProcessed: true) {
254+ id
223255 fromCollective {
256+ id
224257 type
225258 slug
226259 name
@@ -241,14 +274,17 @@ export async function fetchMembers({ collectiveSlug, tierSlug, backerType, isAct
241274 } ;
242275 }
243276
244- const result = await graphqlRequest ( query , {
277+ const { data , errors } = await graphqlRequest ( query , {
245278 collectiveSlug,
246279 tierSlug,
247280 type,
248281 role,
249282 isActive,
250283 } ) ;
251- const members = processResult ( result ) ;
284+ if ( errors ?. length ) {
285+ throw new Error ( errors [ 0 ] . message ) ;
286+ }
287+ const members = processResult ( data ) ;
252288 return members ;
253289}
254290
0 commit comments