prisma-labs / graphql-framework-experiment

Code-First Type-Safe GraphQL Framework
https://nexusjs.org
MIT License
673 stars 66 forks source link

Test context app GraphQL query type generation #902

Open enzoferey opened 4 years ago

enzoferey commented 4 years ago

Apologies if this is already on your minds in some other issue, I looked a bit around but it's not easy to find things inside 225 issues even with a search feature ๐Ÿ˜…

Perceived Problem

When creating a test context via createTextContext, you can then use context.app.query to execute GraphQL queries against your app. However, the result of these queries is not typed, forcing you to manually write the types or going into no-type-safe land.

const result = await context.app.query(`
    mutation($currentPassword: String!, $newPassword: String!) {
      changePassword(currentPassword: $currentPassword, newPassword: $newPassword) {
        success
      }
    }
  `,
  { currentPassword, newPassword }
);

// result: unkown

Ideas / Proposed Solution(s)

Nexus has all the information it needs via Nexus schema to be able to infer return types of queries. This is something Apollo does based on the SDL schema, but we might be able to do better as we have accurate TypeScript types as well. In most of my projects I use ts-graphql-plugin and its typegen command (source). It works pretty well although it requires you to manually assign the type to the returned value (I would expect from a framework to the mapping for me under the hood).

Weakky commented 4 years ago

related to #1053