Skip to content
This repository has been archived by the owner on Sep 30, 2019. It is now read-only.

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure committed Mar 12, 2019
1 parent 2e55624 commit 7878e1c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
22 changes: 9 additions & 13 deletions packages/appsync-emulator-serverless/__test__/example/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ module.exports.graphqlJSON = (event, context, callback) =>
});

module.exports.graphqlArrayJSON = (event, context, callback) =>
callback(null, [
{test: 'yup.0'},
{test: 'yup.1'}
]);


module.exports.graphqlBatchJSON = (events, context, callback) =>
callback(null, events.map( (e, index) => {
return [
{test: `${e.source.test}.0`},
{test: `${e.source.test}.1`}
];
}));
callback(null, [{ test: 'yup.0' }, { test: 'yup.1' }]);

module.exports.graphqlBatchJSON = (events, context, callback) =>
callback(
null,
events.map(e => [
{ test: `${e.source.test}.0` },
{ test: `${e.source.test}.1` },
]),
);

module.exports.graphql = (event, context, callback) => {
const field = Object.keys(event.arguments)[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('creates executable schema', () => {
expect(result).toMatchObject({ data: { lambda: { test: 'yup' } } });
});

it.only('should allow querying lambda in batch', async () => {
it('should allow querying lambda in batch', async () => {
const result = await graphql({
schema,
contextValue,
Expand Down
54 changes: 30 additions & 24 deletions packages/appsync-emulator-serverless/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,32 +188,37 @@ const dispatchRequestToSource = async (
}
};

const batchLoaders = {};
const batchRequests = {};

const getBatchLoader = (fieldPath, source, configs) => {
if (batchLoaders[fieldPath] === undefined) {
batchRequests[fieldPath] = {};
batchLoaders[fieldPath] = new DataLoader(keys => {
// Group requests together and execute them in batch
let batchRequest = batchRequests[fieldPath][keys[0]];
batchRequest.payload = keys.map(k => batchRequests[fieldPath][k].payload);
consola.info(
'Rendered Batch Request:\n',
inspect(batchRequest, { depth: null, colors: true }),
const generateDataLoaderResolver = (source, configs) => {
const batchLoaders = {};
return fieldPath => {
if (batchLoaders[fieldPath] === undefined) {
consola.info('load', fieldPath);
batchLoaders[fieldPath] = new DataLoader(
requests => {
consola.info('requests:', requests);
const batchRequest = requests[0];
batchRequest.payload = requests.map(r => r.payload);
consola.info(
'Rendered Batch Request:\n',
inspect(batchRequest, { depth: null, colors: true }),
);
log.info('resolver batch request', batchRequest);
return dispatchRequestToSource(source, configs, batchRequest);
},
{
shouldCache: false,
},
);
log.info('resolver batch request', batchRequest);
return dispatchRequestToSource(source, configs, batchRequest);
});
}
}

return batchLoaders[fieldPath];
return batchLoaders[fieldPath];
};
};

const generateTypeResolver = (
source,
configs,
{ requestPath, responsePath },
{ requestPath, responsePath, dataLoaderResolver },
) => async (root, vars, context, info) => {
try {
const fieldPath = `${info.parentType}.${info.fieldName}`;
Expand All @@ -227,11 +232,10 @@ const generateTypeResolver = (

let requestResult;
if (request.operation === 'BatchInvoke') {
const loader = getBatchLoader(fieldPath, source, configs);
// put request in batch, this will be used by the data loader
let key = pathInfo.join('.');
batchRequests[fieldPath][key] = request;
requestResult = await loader.load(key);
consola.info("Batch invoke!!!", fieldPath, dataLoaderResolver);
const loader = dataLoaderResolver(fieldPath);
consola.info("Batch invoke!!!", loader);
requestResult = await loader.load(request);
} else {
consola.info(
'Rendered Request:\n',
Expand All @@ -256,6 +260,7 @@ const generateTypeResolver = (
} catch (err) {
consola.error(`${info.parentType}.${info.fieldName} failed`);
consola.error(err.errorMessage || err.stack || err);
consola.error(JSON.stringify(err));
throw err;
}
};
Expand Down Expand Up @@ -336,6 +341,7 @@ const generateResolvers = (cwd, config, configs) => {
const source = dataSourceByName[dataSource];
const pathing = {
requestPath: path.join(mappingTemplates, request),
dataLoaderResolver: generateDataLoaderResolver(source, configs),
responsePath: path.join(mappingTemplates, response),
};
const resolver =
Expand Down
2 changes: 1 addition & 1 deletion packages/appsync-emulator-serverless/schemaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const uuid = require('uuid/v4');
const { createSchema: createSchemaCore } = require('./schema');
const { PubSub } = require('graphql-subscriptions');
const { wrapSchema } = require('./schemaWrapper');
const { wrapSchema } = require('./schemaWrapper')

const setupDocumentDB = async (serverlessConfig, appSyncConfig, dynamodb) => {
const {
Expand Down

0 comments on commit 7878e1c

Please sign in to comment.