graphql-zeus
graphql-zeus copied to clipboard
Query string generator
I was looking at these docs and it shows how to generate a gql string. It works well and I can generate gql strings for queries with no issues. I am using it to add type completion to my jest tests. I was not able to determine how to "effectively" generate gql strings for mutations.
How I would expect it to work
const attemptedQuery = Zeus('mutation', {
facilityUpsertStaff: [
{ facilityId, staff },
{
uid: true,
firstName: true
}
]
}) // was wrapping variables in [Object null] thing
How I got it to work
const query = Zeus('mutation', {
facilityUpsertStaff: [
// @ts-expect-error typing off for staff var
{ facilityId: '$facilityId', staff: '$staff' },
{
uid: true,
firstName: true
}
]
}, { variables: { $params: '$facilityId: String!, $staff: StaffUpsertDto!', values: { facilityId, staff } } })
Test and util
const facilityId = 'some-id'
it('should create facility.staff', async () => {
const staff = mockStaffUpsertDtoFactory()
const query = Zeus('mutation', {
facilityUpsertStaff: [
// @ts-expect-error typing is off on mutations with parameters
{ facilityId: '$facilityId', staff: '$staff' },
{
uid: true,
firstName: true
}
]
}, { variables: { $params: '$facilityId: String!, $staff: StaffUpsertDto!', values: { facilityId, staff } } })
const variables = { facilityId, staff }
const resp = await queryWithAuth<{ facilityUpsertStaff: Staff }>({ app, query, variables })
staffId = resp.facilityUpsertStaff.uid
expect(resp.facilityUpsertStaff).toStrictEqual({
uid: expect.any(String),
firstName: staff.firstName
})
})
const queryWithAuth = async <R>({ app, query, variables }: QueryWithAuth): Promise<request.Response & R> => {
const { text } = await request(app.getHttpServer())
.post('/graphql')
.set('Accept', 'application/json')
.set('Authorization', `Bearer ${token}`)
.send({ query, variables })
.expect('Content-Type', /json/)
.expect(HttpStatus.OK)
const resp = JSON.parse(text)
return resp.errors !== undefined ? resp.errors : resp.data
}
Thanks
https://zeus.graphqleditor.com/page/graphql/variables.html
take a look here. Maybe it is not perfect solution, but we will improve it soon