Skip to content

Commit

Permalink
feat: create propositions and view supports/supportedBy propositions (#…
Browse files Browse the repository at this point in the history
…24)

* fix: update target version and run yarn

* feat: add the createProposition mutation

* feat: add GREMLIN_PORT to apollo-start

* refactor: supports and supportedBy should return propositions
  • Loading branch information
chasingmaxwell authored Aug 30, 2019
1 parent 21ad6d0 commit 85b8825
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.13
lts/dubnium
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"start": "yarn gremlin-start ; yarn build ; yarn apollo-start",
"gremlin-start": "GREMLIN_PORT=3182 node ./scripts/gremlin.js start",
"gremlin-stop": "node ./scripts/gremlin.js stop",
"apollo-start": "yarn build ; node ./dist/server.js",
"apollo-start": "yarn build ; GREMLIN_PORT=3182 node ./dist/server.js",
"copyTypeDefs": "cp ./src/graphql/typeDefs/*.graphql ./dist/graphql/typeDefs/",
"build": "tsc --declaration ; yarn copyTypeDefs",
"watch": "nodemon --watch src --ext ts --exec \"yarn start\"",
"tslint": "tslint -c tslint.json -p tsconfig.json",
"prettier": "prettier --write \"**/*.{js,ts}\"",
"lint": "yarn prettier ; yarn tslint",
"jest": "GREMLIN_PORT=3183 NODE_ENV=test jest --forceExit --coverage --verbose",
"jest": "GREMLIN_PORT=3183 NODE_ENV=test jest --coverage --verbose",
"test": "yarn jest"
},
"dependencies": {
Expand Down
65 changes: 65 additions & 0 deletions src/graphql/resolvers/Proposition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getProposition } from '../../lib/proposition';
import Proposition from './Proposition';

jest.mock('../../lib/proposition', () => ({
getProposition: jest.fn(),
}));

(getProposition as jest.Mock<any>).mockImplementation(async id => ({
id,
supportedBy: [],
supports: [],
text: 'I am a proposition',
}));

const { supportedBy, supports } = Proposition;

describe('Proposition', () => {
describe('supportedBy', () => {
it('returns a proposition for every id in the supportedBy property', async () => {
expect.assertions(2);
const [prop1, prop2] = supportedBy({
id: 0,
supportedBy: [1, 2],
supports: [],
text: 'Supported proposition',
});
await expect(prop1).resolves.toEqual({
id: 1,
supportedBy: [],
supports: [],
text: 'I am a proposition',
});
await expect(prop2).resolves.toEqual({
id: 2,
supportedBy: [],
supports: [],
text: 'I am a proposition',
});
});
});

describe('supports', () => {
it('returns a proposition for every id in the supports property', async () => {
expect.assertions(2);
const [prop1, prop2] = supports({
id: 0,
supportedBy: [],
supports: [1, 2],
text: 'Supported proposition',
});
await expect(prop1).resolves.toEqual({
id: 1,
supportedBy: [],
supports: [],
text: 'I am a proposition',
});
await expect(prop2).resolves.toEqual({
id: 2,
supportedBy: [],
supports: [],
text: 'I am a proposition',
});
});
});
});
15 changes: 15 additions & 0 deletions src/graphql/resolvers/Proposition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { map } from 'ramda';
import { getProposition, IProposition } from '../../lib/proposition';

function supports(proposition: IProposition) {
return map(getProposition, proposition.supports);
}

function supportedBy(proposition: IProposition) {
return map(getProposition, proposition.supportedBy);
}

export default {
supportedBy,
supports,
};
35 changes: 35 additions & 0 deletions src/graphql/resolvers/createProposition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
createProposition as _createProposition,
INewProposition,
IProposition,
} from '../../lib/proposition';
import createProposition from './createProposition';
jest.mock('../../lib/proposition', () => ({
createProposition: jest.fn(),
}));

(_createProposition as jest.Mock<any>).mockImplementation(
async (newProp: INewProposition): Promise<IProposition> => ({
...newProp,
id: 0,
})
);

describe('createProposition', () => {
it('creates a proposition', async () => {
expect.assertions(2);
await expect(
createProposition({}, { text: 'All men are mortal' })
).resolves.toEqual({
id: 0,
supportedBy: [],
supports: [],
text: 'All men are mortal',
});
expect(_createProposition).toHaveBeenCalledWith({
supportedBy: [],
supports: [],
text: 'All men are mortal',
});
});
});
20 changes: 20 additions & 0 deletions src/graphql/resolvers/createProposition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
createProposition as _createProposition,
INewProposition,
IProposition,
} from '../../lib/proposition';

const DEFAULT_PROPS = {
supportedBy: [],
supports: [],
};

/**
* GraphQL resolver for the createProposition mutation.
*/
export default async function createProposition(
_: any,
args: { text: string; supports?: number[]; supportedBy?: number[] }
): Promise<IProposition> {
return _createProposition({ ...DEFAULT_PROPS, ...args });
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { getProposition, IProposition } from '../../lib/proposition';
import proposition from './proposition';
import {
getProposition as _getProposition,
IProposition,
} from '../../lib/proposition';
import getProposition from './getProposition';
jest.mock('../../lib/proposition', () => ({
getProposition: jest.fn(),
}));

(getProposition as jest.Mock<any>).mockImplementation(
(_getProposition as jest.Mock<any>).mockImplementation(
async (id: number): Promise<IProposition | void> => ({
id,
supportedBy: [],
Expand All @@ -16,12 +19,12 @@ jest.mock('../../lib/proposition', () => ({
describe('proposition', () => {
it('gets a proposition by an id in arguments', async () => {
expect.assertions(2);
await expect(proposition({}, { id: 1 })).resolves.toEqual({
await expect(getProposition({}, { id: 1 })).resolves.toEqual({
id: 1,
supportedBy: [],
supports: [],
text: 'All men are mortal',
});
expect(getProposition).toHaveBeenCalledWith(1);
expect(_getProposition).toHaveBeenCalledWith(1);
});
});
14 changes: 14 additions & 0 deletions src/graphql/resolvers/getProposition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
getProposition as _getProposition,
IProposition,
} from '../../lib/proposition';

/**
* GraphQL resolver for the proposition field.
*/
export default async function getProposition(
_: any,
{ id }: { id: number }
): Promise<IProposition | void> {
return _getProposition(id);
}
7 changes: 7 additions & 0 deletions src/graphql/resolvers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ describe('resolvers', () => {
it('exposes the correct resolvers', () => {
expect(resolvers).toMatchInlineSnapshot(`
Object {
"Mutation": Object {
"createProposition": [Function],
},
"Proposition": Object {
"supportedBy": [Function],
"supports": [Function],
},
"Query": Object {
"proposition": [Function],
},
Expand Down
8 changes: 7 additions & 1 deletion src/graphql/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import proposition from './proposition';
import createProposition from './createProposition';
import proposition from './getProposition';
import Proposition from './Proposition';

export default {
Mutation: {
createProposition,
},
Proposition,
Query: {
proposition,
},
Expand Down
11 changes: 0 additions & 11 deletions src/graphql/resolvers/proposition.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/graphql/typeDefs/Proposition.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type Proposition {
Unique identifier of other propositions which are supported by this
proposition.
"""
supports: [Int]!
supports: [Proposition]!
"""
Unique identifier of other propositions by which this proposition is
supported.
"""
supportedBy: [Int]!
supportedBy: [Proposition]!
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ type Query {
"""
proposition(id: Int!): Proposition
}

type Mutation {
"""
Create a proposition.
"""
createProposition(
text: String!
supports: [Int!]
supportedBy: [Int!]
): Proposition
}
9 changes: 7 additions & 2 deletions src/graphql/typeDefs/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ type Query {
proposition(id: Int!): Proposition
}
type Mutation {
\\"\\"\\"Create a proposition.\\"\\"\\"
createProposition(text: String!, supports: [Int!], supportedBy: [Int!]): Proposition
}
\\"\\"\\"Describes a proposition.\\"\\"\\"
type Proposition {
\\"\\"\\"Unique identifier that represents this proposition.\\"\\"\\"
Expand All @@ -17,12 +22,12 @@ type Proposition {
Unique identifier of other propositions which are supported by this
proposition.
\\"\\"\\"
supports: [Int]!
supports: [Proposition]!
\\"\\"\\"
Unique identifier of other propositions by which this proposition is
supported.
\\"\\"\\"
supportedBy: [Int]!
supportedBy: [Proposition]!
}
"
`;
2 changes: 1 addition & 1 deletion src/graphql/typeDefs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { importSchema } from 'graphql-import';
import { join } from 'path';

const typeDefs = importSchema(join(__dirname, './Query.graphql'));
const typeDefs = importSchema(join(__dirname, './Root.graphql'));

export default typeDefs;
2 changes: 1 addition & 1 deletion src/lib/proposition.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gremlin from 'gremlin';
import { getTraversal } from './util';

interface INewProposition {
export interface INewProposition {
text: string;
supports: number[];
supportedBy: number[];
Expand Down
36 changes: 2 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ debug@^4.0.0, debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3684,7 +3684,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -4952,11 +4952,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -4965,33 +4960,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -5037,11 +5010,6 @@ lodash.isstring@^4.0.1:
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

0 comments on commit 85b8825

Please sign in to comment.