Running a custom script each time Next app is recompiled during development
See original GitHub issueHi,
TLDR:
I want to run a custom script each time Next recompiles during development. I’m developing a Next app locally with now dev
. Is there any place I define a command that should run each time the Next app is recompiled in response to a change in the codebase?
Details on why I want to do this below:
I’m using Next 9 with a GraphQL API served from pages/api/graphql.ts
. I’m using apollo-server-micro
, based on the example shown here:
https://gist.github.com/mfellner/2119db3584023092d70118a8dabd146e
Additionally, I’m defining my schema for the server using a library called nexus
. With nexus
, each time the server starts, nexus
will write a .ts
file to the local filesystem. This file contains types which reflect the graphql schema of the server, enabling type checking in the resolvers of the server.
Here’s a line that configures where nexus
generates this file:
https://github.com/prisma/nexus/blob/develop/examples/apollo-fullstack/src/index.ts#L18
The nexus examples recommend using a library like ts-node-dev
to run a server locally, which will automatically restart in response to any changes in the schema. Example here:
https://github.com/prisma/nexus/blob/develop/examples/apollo-fullstack/package.json#L8
However, when developing through an api endpoint setup in Next, with apollo-server-micro
, the server doesn’t ever “start”. As far as I understand, apollo-server-micro
just exports a handler for use in lambdas.
As a result, there’s no point at which nexus
autogenerates my types file.
I want to run the command to generate these types each time my Next app recompiles, so the type checking on my GraphQL resolvers always matches the latest changes to my GraphQL schema.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top GitHub Comments
Hey @Timer!
Can you confirm whether or not, in general, there’s a way to tell Next to run a custom command each time it recompiles your code?
I actually did post the same question on Spectrum about 2 weeks back: https://spectrum.chat/next-js/general/running-a-custom-script-each-time-next-app-is-recompiled-during-development~23e051e5-d0a1-4d3b-9c9e-c1e354d40d30
I also posted it here, in case this wasn’t already an existing feature of the framework. I thought this issue might be turned into a ‘feature request’ ticket, incase there wasn’t an existing way to provide Next with a custom command to run during re-compilation.
Thank you for your help 😃
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.