zapier / apollo-server-integration-testing

Test helper for writing apollo-server integration tests
MIT License
133 stars 21 forks source link

Add better defaults for `TestQuery`'s return value type #9

Closed zhouzi closed 3 years ago

zhouzi commented 4 years ago

Currently, calling query or mutation is a bit tedious as it requires to define precisely their return value. For example:

// Typing the resulting data
const { data } = await query<{ data: User[] }>(USERS);

// Typing errors
const { errors } = await query<{ errors: ApolloError[] }>(USERS);

Adding the data property and having to mock errors is redundant. It can be simplified by using graphql's ExecutionResult. The above example could be turned into:

// Typing the resulting data
const { data } = await query<ExecutionResult<User[]>>(USERS);

// Typing errors
const { errors } = await query<ExecutionResult>(USERS);

I think it would be ideal if TestQuery could use ExecutionResult so we'd end up with:

// Typing the resulting data
const { data } = await query<User[]>(USERS);

// Typing errors
const { errors } = await query(USERS);

I would be happy to submit a PR if that's something you would consider. I might miss some use cases so let me know what you think.

vitorbal commented 4 years ago

Hey @Zhouzi thanks for reaching out! I'm not too experienced with typescript generics, but what you describe sounds like a better DX for sure. If you're up for spiking it out on a PR, that would be really cool! I think that's the best way for us to check if we're missing any edge cases :)