Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Stop sending request response from the GraphQL proxy #312

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

### Fixed

- ⚠️ [Breaking] Stop responding to the request in the GraphQL Proxy function, returning Shopify's response instead [#312](https://github.com/Shopify/shopify-node-api/pull/312)

## [2.1.0] - 2022-02-03

### Added
Expand Down
19 changes: 12 additions & 7 deletions src/utils/__tests__/graphql_proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'http';

import jwt from 'jsonwebtoken';
import express from 'express';
import express, {Request, Response} from 'express';
import request from 'supertest';

import {Session} from '../../auth/session';
Expand Down Expand Up @@ -39,6 +39,17 @@ const accessToken = 'dangit';
let token = '';

describe('GraphQL proxy with session', () => {
const app = express();
app.post('/proxy', async (req: Request, res: Response) => {
try {
const response = await graphqlProxy(req, res);
res.send(response.body);
} catch (err) {
res.status(400);
res.send(JSON.stringify(err.message));
}
});

beforeEach(async () => {
Context.IS_EMBEDDED_APP = true;
Context.initialize(Context);
Expand Down Expand Up @@ -68,9 +79,6 @@ describe('GraphQL proxy with session', () => {
});

it('can forward query and return response', async () => {
const app = express();
app.post('/proxy', graphqlProxy);

fetchMock.mockResponses(
JSON.stringify(successResponse),
JSON.stringify(successResponse),
Expand All @@ -94,9 +102,6 @@ describe('GraphQL proxy with session', () => {
});

it('rejects if no query', async () => {
const app = express();
app.post('/proxy', graphqlProxy);

const response = await request(app)
.post('/proxy')
.set('authorization', `Bearer ${token}`)
Expand Down
30 changes: 5 additions & 25 deletions src/utils/graphql_proxy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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<void> {
): Promise<RequestReturn> {
const session = await loadCurrentSession(userReq, userRes);
if (!session) {
throw new ShopifyErrors.SessionNotFound(
Expand All @@ -24,7 +25,7 @@ export default async function graphqlProxy(
const token: string = session.accessToken;
let reqBodyString = '';

const promise: Promise<void> = new Promise((resolve, _reject) => {
return new Promise((resolve, reject) => {
userReq.on('data', (chunk) => {
reqBodyString += chunk;
});
Expand All @@ -37,37 +38,16 @@ export default async function graphqlProxy(
// we can just continue and attempt to pass the string
}

let status = 200;
let body: unknown = '';

try {
const options = {
data: reqBodyObject ? reqBodyObject : reqBodyString,
};
const client = new GraphqlClient(shopName, token);
const response = await client.query(options);
body = response.body;
return resolve(response);
} catch (err) {
switch (err.constructor.name) {
case 'MissingRequiredArgument':
status = 400;
break;
case 'HttpResponseError':
status = err.code;
break;
case 'HttpThrottlingError':
status = 429;
break;
default:
status = 500;
}
body = err.message;
} finally {
userRes.statusCode = status;
userRes.end(JSON.stringify(body));
return reject(err);
}
return resolve();
});
});
return promise;
}