Skip to content

Commit 0a2fe0b

Browse files
committed
feat: Add create many and tests.
1 parent 4b047ff commit 0a2fe0b

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

src/introspection/getSchemaFromData.js

Lines changed: 20 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,29 @@ export default (data) => {
140141
{}
141142
);
142143
const { id, ...createFields } = typeFields;
144+
145+
// Build input type.
146+
var inputFields = { ...typeFields };
147+
Object.keys(inputFields).forEach((key) => {
148+
delete inputFields[key].resolve;
149+
});
150+
var inputType = new GraphQLInputObjectType({
151+
name: type.name + 'Input',
152+
fields: inputFields,
153+
});
154+
143155
fields[`create${type.name}`] = {
144156
type: typesByName[type.name],
145157
args: createFields,
146158
};
159+
fields[`createMany${type.name}`] = {
160+
type: new GraphQLList(typesByName[type.name]),
161+
args: {
162+
data: {
163+
type: new GraphQLList(inputType),
164+
},
165+
},
166+
};
147167
fields[`update${type.name}`] = {
148168
type: typesByName[type.name],
149169
args: nullableTypeFields,
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
});

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)