Skip to content
Open
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: 1 addition & 1 deletion dist/vuex-orm-graphql.es5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vuex-orm-graphql.es5.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vuex-orm-graphql.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vuex-orm-graphql.umd.js.map

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vuex-orm/plugin-graphql",
"version": "1.0.0-rc.36",
"version": "1.0.0-jr03",
"description": "Vuex-ORM persistence plugin to sync the store against a GraphQL API.",
"main": "dist/vuex-orm-graphql.umd.js",
"module": "dist/vuex-orm-graphql.es5.js",
Expand Down Expand Up @@ -52,8 +52,7 @@
"url": "https://github.com/vuex-orm/plugin-graphql/issues"
},
"dependencies": {
"apollo-cache-inmemory": "^1.1.7",
"app": "latest"
"apollo-cache-inmemory": "^1.1.7"
},
"peerDependencies": {
"@vuex-orm/core": "^0.31.12"
Expand Down
32 changes: 27 additions & 5 deletions src/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import RootState from "@vuex-orm/core/lib/modules/contracts/RootState";
import Transformer from "../graphql/transformer";
import Schema from "../graphql/schema";
import { singularize } from "../support/utils";
import { GraphQLType } from "../support/interfaces";

/**
* Base class for all Vuex actions. Contains some utility and convenience methods.
Expand Down Expand Up @@ -46,19 +47,19 @@ export default class Action {
newData.id = parseInt(newData.id, 10);

const insertedData: Data = await Store.insertData(
{ [model.pluralName]: newData } as Data,
{ [model.singularName]: newData } as Data,
dispatch
);

// Try to find the record to return
const records = insertedData[model.pluralName];
const records = insertedData[model.singularName];
const newRecord = records[records.length - 1];
if (newRecord) {
return newRecord;
} else {
Context.getInstance().logger.log(
"Couldn't find the record of type '",
model.pluralName,
model.singularName,
"' within",
insertedData,
". Falling back to find()"
Expand Down Expand Up @@ -104,7 +105,12 @@ export default class Action {
* @returns {Arguments}
*/
static addRecordToArgs(args: Arguments, model: Model, data: Data): Arguments {
args[model.singularName] = Transformer.transformOutgoingData(model, data, false);
args[model.singularName] = Transformer.transformOutgoingData(
model,
data,
false,
this.getInputType(model)
);
return args;
}

Expand All @@ -121,7 +127,12 @@ export default class Action {

if (value instanceof context.components.Model) {
const model = context.getModel(singularize(value.$self().entity));
const transformedValue = Transformer.transformOutgoingData(model, value, false);
const transformedValue = Transformer.transformOutgoingData(
model,
value,
false,
this.getInputType(model)
);
context.logger.log(
"A",
key,
Expand All @@ -136,4 +147,15 @@ export default class Action {

return args;
}

/**
* Gets the mutation input type for the given model
* @param {Model} model
* @returns {GraphQLType | null}
*/
protected static getInputType(model: Model): GraphQLType | null {
const context: Context = Context.getInstance();
const inputTypeName: string = context.adapter.getInputTypeName(model);
return context.schema!.getType(inputTypeName);
}
}
1 change: 1 addition & 0 deletions src/actions/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class Fetch extends Action {
model,
params.filter as Data,
true,
undefined,
Object.keys(params.filter)
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/actions/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export default class Persist extends Action {
if (id) {
const model = this.getModelFromState(state!);
const mutationName = Context.getInstance().adapter.getNameForPersist(model);
const oldRecord = model.getRecordWithId(id)!;

const oldRecord = model.baseModel
.query()
.withAllRecursive()
.where("$id", id)
.first()!;

const mockReturnValue = model.$mockHook("persist", {
id,
Expand Down
21 changes: 19 additions & 2 deletions src/graphql/transformer.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
singularize
} from "../support/utils";
import { ConnectionMode } from "../adapters/adapter";
import { GraphQLType, GraphQLField } from "../support/interfaces";

/**
* This class provides methods to transform incoming data from GraphQL in to a format Vuex-ORM understands and
Expand All @@ -32,13 +33,15 @@ export default class Transformer {
model: Model,
data: Data,
read: boolean,
inputType?: GraphQLType | null,
whitelist?: Array<String>,
outgoingRecords?: Map<string, Array<string>>,
recursiveCall?: boolean
): Data {
const context = Context.getInstance();
const relations: Map<string, Relation> = model.getRelations();
const returnValue: Data = {} as Data;
const inputFields = inputType ? inputType.inputFields : undefined;
if (outgoingRecords === undefined) outgoingRecords = new Map<string, Array<string>>();
if (recursiveCall === undefined) recursiveCall = false;

Expand All @@ -64,7 +67,8 @@ export default class Transformer {
key,
value,
model,
whitelist
whitelist,
inputFields
)
) {
let relatedModel = Model.getRelatedModel(relations.get(key)!);
Expand All @@ -81,6 +85,7 @@ export default class Transformer {
v,
read,
undefined,
undefined,
outgoingRecords,
true
);
Expand All @@ -102,6 +107,7 @@ export default class Transformer {
value,
read,
undefined,
undefined,
outgoingRecords,
true
);
Expand Down Expand Up @@ -176,6 +182,12 @@ export default class Transformer {
result[key] = parseFloat(data[key]);
} else if (key.endsWith("Type") && model.isTypeFieldOfPolymorphicRelation(key)) {
result[key] = pluralize(downcaseFirstLetter(data[key]));
} else if (Array.isArray(data[key]) && recursiveCall) {
const relation: Relation | undefined = model.getRelations().get(key);
if (relation) {
const related: Model | null = Model.getRelatedModel(relation)!;
result[key] = this.transformIncomingData(data[key], related, mutation, true);
}
} else {
result[key] = data[key];
}
Expand All @@ -201,14 +213,16 @@ export default class Transformer {
* @param {any} value Value of the field.
* @param {Model} model Model class which contains the field.
* @param {Array<String>|undefined} whitelist Contains a list of fields which should always be included.
* @param {Array<GraphQLField>|undefined} schemaFields Contains a list of schema fields which are defined in the model input type for the mutation.
* @returns {boolean}
*/
public static shouldIncludeOutgoingField(
forFilter: boolean,
fieldName: string,
value: any,
model: Model,
whitelist?: Array<String>
whitelist?: Array<String>,
schemaFields?: Array<GraphQLField>
): boolean {
// Always add fields on the whitelist.
if (whitelist && whitelist.includes(fieldName)) return true;
Expand All @@ -219,6 +233,9 @@ export default class Transformer {
// Ignore empty fields
if (value === null || value === undefined) return false;

// Is the field in the mutation type field list, if provided?
if (schemaFields && !schemaFields.find(f => f.name === fieldName)) return false;

// Include all eager save connections
if (model.getRelations().has(fieldName)) {
// We never add relations to filters.
Expand Down
5 changes: 1 addition & 4 deletions src/orm/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ export default class Model {
if (!field) return false;

const context = Context.getInstance();
return (
field instanceof context.components.Number || field instanceof context.components.Increment
);
return field instanceof context.components.Number;
}

/**
Expand All @@ -81,7 +79,6 @@ export default class Model {
const context = Context.getInstance();

return (
field instanceof context.components.Increment ||
field instanceof context.components.Attr ||
field instanceof context.components.String ||
field instanceof context.components.Number ||
Expand Down