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 #12

Closed zhouzi closed 3 years ago

zhouzi commented 4 years ago

This PR adds tests (albeit pretty simple) and improves TestQuery's return value type.

Prior to this PR, here's how a query's return value is typed:

const { data, errors } = await query<{ data: { books: Book[] }, errors: ApolloError[] }>(GET_BOOKS);

Here's how it can be done with this PR:

const { data, errors } = await query<{ books: Book[] }>(GET_BOOKS);

It would be a breaking change though, as with those changes, the first example's return value would end up being typed as:

{
  data: {
    data: {
      books: Book[]
    },
    errors: ApolloError[]
  },
  errors: ApolloError[]
}

I will be testing this PR on our codebase to make sure it's working as expected.

Closes #9

zhouzi commented 3 years ago

Sorry for the delay, I had to move onto something else. I needed this recently so I fixed a few things and published it under @zhouzi/apollo-server-integration-testing in order to test it properly on the codebase I am working on. Type checks started failing after upgrading, as expected. But once fixed it works like a charm ✨ We are using graphql-codegen so here's an example of how it changed:

// before: no type check at all
const { data } = await query(GET_USER, { variables: { id: "some_id" } })

// data is properly typechecked, along with the variables
const { data } = await query<Pick<Query, "user">, QueryUserArgs>(GET_USER, { variables: { id: "some_id" } });

As mentioned previously, the type change is "breaking" in the sense that it requires users of the library to update their code if they are using TypeScript.

vitorbal commented 3 years ago

Hi @Zhouzi thanks for the update and for looping back to this one. I'd be happy to get this merged, but I think it should be a major version bump since it will be a breaking change for typescript users. What do you think?

zhouzi commented 3 years ago

Agreed, it should definitely be a major version 👍 I added it to the change log's vNext, let me know if there's anything else I can do to help.

vitorbal commented 3 years ago

Great, thanks again @Zhouzi! I have some pending work stuff to do today but I'll try to cut the major version later in the evening 👍

JeremyKirkham commented 3 years ago

Looking forward to this 👍

vitorbal commented 3 years ago

Just published 3.0.0 with this change! Please let me know if you find any bugs. Thanks again for your contribution, @Zhouzi!