Skip to content

Commit

Permalink
fix #29. put object assign args in create/update plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwy3y3 committed Dec 23, 2018
1 parent d30ced1 commit b6139e8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/gqlify-firestore/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export class FirestoreDataSource implements DataSource {
}

public async create(payload: any): Promise<any> {
payload = Object.assign({}, payload);
const ref = await this.db.collection(this.path).add(payload);
await ref.update({ id: ref.id });
const doc = await ref.get();
Expand Down
9 changes: 7 additions & 2 deletions packages/gqlify/src/plugins/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,17 @@ export default class CreatePlugin implements Plugin {

return {
[mutationName]: async (root, args, context) => {
// args may not have `hasOwnProperty`.
// https://github.com/Canner/gqlify/issues/29
const data = {...args.data};

// no relationship or other hooks
if (!wrapCreate) {
return dataSource.create(args.data);
return dataSource.create(data);
}

// wrap
const createContext: CreateContext = {data: args.data, response: {}};
const createContext: CreateContext = {data, response: {}};
await wrapCreate(createContext, async ctx => {
ctx.response = await dataSource.create(ctx.data);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/gqlify/src/plugins/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,18 @@ export default class UpdatePlugin implements Plugin {
return {
[mutationName]: async (root, args, context) => {
const whereUnique = this.whereInputPlugin.parseUniqueWhere(args.where);
// args may not have `hasOwnProperty`.
// https://github.com/Canner/gqlify/issues/29
const data = {...args.data};

// no relationship or other hooks
if (!wrapUpdate) {
await dataSource.update(whereUnique, args.data);
await dataSource.update(whereUnique, data);
return args.where;
}

// wrap
const updateContext: UpdateContext = {where: args.where, data: args.data, response: {}};
const updateContext: UpdateContext = {where: args.where, data, response: {}};
await wrapUpdate(updateContext, async ctx => {
await dataSource.update(whereUnique, ctx.data);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/gqlify/test/fixtures/oneModel.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ type User @GQLifyModel(dataSource: "memory", key: "users") {
status: STATUS
attributes: JSON
location: Location
note: [Note!]!
note: [Note!]
}
59 changes: 59 additions & 0 deletions packages/gqlify/test/testsuites/oneModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ export function testSuits() {
expect(user).to.deep.equal(createUser);
});

it('should create record with args', async () => {
const userData = {
username: 'wwwy3y3',
email: 'wwwy3y3@canner.io',
};
const query = `
mutation {
createUser (data: {username: "${userData.username}", email: "${userData.email}"}) {${fields}}
}
`;
const {createUser} = await (this as any).graphqlRequest(query);
expect(createUser).to.have.property('id');
expect(createUser).to.deep.include(userData);

// query to see if exists
const {users: createdUsers} = await (this as any).graphqlRequest(`
query {
users {${fields}}
}
`);
expect(createdUsers[0]).to.deep.equal(createUser);

// query one
const {user} = await (this as any).graphqlRequest(`
query {
user(where: {id: "${createUser.id}"}) {${fields}}
}
`);
expect(user).to.deep.equal(createUser);
});

it('should create many records to test where & pagination filter', async () => {
// create 20 users, make 10 of them having status NOT_OK
const createdUsers = await Promise.all(
Expand Down Expand Up @@ -184,6 +215,34 @@ export function testSuits() {
expect(certainUser.username).to.deep.equal(updateUserVariables.data.username);
});

it('should update with args', async () => {
const createUserVariables = {
data: fakeUserData(),
};
const createUserQuery = `
mutation ($data: UserCreateInput!) {
createUser (data: $data) {${fields}}
}
`;
const {createUser} = await (this as any).graphqlRequest(createUserQuery, createUserVariables);

const newUsername = faker.internet.userName();
const updateUserQuery = `
mutation {
updateUser (where: {id: "${createUser.id}"}, data: {username: "${newUsername}"}) { id }
}
`;
const {updateUser} = await (this as any).graphqlRequest(updateUserQuery);
expect(updateUser.id).to.be.equal(createUser.id);

const {user: certainUser} = await (this as any).graphqlRequest(`
query {
user(where: {id: "${createUser.id}"}) {${fields}}
}
`);
expect(certainUser.username).to.deep.equal(newUsername);
});

it('should delete', async () => {
const createUserVariables = {
data: fakeUserData(),
Expand Down

0 comments on commit b6139e8

Please sign in to comment.