Skip to content

Commit 715cb16

Browse files
authored
Merge pull request #115 from vantreeseba/add_create_many
Add create many
2 parents 4b047ff + 0bd627f commit 715cb16

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type Query {
108108
}
109109
type Mutation {
110110
createPost(data: String): Post
111+
createManyPost(data: [{data:String}]): [Post]
111112
updatePost(data: String): Post
112113
removePost(id: ID!): Post
113114
}

src/introspection/getSchemaFromData.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
GraphQLList,
55
GraphQLNonNull,
66
GraphQLObjectType,
7+
GraphQLInputObjectType,
78
GraphQLSchema,
89
GraphQLString,
910
parse,
@@ -140,10 +141,34 @@ export default (data) => {
140141
{}
141142
);
142143
const { id, ...createFields } = typeFields;
144+
145+
// Build input type.
146+
const inputFields = Object.keys(typeFields).reduce(
147+
(f, fieldName) => {
148+
f[fieldName] = Object.assign({}, typeFields[fieldName]);
149+
delete f[fieldName].resolve;
150+
return f;
151+
},
152+
{}
153+
);
154+
155+
const createManyInputType = new GraphQLInputObjectType({
156+
name: type.name + 'Input',
157+
fields: inputFields,
158+
});
159+
143160
fields[`create${type.name}`] = {
144161
type: typesByName[type.name],
145162
args: createFields,
146163
};
164+
fields[`createMany${type.name}`] = {
165+
type: new GraphQLList(typesByName[type.name]),
166+
args: {
167+
data: {
168+
type: new GraphQLList(createManyInputType),
169+
},
170+
},
171+
};
147172
fields[`update${type.name}`] = {
148173
type: typesByName[type.name],
149174
args: nullableTypeFields,

src/resolver/Mutation/createMany.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import create from './create';
2+
3+
export default (entityData = []) => (_, entities) => {
4+
return entities.data.map((e) => create(entityData)(null, e));
5+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import createMany from './createMany';
2+
3+
test('returns a new object with id 0 on empty datastore', () => {
4+
expect(createMany()(null, { data: [{}] })).toEqual([{ id: 0 }]);
5+
});
6+
7+
test('returns a new object with incremental id', () => {
8+
const data = [{ id: 1 }, { id: 3 }];
9+
expect(createMany(data)(null, { data: [{}] })).toEqual([{ id: 4 }]);
10+
});
11+
12+
test('returns a new object using create data', () => {
13+
const data = [{ id: 0, value: 'foo' }];
14+
expect(createMany(data)(null, { data: [{ value: 'toto' }] })).toEqual([
15+
{
16+
id: 1,
17+
value: 'toto',
18+
},
19+
]);
20+
});
21+
22+
test('creates a new record', () => {
23+
const data = [{ id: 1 }, { id: 3 }];
24+
createMany(data)(null, { data: [{ value: 'foo' }] });
25+
expect(data).toEqual([{ id: 1 }, { id: 3 }, { id: 4, value: 'foo' }]);
26+
});
27+
28+
test('creates multiple new records', () => {
29+
const data = [{ id: 1 }, { id: 3 }];
30+
createMany(data)(null, {
31+
data: [{ value: 'foo' }, { value: 'bar' }, { value: 'baz' }],
32+
});
33+
expect(data).toEqual([
34+
{ id: 1 },
35+
{ id: 3 },
36+
{ id: 4, value: 'foo' },
37+
{ id: 5, value: 'bar' },
38+
{ id: 6, value: 'baz' },
39+
]);
40+
});

src/resolver/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import all from './Query/all';
55
import meta from './Query/meta';
66
import single from './Query/single';
77
import create from './Mutation/create';
8+
import createMany from './Mutation/createMany';
89
import update from './Mutation/update';
910
import remove from './Mutation/remove';
1011
import entityResolver from './Entity';
@@ -20,6 +21,7 @@ const getQueryResolvers = (entityName, data) => ({
2021

2122
const getMutationResolvers = (entityName, data) => ({
2223
[`create${entityName}`]: create(data),
24+
[`createMany${entityName}`]: createMany(data),
2325
[`update${entityName}`]: update(data),
2426
[`remove${entityName}`]: remove(data),
2527
});

0 commit comments

Comments
 (0)