Skip to content

Commit

Permalink
Merge pull request #115 from vantreeseba/add_create_many
Browse files Browse the repository at this point in the history
Add create many
  • Loading branch information
fzaninotto authored Apr 20, 2021
2 parents 4b047ff + 0bd627f commit 715cb16
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type Query {
}
type Mutation {
createPost(data: String): Post
createManyPost(data: [{data:String}]): [Post]
updatePost(data: String): Post
removePost(id: ID!): Post
}
Expand Down
25 changes: 25 additions & 0 deletions src/introspection/getSchemaFromData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLSchema,
GraphQLString,
parse,
Expand Down Expand Up @@ -140,10 +141,34 @@ export default (data) => {
{}
);
const { id, ...createFields } = typeFields;

// Build input type.
const inputFields = Object.keys(typeFields).reduce(
(f, fieldName) => {
f[fieldName] = Object.assign({}, typeFields[fieldName]);
delete f[fieldName].resolve;
return f;
},
{}
);

const createManyInputType = new GraphQLInputObjectType({
name: type.name + 'Input',
fields: inputFields,
});

fields[`create${type.name}`] = {
type: typesByName[type.name],
args: createFields,
};
fields[`createMany${type.name}`] = {
type: new GraphQLList(typesByName[type.name]),
args: {
data: {
type: new GraphQLList(createManyInputType),
},
},
};
fields[`update${type.name}`] = {
type: typesByName[type.name],
args: nullableTypeFields,
Expand Down
5 changes: 5 additions & 0 deletions src/resolver/Mutation/createMany.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import create from './create';

export default (entityData = []) => (_, entities) => {
return entities.data.map((e) => create(entityData)(null, e));
};
40 changes: 40 additions & 0 deletions src/resolver/Mutation/createMany.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import createMany from './createMany';

test('returns a new object with id 0 on empty datastore', () => {
expect(createMany()(null, { data: [{}] })).toEqual([{ id: 0 }]);
});

test('returns a new object with incremental id', () => {
const data = [{ id: 1 }, { id: 3 }];
expect(createMany(data)(null, { data: [{}] })).toEqual([{ id: 4 }]);
});

test('returns a new object using create data', () => {
const data = [{ id: 0, value: 'foo' }];
expect(createMany(data)(null, { data: [{ value: 'toto' }] })).toEqual([
{
id: 1,
value: 'toto',
},
]);
});

test('creates a new record', () => {
const data = [{ id: 1 }, { id: 3 }];
createMany(data)(null, { data: [{ value: 'foo' }] });
expect(data).toEqual([{ id: 1 }, { id: 3 }, { id: 4, value: 'foo' }]);
});

test('creates multiple new records', () => {
const data = [{ id: 1 }, { id: 3 }];
createMany(data)(null, {
data: [{ value: 'foo' }, { value: 'bar' }, { value: 'baz' }],
});
expect(data).toEqual([
{ id: 1 },
{ id: 3 },
{ id: 4, value: 'foo' },
{ id: 5, value: 'bar' },
{ id: 6, value: 'baz' },
]);
});
2 changes: 2 additions & 0 deletions src/resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import all from './Query/all';
import meta from './Query/meta';
import single from './Query/single';
import create from './Mutation/create';
import createMany from './Mutation/createMany';
import update from './Mutation/update';
import remove from './Mutation/remove';
import entityResolver from './Entity';
Expand All @@ -20,6 +21,7 @@ const getQueryResolvers = (entityName, data) => ({

const getMutationResolvers = (entityName, data) => ({
[`create${entityName}`]: create(data),
[`createMany${entityName}`]: createMany(data),
[`update${entityName}`]: update(data),
[`remove${entityName}`]: remove(data),
});
Expand Down

0 comments on commit 715cb16

Please sign in to comment.