1- import { ApolloClient , FetchPolicy } from ' apollo-client' ;
2- import { InMemoryCache } from ' apollo-cache-inmemory' ;
3- import { HttpLink } from ' apollo-link-http' ;
4- import { ApolloLink } from ' apollo-link' ;
5- import Context from ' ../common/context' ;
6- import { Arguments , Data } from ' ../support/interfaces' ;
7- import Transformer from ' ./transformer' ;
8- import Model from ' ../orm/model' ;
9- import gql from ' graphql-tag' ;
1+ import { ApolloClient , FetchPolicy } from " apollo-client" ;
2+ import { InMemoryCache } from " apollo-cache-inmemory" ;
3+ import { HttpLink } from " apollo-link-http" ;
4+ import { ApolloLink } from " apollo-link" ;
5+ import Context from " ../common/context" ;
6+ import { Arguments , Data } from " ../support/interfaces" ;
7+ import Transformer from " ./transformer" ;
8+ import Model from " ../orm/model" ;
9+ import gql from " graphql-tag" ;
1010
1111/**
1212 * This class takes care of the communication with the graphql endpoint by leveraging the awesome apollo-client lib.
@@ -27,16 +27,17 @@ export default class Apollo {
2727 /**
2828 * @constructor
2929 */
30- public constructor ( ) {
30+ public constructor ( ) {
3131 const context = Context . getInstance ( ) ;
3232
3333 // This allows the test suite to pass a custom link
3434 if ( context . options . link ) {
3535 this . httpLink = context . options . link ;
3636 } else {
37+ /* istanbul ignore next */
3738 this . httpLink = new HttpLink ( {
38- uri : context . options . url ? context . options . url : ' /graphql' ,
39- credentials : context . options . credentials ? context . options . credentials : ' same-origin' ,
39+ uri : context . options . url ? context . options . url : " /graphql" ,
40+ credentials : context . options . credentials ? context . options . credentials : " same-origin" ,
4041 useGETForQueries : Boolean ( context . options . useGETForQueries )
4142 } ) ;
4243 }
@@ -57,10 +58,14 @@ export default class Apollo {
5758 * @param {boolean } bypassCache If true the query will be send to the server without using the cache. For queries only
5859 * @returns {Promise<Data> } The new records
5960 */
60- public async request ( model : Model , query : any , variables ?: Arguments , mutation : boolean = false ,
61- bypassCache : boolean = false ) : Promise < Data > {
62-
63- const fetchPolicy : FetchPolicy = bypassCache ? 'network-only' : 'cache-first' ;
61+ public async request (
62+ model : Model ,
63+ query : any ,
64+ variables ?: Arguments ,
65+ mutation : boolean = false ,
66+ bypassCache : boolean = false
67+ ) : Promise < Data > {
68+ const fetchPolicy : FetchPolicy = bypassCache ? "network-only" : "cache-first" ;
6469 Context . getInstance ( ) . logger . logQuery ( query , variables , fetchPolicy ) ;
6570
6671 const context = { headers : Apollo . getHeaders ( ) } ;
@@ -76,16 +81,30 @@ export default class Apollo {
7681 return Transformer . transformIncomingData ( response . data as Data , model , mutation ) ;
7782 }
7883
79- public async simpleQuery ( query : string , variables : Arguments , bypassCache : boolean = false , context ?: Data ) : Promise < any > {
80- const fetchPolicy : FetchPolicy = bypassCache ? 'network-only' : 'cache-first' ;
81- return this . apolloClient . query ( { query : gql ( query ) , variables, fetchPolicy, context : { headers : Apollo . getHeaders ( ) } } ) ;
84+ public async simpleQuery (
85+ query : string ,
86+ variables : Arguments ,
87+ bypassCache : boolean = false ,
88+ context ?: Data
89+ ) : Promise < any > {
90+ const fetchPolicy : FetchPolicy = bypassCache ? "network-only" : "cache-first" ;
91+ return this . apolloClient . query ( {
92+ query : gql ( query ) ,
93+ variables,
94+ fetchPolicy,
95+ context : { headers : Apollo . getHeaders ( ) }
96+ } ) ;
8297 }
8398
84- public async simpleMutation ( query : string , variables : Arguments , context ?: Data ) : Promise < any > {
85- return this . apolloClient . mutate ( { mutation : gql ( query ) , variables, context : { headers : Apollo . getHeaders ( ) } } ) ;
99+ public async simpleMutation ( query : string , variables : Arguments , context ?: Data ) : Promise < any > {
100+ return this . apolloClient . mutate ( {
101+ mutation : gql ( query ) ,
102+ variables,
103+ context : { headers : Apollo . getHeaders ( ) }
104+ } ) ;
86105 }
87106
88- private static getHeaders ( ) {
107+ private static getHeaders ( ) {
89108 const context = Context . getInstance ( ) ;
90109
91110 let headers : Object | Function = context . options . headers ? context . options . headers : { } ;
0 commit comments