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

Some improvements I made #153

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fb16c43
add the new method to trait
stevefan1999-personal Jul 28, 2020
4e2b6dd
supply a default argument filter name 'filter'
stevefan1999-personal Jul 28, 2020
0d64750
set the filter name to use it from adapter
stevefan1999-personal Jul 28, 2020
38ba506
use more destructing to keep variables in one place
stevefan1999-personal Jul 28, 2020
df43a95
implement /should(Query/Mutation)AlwaysUsePluralStyle/
stevefan1999-personal Jul 28, 2020
c9419be
some refactors on query builder
stevefan1999-personal Jul 28, 2020
2fd3f6c
fix types not generated into right place
stevefan1999-personal Jul 28, 2020
e27e779
say no more to forEach functions that are slow and sluggish
stevefan1999-personal Jul 28, 2020
83d2c03
abstract more var to destructing
stevefan1999-personal Jul 28, 2020
b8dd455
optimize prepare arguments to express 'flattening' to list
stevefan1999-personal Jul 28, 2020
1836ffe
tslint chore
stevefan1999-personal Jul 28, 2020
5def328
use list spreading to make it look better and optimize search conditions
stevefan1999-personal Jul 28, 2020
b1f496d
don't use String but string which is almost equivalent
stevefan1999-personal Jul 28, 2020
36f1b1f
add downlevelIteration to allow map iteration
stevefan1999-personal Jul 28, 2020
6ba9f9a
update package.json
stevefan1999-personal Jul 28, 2020
35da181
set should always use plural style option to what i think the most ap…
stevefan1999-personal Jul 28, 2020
2afc138
stop yarning about frozen lock
stevefan1999-personal Jul 28, 2020
1e5f588
improve insert data to have less async mindf*ck
stevefan1999-personal Jul 28, 2020
054a936
allow overriding with plural name (for hacky case like hasura that do…
stevefan1999-personal Jul 28, 2020
05d6367
tabularize instanceof for clutters
stevefan1999-personal Jul 28, 2020
d4118d1
null coalesce
stevefan1999-personal Jul 28, 2020
a2d7ec8
remove an entire block that is redundant
stevefan1999-personal Jul 28, 2020
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
84 changes: 42 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,64 +53,64 @@
"url": "https://github.com/vuex-orm/plugin-graphql/issues"
},
"dependencies": {
"apollo-cache-inmemory": "^1.6.5",
"apollo-cache-inmemory": "^1.6.6",
"app": "latest"
},
"peerDependencies": {
"@vuex-orm/core": "^0.36.3"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@types/jest": "^25.2.1",
"@types/node": "^13.11.0",
"@types/sinon": "^9.0.0",
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^9.1.1",
"@types/jest": "^26.0.7",
"@types/node": "^14.0.26",
"@types/sinon": "^9.0.4",
"@vuex-orm/core": "^0.36.3",
"apollo-client": "^2.6.8",
"apollo-link": "^1.2.0",
"apollo-link-http": "^1.3.2",
"apollo-link-schema": "^1.1.0",
"apollo-client": "^2.6.10",
"apollo-link": "^1.2.14",
"apollo-link-http": "^1.5.17",
"apollo-link-schema": "^1.2.5",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.7.0",
"colors": "^1.3.2",
"commitizen": "^3.0.0",
"coveralls": "^3.0.2",
"cross-env": "^5.2.0",
"cross-fetch": "^3.0.2",
"cz-conventional-changelog": "^2.1.0",
"graphql": "^14.6.0",
"graphql-tag": "^2.10.2",
"graphql-tools": "^4.0.7",
"husky": "^1.0.1",
"jest": "^25.2.7",
"jest-config": "^25.2.7",
"lint-staged": "^7.3.0",
"colors": "^1.4.0",
"commitizen": "^4.1.2",
"coveralls": "^3.1.0",
"cross-env": "^7.0.2",
"cross-fetch": "^3.0.5",
"cz-conventional-changelog": "^3.2.0",
"graphql": "^15.3.0",
"graphql-tag": "^2.10.4",
"graphql-tools": "^6.0.15",
"husky": "^4.2.5",
"jest": "^26.1.0",
"jest-config": "^26.1.0",
"lint-staged": "^10.2.11",
"lodash.clone": "^4.5.0",
"lodash.isequal": "^4.5.0",
"node-fetch": "^2.1.1",
"normalizr": "^3.2.4",
"pluralize": "^7.0.0",
"prettier": "^1.14.3",
"node-fetch": "^2.6.0",
"normalizr": "^3.6.0",
"pluralize": "^8.0.0",
"prettier": "^2.0.5",
"prompt": "^1.0.0",
"replace-in-file": "^3.4.2",
"rimraf": "^2.6.2",
"rollup": "^2.3.2",
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.27.0",
"replace-in-file": "^6.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.23.0",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"rollup-plugin-terser": "^6.1.0",
"rollup-plugin-typescript2": "^0.27.1",
"brotli": "^1.3.2",
"sinon": "^6.0.0",
"ts-jest": "^25.3.1",
"ts-node": "^8.8.2",
"tslint": "^6.1.1",
"tslint-config-prettier": "^1.15.0",
"sinon": "^9.0.2",
"ts-jest": "^26.1.4",
"ts-node": "^8.10.2",
"tslint": "^6.1.2",
"tslint-config-prettier": "^1.18.0",
"tslint-config-standard": "^9.0.0",
"typescript": "^3.8.3",
"typescript": "^3.9.7",
"vue": "^2.6.11",
"vue-server-renderer": "^2.6.11",
"vuepress": "^1.3.0",
"vuex": "^3.1.3"
"vuepress": "^1.5.2",
"vuex": "^3.5.1"
},
"jest": {
"transform": {
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function createEntry(config) {
check: config.format === 'es' && config.browser && config.env === 'development',
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
useTsconfigDeclarationDir: true,
tsconfigOverride: {
compilerOptions: {
declaration: config.format === 'es' && config.browser && config.env === 'development',
Expand Down
25 changes: 13 additions & 12 deletions src/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ export default class Action {
model: Model
): Promise<any> {
if (variables) {
const context: Context = Context.getInstance();
const schema: Schema = await context.loadSchema();
const context = Context.getInstance();
const { adapter, apollo, logger } = context;
const schema = await context.loadSchema();

const multiple: boolean = Schema.returnsConnection(schema.getMutation(name)!);
const query = QueryBuilder.buildQuery("mutation", model, name, variables, multiple);

// Send GraphQL Mutation
let newData = await context.apollo.request(model, query, variables, true);
let newData = await apollo.request(model, query, variables, true);

// When this was not a destroy action, we get new data, which we should insert in the store
if (name !== context.adapter.getNameForDestroy(model)) {
if (name !== adapter.getNameForDestroy(model)) {
newData = newData[Object.keys(newData)[0]];

// IDs as String cause terrible issues, so we convert them to integers.
Expand All @@ -56,7 +57,7 @@ export default class Action {
if (newRecord) {
return newRecord;
} else {
Context.getInstance().logger.log(
logger.log(
"Couldn't find the record of type '",
model.pluralName,
"' within",
Expand Down Expand Up @@ -115,14 +116,14 @@ export default class Action {
*/
protected static transformArgs(args: Arguments): Arguments {
const context = Context.getInstance();
const { components, logger } = context;

Object.keys(args).forEach((key: string) => {
const value: Data = args[key];

if (value instanceof context.components.Model) {
for (const [key, value] of Object.entries(args)) {
if (value instanceof components.Model) {
const model = context.getModel(singularize(value.$self().entity));
const transformedValue = Transformer.transformOutgoingData(model, value, false);
context.logger.log(
const transformedValue = Transformer.transformOutgoingData(model, value as Data, false);

logger.log(
"A",
key,
"model was found within the variables and will be transformed from",
Expand All @@ -132,7 +133,7 @@ export default class Action {
);
args[key] = transformedValue;
}
});
}

return args;
}
Expand Down
11 changes: 6 additions & 5 deletions src/actions/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class Fetch extends Action {
params?: ActionParams
): Promise<Data> {
const context = Context.getInstance();
const { adapter, apollo } = context;
const model = this.getModelFromState(state!);

const mockReturnValue = model.$mockHook("fetch", {
Expand All @@ -53,7 +54,7 @@ export default class Fetch extends Action {
// Filter
let filter = {};

if (params && params.filter) {
if (params?.filter) {
filter = Transformer.transformOutgoingData(
model,
params.filter as Data,
Expand All @@ -62,15 +63,15 @@ export default class Fetch extends Action {
);
}

const bypassCache = params && params.bypassCache;
const bypassCache = params?.bypassCache;

// When the filter contains an id, we query in singular mode
const multiple: boolean = !filter["id"];
const name: string = context.adapter.getNameForFetch(model, multiple);
const multiple: boolean = adapter.shouldQueryAlwaysUsePluralStyle() || !filter["id"];
const name: string = adapter.getNameForFetch(model, multiple);
const query = QueryBuilder.buildQuery("query", model, name, filter, multiple, multiple);

// Send the request to the GraphQL API
const data = await context.apollo.request(model, query, filter, false, bypassCache as boolean);
const data = await apollo.request(model, query, filter, false, bypassCache as boolean);

// Insert incoming data into the store
return Store.insertData(data, dispatch!);
Expand Down
5 changes: 3 additions & 2 deletions src/actions/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ export default class Persist extends Action {
* In this case Action.mutation has inserted a new record instead of updating the existing one.
*
* @param {Model} model
* @param {Data} record
* @param newRecord
* @param oldRecord
* @returns {Promise<void>}
*/
private static async deleteObsoleteRecord(model: Model, newRecord: Data, oldRecord: Data) {
if (newRecord && oldRecord && newRecord.id !== oldRecord.id) {
if (newRecord?.id !== oldRecord?.id) {
Context.getInstance().logger.log("Dropping deprecated record", oldRecord);
return oldRecord.$delete();
}
Expand Down
7 changes: 7 additions & 0 deletions src/adapters/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ export default interface Adapter {
getInputTypeName(model: Model, action?: string): string;

prepareSchemaTypeName(name: string): string;

getNameForArgumentFilter(): string;

shouldQueryAlwaysUsePluralStyle(): boolean;
shouldMutationAlwaysUsePluralStyle(): boolean;

shouldTryPluralNameIfSchemaNameNotFoundWithSingular(): boolean;
}
16 changes: 16 additions & 0 deletions src/adapters/builtin/default-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ export default class DefaultAdapter implements Adapter {
prepareSchemaTypeName(name: string): string {
return upcaseFirstLetter(name);
}

getNameForArgumentFilter(): string {
return "filter";
}

shouldQueryAlwaysUsePluralStyle(): boolean {
return false;
}

shouldMutationAlwaysUsePluralStyle(): boolean {
return false;
}

shouldTryPluralNameIfSchemaNameNotFoundWithSingular(): boolean {
return false;
}
}
33 changes: 24 additions & 9 deletions src/common/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,30 +182,45 @@ export default class Context {
}

public processSchema() {
this.models.forEach((model: Model) => {

for (const [_, model] of this.models) {
let type: GraphQLType;

let unableToResolveEntity = false;

try {
type = this.schema!.getType(model.singularName)!;
} catch (error) {
} catch {
if (this.adapter.shouldTryPluralNameIfSchemaNameNotFoundWithSingular()) {
try {
type = this.schema!.getType(model.pluralName)!;
} catch {
unableToResolveEntity = true;
}
} else {
unableToResolveEntity = true;
}
}

if (unableToResolveEntity) {
this.logger.warn(`Ignoring entity ${model.singularName} because it's not in the schema.`);
return;
}

model.fields.forEach((field: Field, fieldName: string) => {
if (!type.fields!.find(f => f.name === fieldName)) {
for (const [fieldName, _] of model.fields) {
if (!type!.fields!.find(f => f.name === fieldName)) {
this.logger.warn(
`Ignoring field ${model.singularName}.${fieldName} because it's not in the schema.`
);

// TODO: Move skipFields to the model
model.baseModel.skipFields = model.baseModel.skipFields ? model.baseModel.skipFields : [];
model.baseModel.skipFields = model.baseModel.skipFields ?? [];
if (!model.baseModel.skipFields.includes(fieldName)) {
model.baseModel.skipFields.push(fieldName);
}
}
});
});
}
}

if (this.connectionMode === ConnectionMode.AUTO) {
this.connectionMode = this.schema!.determineQueryMode();
Expand Down Expand Up @@ -296,10 +311,10 @@ export default class Context {
* Wraps all Vuex-ORM entities in a Model object and saves them into this.models
*/
private collectModels() {
this.database.entities.forEach((entity: any) => {
for (const entity of this.database.entities) {
const model: Model = new Model(entity.model as typeof ORMModel);
this.models.set(model.singularName, model);
Model.augment(model);
});
}
}
}
Loading