diff --git a/__tests__/prompts.ts b/__tests__/prompts.ts new file mode 100644 index 000000000..bf958ad80 --- /dev/null +++ b/__tests__/prompts.ts @@ -0,0 +1,95 @@ +import { DataSource } from 'typeorm'; +import createOrGetConnection from '../src/db'; +import { + disposeGraphQLTesting, + GraphQLTestClient, + GraphQLTestingState, + initializeGraphQLTesting, + MockContext, +} from './helpers'; +import { Prompt } from '../src/entity/Prompt'; + +let con: DataSource; +let state: GraphQLTestingState; +let client: GraphQLTestClient; +let loggedUser: string = null; + +beforeAll(async () => { + con = await createOrGetConnection(); + state = await initializeGraphQLTesting( + () => new MockContext(con, loggedUser), + ); + client = state.client; +}); + +afterAll(() => disposeGraphQLTesting(state)); + +beforeEach(async () => { + loggedUser = null; + + await con.getRepository(Prompt).save([ + { + id: 'prompt1', + order: 1, + label: 'label1', + description: 'description1', + prompt: 'prompt1', + flags: { icon: 'icon1', color: 'color1' }, + }, + { + id: 'prompt2', + order: 2, + label: 'label2', + description: 'description2', + prompt: 'prompt2', + flags: { icon: 'icon2', color: 'color2' }, + }, + { + id: 'prompt3', + order: 3, + label: 'label3', + description: 'description3', + prompt: 'prompt3', + flags: { icon: 'icon3', color: 'color3' }, + }, + ]); +}); + +describe('query prompts', () => { + const QUERY = `{ + prompts { + id + label + description + flags { + icon + color + } + } + }`; + + it('should return all prompts in order', async () => { + const res = await client.query(QUERY); + + expect(res.data.prompts).toEqual([ + { + id: 'prompt1', + label: 'label1', + description: 'description1', + flags: { icon: 'icon1', color: 'color1' }, + }, + { + id: 'prompt2', + label: 'label2', + description: 'description2', + flags: { icon: 'icon2', color: 'color2' }, + }, + { + id: 'prompt3', + label: 'label3', + description: 'description3', + flags: { icon: 'icon3', color: 'color3' }, + }, + ]); + }); +}); diff --git a/src/graphorm/index.ts b/src/graphorm/index.ts index 13644331b..cf8f001f9 100644 --- a/src/graphorm/index.ts +++ b/src/graphorm/index.ts @@ -940,6 +940,13 @@ const obj = new GraphORM({ }, }, }, + Prompt: { + fields: { + flags: { + jsonType: true, + }, + }, + }, }); export default obj; diff --git a/src/graphql.ts b/src/graphql.ts index 825975b37..3cf4e2b90 100644 --- a/src/graphql.ts +++ b/src/graphql.ts @@ -25,6 +25,7 @@ import * as urlDirective from './directive/url'; import * as leaderboard from './schema/leaderboard'; import * as integrations from './schema/integrations'; import * as contentPreference from './schema/contentPreference'; +import * as prompts from './schema/prompts'; import { makeExecutableSchema } from '@graphql-tools/schema'; import { rateLimitTypeDefs, @@ -63,6 +64,7 @@ export const schema = urlDirective.transformer( leaderboard.typeDefs, integrations.typeDefs, contentPreference.typeDefs, + prompts.typeDefs, ], resolvers: merge( common.resolvers, @@ -87,6 +89,7 @@ export const schema = urlDirective.transformer( leaderboard.resolvers, integrations.resolvers, contentPreference.resolvers, + prompts.resolvers, ), }), ), diff --git a/src/schema/prompts.ts b/src/schema/prompts.ts new file mode 100644 index 000000000..6145255d9 --- /dev/null +++ b/src/schema/prompts.ts @@ -0,0 +1,62 @@ +import { IResolvers } from '@graphql-tools/utils'; +import { traceResolvers } from './trace'; +import graphorm from '../graphorm'; +import { AuthContext, BaseContext } from '../Context'; +import { Prompt } from '../entity/Prompt'; + +type GQLPrompt = Prompt; + +export const typeDefs = /* GraphQL */ ` + """ + Flags for the prompt + """ + type PromptFlagsPublic { + icon: String + color: String + } + + """ + Prompt object + """ + type Prompt { + """ + The ID representing this prompt + """ + id: String! + + order: Int! + + label: String! + + description: String + + createdAt: DateTime! + + updatedAt: DateTime! + + flags: PromptFlagsPublic + } + + extend type Query { + """ + Get all available prompts + """ + prompts: [Prompt]! + } +`; + +export const resolvers: IResolvers = traceResolvers< + unknown, + BaseContext +>({ + Query: { + prompts: (_, __, ctx: AuthContext, info): Promise => + graphorm.query(ctx, info, (builder) => { + builder.queryBuilder = builder.queryBuilder.orderBy( + `"${builder.alias}".order`, + ); + + return builder; + }), + }, +});