From fd565949c17a160a1297838a7388fc6a7c9a954f Mon Sep 17 00:00:00 2001 From: Peter Sieg Date: Sun, 15 Sep 2019 17:45:21 -0400 Subject: [PATCH] feat: expose propositions field which lists all propositions --- src/graphql/resolvers/index.test.ts | 1 + src/graphql/resolvers/index.ts | 2 + .../resolvers/listPropositions.test.ts | 40 +++++++++++++++++++ src/graphql/resolvers/listPropositions.ts | 8 ++++ src/graphql/typeDefs/Root.graphql | 1 + .../typeDefs/__snapshots__/index.test.ts.snap | 1 + 6 files changed, 53 insertions(+) create mode 100644 src/graphql/resolvers/listPropositions.test.ts create mode 100644 src/graphql/resolvers/listPropositions.ts diff --git a/src/graphql/resolvers/index.test.ts b/src/graphql/resolvers/index.test.ts index aec5738..19ac2c1 100644 --- a/src/graphql/resolvers/index.test.ts +++ b/src/graphql/resolvers/index.test.ts @@ -15,6 +15,7 @@ describe('resolvers', () => { }, "Query": Object { "proposition": [Function], + "propositions": [Function], }, } `); diff --git a/src/graphql/resolvers/index.ts b/src/graphql/resolvers/index.ts index 381cfde..720a896 100644 --- a/src/graphql/resolvers/index.ts +++ b/src/graphql/resolvers/index.ts @@ -1,5 +1,6 @@ import createProposition from './createProposition'; import proposition from './getProposition'; +import propositions from './listPropositions'; import Proposition from './Proposition'; import setSupportedBy from './setSupportedBy'; import setSupports from './setSupports'; @@ -13,5 +14,6 @@ export default { Proposition, Query: { proposition, + propositions, }, }; diff --git a/src/graphql/resolvers/listPropositions.test.ts b/src/graphql/resolvers/listPropositions.test.ts new file mode 100644 index 0000000..63403b6 --- /dev/null +++ b/src/graphql/resolvers/listPropositions.test.ts @@ -0,0 +1,40 @@ +import { + IProposition, + listPropositions as _listPropositions, +} from '../../lib/proposition'; +import listPropositions from './listPropositions'; + +jest.mock('../../lib/proposition', () => ({ + listPropositions: jest.fn(), +})); + +const propositions = [ + { + id: 0, + supportedBy: [1, 2], + supports: [], + text: 'Greeks are mortal', + }, + { + id: 1, + supportedBy: [], + supports: [0], + text: 'All men are mortal', + }, + { + id: 2, + supportedBy: [], + supports: [0], + text: 'Greeks are men', + }, +]; + +(_listPropositions as jest.Mock).mockImplementation( + async (): Promise => propositions +); + +describe('Propositions', () => { + it('returns an array of propositions', async () => { + await expect(listPropositions()).resolves.toEqual(propositions); + }); +}); diff --git a/src/graphql/resolvers/listPropositions.ts b/src/graphql/resolvers/listPropositions.ts new file mode 100644 index 0000000..5ccc27d --- /dev/null +++ b/src/graphql/resolvers/listPropositions.ts @@ -0,0 +1,8 @@ +import { IProposition, listPropositions } from '../../lib/proposition'; + +/** + * GraphQL resolver for the propositions field. + */ +export default async function propositions(): Promise { + return listPropositions(); +} diff --git a/src/graphql/typeDefs/Root.graphql b/src/graphql/typeDefs/Root.graphql index 35df845..ab5edfc 100644 --- a/src/graphql/typeDefs/Root.graphql +++ b/src/graphql/typeDefs/Root.graphql @@ -8,6 +8,7 @@ type Query { Get a proposition by its identifier. """ proposition(id: Int!): Proposition + propositions: [Proposition] } type Mutation { diff --git a/src/graphql/typeDefs/__snapshots__/index.test.ts.snap b/src/graphql/typeDefs/__snapshots__/index.test.ts.snap index 1cd14a2..730c2bd 100644 --- a/src/graphql/typeDefs/__snapshots__/index.test.ts.snap +++ b/src/graphql/typeDefs/__snapshots__/index.test.ts.snap @@ -5,6 +5,7 @@ exports[`typeDefs exports the GraphQL schema 1`] = ` type Query { \\"\\"\\"Get a proposition by its identifier.\\"\\"\\" proposition(id: Int!): Proposition + propositions: [Proposition] } type Mutation {