import { Middleware , RelayRequestAny } from "react-relay-network-modern/es" ; import { modifyQuery } from "coral-framework/utils" ; function hasMutations ( req ) { return req . isMutation ( ) ; } function queriesAreEmpty ( req ) { return req . getQueryString ( ) === "" ; } function hasToken ( req ) { return "Authorization" in req . fetchOpts . headers ; } /**
 * persistedQueriesGetMethodMiddleware will use the GET method instead of POST for
 * all request excluding mutations when persisted queries are used.
 * The request data will be encoded in base64url and set in the GET query string under
 * the variable "d=".
 */ const persistedQueriesGetMethodMiddleware = ( next ) => async ( req ) => { if ( queriesAreEmpty ( req ) && ! hasMutations ( req ) && ! hasToken ( req ) ) { const body = JSON . parse ( req . fetchOpts . body as string ) ; delete req . fetchOpts . body ; req . fetchOpts . method = "GET" ; const params = { query : "" } ; for ( const key in body ) { if ( ! Object . prototype . hasOwnProperty . call ( body , key ) ) { continue ; } const value = body [ key ] ; params [ key ] = typeof value === "string" ? value : JSON . stringify ( value ) ; } req . fetchOpts . url = modifyQuery ( req . fetchOpts . url as string , params ) ; } return next ( req ) ; } ; export default persistedQueriesGetMethodMiddleware ;