Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 2x 2x 2x 2x 2x 5x 5x 5x 10x 5x 5x 5x 1x 4x 1x 3x 3x 3x 3x 3x 2x 3x 3x 7x 3x 3x 3x 3x 3x 3x 3x 3x 2x 1x 1x | import http from 'http';
import {GraphqlClient} from '../clients/graphql';
import {RequestReturn} from '../clients/http_client/types';
import * as ShopifyErrors from '../error';
import loadCurrentSession from './load-current-session';
export default async function graphqlProxy(
userReq: http.IncomingMessage,
userRes: http.ServerResponse,
): Promise<RequestReturn> {
const session = await loadCurrentSession(userReq, userRes);
if (!session) {
throw new ShopifyErrors.SessionNotFound(
'Cannot proxy query. No session found.',
);
} else if (!session.accessToken) {
throw new ShopifyErrors.InvalidSession(
'Cannot proxy query. Session not authenticated.',
);
}
const shopName: string = session.shop;
const token: string = session.accessToken;
let reqBodyString = '';
return new Promise((resolve, reject) => {
userReq.on('data', (chunk) => {
reqBodyString += chunk;
});
userReq.on('end', async () => {
let reqBodyObject: {[key: string]: unknown} | undefined;
try {
reqBodyObject = JSON.parse(reqBodyString);
} catch (err) {
// we can just continue and attempt to pass the string
}
try {
const options = {
data: reqBodyObject ? reqBodyObject : reqBodyString,
};
const client = new GraphqlClient(shopName, token);
const response = await client.query(options);
return resolve(response);
} catch (err) {
return reject(err);
}
});
});
}
|