-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: query to list all prompts (#2569)
- Loading branch information
1 parent
c6396aa
commit 2865e9a
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}), | ||
}, | ||
}); |