Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack config for graphql-let breaks HMR #636

Open
alex-vladut opened this issue Oct 23, 2022 · 2 comments
Open

Webpack config for graphql-let breaks HMR #636

alex-vladut opened this issue Oct 23, 2022 · 2 comments

Comments

@alex-vladut
Copy link

I configured graphql-let for a small project to generate the document types for the .graphql files. As I added the Webpack config to use graphql-let loader it breaks the hot module reload feature and every change I make to the code restarts the server making the development process suboptimal.

For reference, here is the project when I configured it https://github.com/alex-vladut/aws-amplify-cdk, and the Webpack config could be found here https://github.com/alex-vladut/aws-amplify-cdk/blob/main/apps/todo/webpack.config.js.

Another issue I encountered is that whenever I make a change to any graphql file the type file is correctly generated, but for some reason, the typescript compiler is not updated and still shows an error. If I restart the app, it picks up the types as expected and works fine. As an example, let's say I have the following file getUser.graphql

query User($id: ID!) {
  getUser(id: $id) {
    id
   # name
  }
}

It is referenced in a React component as follows:

import { useQuery } from '@apollo/client';
import { UserDocument } from './getUser.graphql';

export function UserFromLib() {
  const { data, loading, error } = useQuery(UserDocument);
  return <div>{data?.getUser?.name}</div>;
}

Notice that here I reference the name field which was not defined in the query. While the app is running, even if the field is added to be returned from the Graphql query, it still shows a typescript compilation error that name is not defined. graphql-let correctly updated the type file though.

I would be curious if anyone encountered such issues in their projects and found a solution, or could point me to possible things I can do to further investigate. Thanks for your help.

@comp615
Copy link
Contributor

comp615 commented Oct 25, 2022

This could be tangential to your original ask and may not solve it, but you might look at using the hook generated by graphql-let directly as the typing on that might be better. In your example above, it'd be something like:

import { useGetUserQuery } from './getUser.graphql';

export function UserFromLib() {
  const { data, loading, error } = useGetUserQuery();
  return <div>{data?.getUser?.name}</div>;
}

@alex-vladut
Copy link
Author

Thank you for your suggestion 👍. I gave it a try and replaced the typed-document-node with typescript-react-apollo in the config, but for some reason the HMR is still broken for me. I will keep digging, not sure what could be the issue here. I would prefer to have the framework-agnostic TypedDocumentNodes, but I agree with you if this is not as reliable as the apollo plugin I could used apollo too.

It would be great if the Apollo document nodes were typed, but it seems it generates a simple untyped DocumentNode:

...
export declare const UserDocument: Apollo.DocumentNode;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants