Skip to content
Merged
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
26 changes: 14 additions & 12 deletions addon/mock/create.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import mockMutationFn from './mutation';
import mockQueryFn from './query';
import { contextSet } from '../utils';
import { contextSet, reduceKeys } from '../utils';

const addRootMock = (mocks, rootType, mockFn, db, options) =>
contextSet(mocks, rootType.name, () =>
mockRootType(rootType._fields, mockFn, db, options));

const mockRootType = (fields = {}, mockFn, db, options) =>
reduceKeys(fields, (mocks, field) =>
contextSet(mocks, field, mockFn(db, options)), {});

export function createMocksForSchema(schema, db, options) {
let { _mutationType, _queryType } = schema;
let mocks = {};

return {
RootMutationType: () =>
mockRootType(_mutationType._fields, mockMutationFn, db, options),
RootQueryType: () =>
mockRootType(_queryType._fields, mockQueryFn, db, options)
};
}
addRootMock(mocks, schema._queryType, mockQueryFn, db, options);
addRootMock(mocks, schema._mutationType, mockMutationFn, db, options);

const mockRootType = (rootTypes = {}, mockFn, db, options) =>
Object.keys(rootTypes).reduce((mocks, rootType) =>
contextSet(mocks, rootType, mockFn(db, options)), {});
return mocks;
}
3 changes: 3 additions & 0 deletions addon/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ export const getTypeFromMeta = (meta, isList) =>
get(meta, isList ? PROP_FOR_LIST_TYPE : PROP_FOR_TYPE);

export const isFunction = (obj) => obj != null && typeof obj === 'function';

export const reduceKeys = (obj, reducerFn, defaultValue) =>
Object.keys(obj).reduce(reducerFn, defaultValue);
15 changes: 5 additions & 10 deletions tests/dummy/app/gql/schema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export default `
schema {
mutation: RootMutationType
query: RootQueryType
}

type Address {
id: String!
line1: String!
Expand All @@ -13,6 +8,10 @@ type Address {
zip: String!
}

type Mutation {
updatePerson(id: ID!, personAttributes: PersonAttributes!): Person
}

type Pet {
id: String!
age: Int!
Expand All @@ -36,11 +35,7 @@ input PersonAttributes {
age: Int
}

type RootMutationType {
updatePerson(id: ID!, personAttributes: PersonAttributes!): Person
}

type RootQueryType {
type Query {
person(id: ID!): Person

people(lastName: String, pageSize: Int): [Person]
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { module, test } from 'qunit';
import { reduceKeys } from 'ember-cli-mirage-graphql/utils';

module('Unit | utils', function() {
test('it can reduce object keys by a function', function(assert) {
let obj = { bar: true, baz: true };
let result1 = reduceKeys(obj, (a, b) => `${a}.${b}`, 'foo');
let result2 = reduceKeys(obj, (a, b) => a ? `${a}.${b}` : b);

assert.equal(result1, 'foo.bar.baz', 'It works with default value');
assert.equal(result2, 'bar.baz', 'It works without default value');
});
});