Skip to content

Commit

Permalink
feat: query to list all prompts (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelchris authored Dec 31, 2024
1 parent c6396aa commit 2865e9a
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
95 changes: 95 additions & 0 deletions __tests__/prompts.ts
Original file line number Diff line number Diff line change
@@ -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' },
},
]);
});
});
7 changes: 7 additions & 0 deletions src/graphorm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,13 @@ const obj = new GraphORM({
},
},
},
Prompt: {
fields: {
flags: {
jsonType: true,
},
},
},
});

export default obj;
3 changes: 3 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -63,6 +64,7 @@ export const schema = urlDirective.transformer(
leaderboard.typeDefs,
integrations.typeDefs,
contentPreference.typeDefs,
prompts.typeDefs,
],
resolvers: merge(
common.resolvers,
Expand All @@ -87,6 +89,7 @@ export const schema = urlDirective.transformer(
leaderboard.resolvers,
integrations.resolvers,
contentPreference.resolvers,
prompts.resolvers,
),
}),
),
Expand Down
62 changes: 62 additions & 0 deletions src/schema/prompts.ts
Original file line number Diff line number Diff line change
@@ -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<unknown, BaseContext> = traceResolvers<
unknown,
BaseContext
>({
Query: {
prompts: (_, __, ctx: AuthContext, info): Promise<GQLPrompt[]> =>
graphorm.query<GQLPrompt>(ctx, info, (builder) => {
builder.queryBuilder = builder.queryBuilder.orderBy(
`"${builder.alias}".order`,
);

return builder;
}),
},
});

0 comments on commit 2865e9a

Please sign in to comment.