Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove casual dependency #39

Merged
merged 1 commit into from Apr 21, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -32,7 +32,6 @@
"homepage": "https://github.com/apollostack/apollo-proxy#readme",
"dependencies": {
"babel-polyfill": "^6.5.0",
"casual-browserify": "^1.5.2",
"express-graphql": "^0.5.1",
"fs": "0.0.2",
"graphql": "^0.5.0",
Expand Down
19 changes: 12 additions & 7 deletions src/mock.js
Expand Up @@ -7,7 +7,6 @@ import {
getNullableType,
getNamedType,
} from 'graphql/type';
import casual from 'casual-browserify';
import { graphql } from 'graphql';
import uuid from 'node-uuid';
import { forEachField, buildSchemaFromTypeDefinitions } from './schemaGenerator';
Expand Down Expand Up @@ -52,16 +51,22 @@ function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = fals
});

const defaultMockMap = new Map();
defaultMockMap.set('Int', () => casual.integer());
defaultMockMap.set('Float', () => casual.double());
defaultMockMap.set('String', () => casual.words(2));
defaultMockMap.set('Boolean', () => casual.coin_flip);
defaultMockMap.set('Int', () => Math.round(Math.random * 200) - 100);
defaultMockMap.set('Float', () => Math.random * 200 - 100);
defaultMockMap.set('String', () => 'Hello World');
defaultMockMap.set('Boolean', () => Math.random > 0.5);
defaultMockMap.set('ID', () => uuid.v4());

function mergeObjects(a, b) {
return Object.assign(a, b);
}

// returns a random element from that ary
function getRandomElement(ary) {
const sample = Math.floor(Math.random() * ary.length);
return ary[sample];
}

// takes either an object or a (possibly nested) array
// and completes the customMock object with any fields
// defined on genericMock
Expand Down Expand Up @@ -129,10 +134,10 @@ function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = fals
if (fieldType instanceof GraphQLUnionType) {
// TODO: if a union type doesn't implement resolveType, we could overwrite
// it with a default that works with what's below.
return { typename: casual.random_element(fieldType.getTypes()) };
return { typename: getRandomElement(fieldType.getTypes()) };
}
if (fieldType instanceof GraphQLEnumType) {
return casual.random_element(fieldType.getValues()).value;
return getRandomElement(fieldType.getValues()).value;
}
if (defaultMockMap.has(fieldType.name)) {
return defaultMockMap.get(fieldType.name)(o, a, c, r);
Expand Down