Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AS2: Support APQ for Batches #1234

Merged
merged 3 commits into from
Jun 22, 2018
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
10 changes: 10 additions & 0 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ export async function runHttpQuery(
!optionsObject.persistedQueries ||
!optionsObject.persistedQueries.cache
) {
if (isBatch) {
// A batch can contain another query that returns data,
// so we don't error out the entire request with an HttpError
throw new PersistedQueryNotSupportedError();
}
// Return 200 to simplify processing: we want this to be intepreted by
// the client as data worth interpreting, not an error.
throwHttpGraphQLError(
Expand All @@ -203,6 +208,11 @@ export async function runHttpQuery(
if (queryString === undefined) {
queryString = await optionsObject.persistedQueries.cache.get(sha);
if (!queryString) {
if (isBatch) {
// A batch can contain multiple undefined persisted queries,
// so we don't error out the entire request with an HttpError
throw new PersistedQueryNotFoundError();
}
throwHttpGraphQLError(
200,
[new PersistedQueryNotFoundError()],
Expand Down
56 changes: 56 additions & 0 deletions packages/apollo-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {

describe('Persisted Queries', () => {
const query = '{testString}';
const query2 = '{ testString }';

const hash = sha256
.create()
Expand All @@ -954,6 +955,16 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
},
};

const extensions2 = {
persistedQuery: {
version: VERSION,
sha256Hash: sha256
.create()
.update(query2)
.hex(),
},
};

beforeEach(async () => {
const map = new Map<string, string>();
const cache = {
Expand Down Expand Up @@ -1005,6 +1016,51 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
expect(result.body.errors).not.to.exist;
});

it('returns with batched persisted queries', async () => {
const errors = await request(app)
.post('/graphql')
.send([
{
extensions,
},
{
extensions: extensions2,
},
]);

expect(errors.body[0].data).to.not.exist;
expect(errors.body[1].data).to.not.exist;
expect(errors.body[0].errors[0].message).to.equal(
'PersistedQueryNotFound',
);
expect(errors.body[0].errors[0].extensions.code).to.equal(
'PERSISTED_QUERY_NOT_FOUND',
);
expect(errors.body[1].errors[0].message).to.equal(
'PersistedQueryNotFound',
);
expect(errors.body[1].errors[0].extensions.code).to.equal(
'PERSISTED_QUERY_NOT_FOUND',
);

const result = await request(app)
.post('/graphql')
.send([
{
extensions,
query,
},
{
extensions: extensions2,
query: query2,
},
]);

expect(result.body[0].data).to.deep.equal({ testString: 'it works' });
expect(result.body[0].data).to.deep.equal({ testString: 'it works' });
expect(result.body.errors).not.to.exist;
});

it('returns result on the persisted query', async () => {
await request(app)
.post('/graphql')
Expand Down