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

Commit

Permalink
Revert "refactor: avoid creating a new context object when adding opt…
Browse files Browse the repository at this point in the history
…ions"

This reverts commit c85634a.
  • Loading branch information
machi1990 authored and wtrocki committed Jun 29, 2020
1 parent 8451a79 commit 071c274
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
54 changes: 36 additions & 18 deletions packages/graphback-codegen-schema/src/SchemaCRUDPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,12 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
}

const selectedFields = getSelectedFieldsFromResolverInfo(info, model);
context.graphback.options = { selectedFields };
const graphback = {
services: context.graphback.services,
options: { selectedFields }
};

return context.graphback.services[modelName].create(args.input, context);
return context.graphback.services[modelName].create(args.input, {...context, graphback});
}
}

Expand All @@ -463,9 +466,12 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
}

const selectedFields = getSelectedFieldsFromResolverInfo(info, model);
context.graphback.options = { selectedFields };
const graphback = {
services: context.graphback.services,
options: { selectedFields }
};

return context.graphback.services[modelName].update(args.input, context)
return context.graphback.services[modelName].update(args.input, {...context, graphback})
}
}

Expand All @@ -485,9 +491,12 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
}

const selectedFields = getSelectedFieldsFromResolverInfo(info, model);
context.graphback.options = { selectedFields };
const graphback = {
services: context.graphback.services,
options: { selectedFields }
};

return context.graphback.services[modelName].delete(args.input, context)
return context.graphback.services[modelName].delete(args.input, {...context, graphback})
}
}

Expand All @@ -502,15 +511,15 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
const modelName = modelType.name;
const findField = getFieldName(modelName, GraphbackOperationType.FIND);

queryObj[findField] = (_: any, args: any, context: GraphbackContext, info: GraphQLResolveInfo ) => {
if (!context.graphback || !context.graphback.services || !context.graphback.services[modelName]) {
throw new Error(`Missing service for ${modelName}`);
}
queryObj[findField] = async (_: any, args: any, context: GraphbackContext, info: GraphQLResolveInfo ) => {
const selectedFields = getSelectedFieldsFromResolverInfo(info, model, "items");
const count = getSelectedFieldsFromResolverInfo(info, model).some((field: string ) => field === "count");
context.graphback.options = { selectedFields, aggregations: { count } };
const graphback = {
services: context.graphback.services,
options: { selectedFields, aggregations: { count } }
};

return context.graphback.services[modelName].findBy(args.filter, context, args.orderBy, args.page)
return context.graphback.services[modelName].findBy(args.filter, {...context, graphback}, args.orderBy, args.page)
}
}

Expand All @@ -533,9 +542,12 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
}

const selectedFields = getSelectedFieldsFromResolverInfo(info, model);
context.graphback.options = { selectedFields };
const graphback = {
services: context.graphback.services,
options: { selectedFields }
};

return context.graphback.services[modelName].findOne({ [primaryKeyLabel]: args.id }, context)
return context.graphback.services[modelName].findOne({ [primaryKeyLabel]: args.id }, {...context, graphback})
}
}

Expand Down Expand Up @@ -622,13 +634,16 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {

const selectedFields = getSelectedFieldsFromResolverInfo(info, model);
selectedFields.push(relationship.relationForeignKey);
context.graphback.options = { selectedFields };
const graphback = {
services: context.graphback.services,
options: { selectedFields }
};

return context.graphback.services[modelName].batchLoadData(
relationship.relationForeignKey,
parent[relationIdField.name],
args.filter,
context
{...context, graphback}
);
}
}
Expand All @@ -652,9 +667,12 @@ export class SchemaCRUDPlugin extends GraphbackPlugin {
}

const selectedFields = getSelectedFieldsFromResolverInfo(info, model);
context.graphback.options = { selectedFields };
const graphback = {
services: context.graphback.services,
options: { selectedFields }
};

return context.graphback.services[modelName].findOne({ [relationIdField.name]: parent[relationship.relationForeignKey] }, context);
return context.graphback.services[modelName].findOne({ [relationIdField.name]: parent[relationship.relationForeignKey] }, {...context, graphback});
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/graphback-datasync/src/DataSyncPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ export class DataSyncPlugin extends GraphbackPlugin {
}

const selectedFields = getSelectedFieldsFromResolverInfo(info, model, "items");
context.graphback.options = { selectedFields };
const graphback = {
services: context.graphback.services,
options: { selectedFields }
};

return dataSyncService.sync(args.lastSync, context, args.filter);
return dataSyncService.sync(args.lastSync, {...context, graphback }, args.filter);
}
}

Expand Down

0 comments on commit 071c274

Please sign in to comment.