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

Format embedded GQL in template literal statements #16064

Merged
merged 1 commit into from
Mar 11, 2024

Conversation

keithlayne
Copy link
Contributor

@keithlayne keithlayne commented Feb 8, 2024

Description

Fixes #16007

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@keithlayne keithlayne marked this pull request as draft February 8, 2024 19:36
@keithlayne keithlayne marked this pull request as ready for review February 8, 2024 19:46
@keithlayne
Copy link
Contributor Author

My description of this feature is probably bad, feel free to fix :)

@sosukesuzuki
Copy link
Member

Can you share a real world use case?

@keithlayne
Copy link
Contributor Author

Can you share a real world use case?

I did in #16007.

@keithlayne
Copy link
Contributor Author

I realized that probably needs clarification (my apologies), so here is a real-life example:

// card.tsx
import type { ContentItem } from './card.generated';

/* GraphQL */ `
  fragment ContentItem on ContentItem {
    id
    name
    thumbnailUri
  }
`;

export interface ContentItemCardProps {
  contentItem: ContentItem;
  children: ReactNode;
}

export const ContentItemCard = (props: ContentItemCardProps) => ...

This kind of construct is probably not useful at all in JS, but with TS + GQL is very useful for us.

Using graphql-codegen, we get named types generated for fragments that we can use (for example) in react component props. The near-operation-file (IIRC) preset is convenient here, it generates a file alongside card.tsx.

The template literals themselves are basically dead code and can be removed from bundle output.

An alternative would be to drop a graphql file alongside card.tsx, which would be fine, but for simple things like this there's some advantage to colocation.

Please lemme know if I need to clarify any more.

@keithlayne
Copy link
Contributor Author

Also maybe worth noting that all my other tooling (codegen, GQL LSP) supports this use case well. It's just a remaining nit that I have to format it by hand right now.

Comment on lines 84 to 85
(parent.type === "ExpressionStatement" &&
node.type === "TemplateLiteral" &&
hasLeadingBlockCommentWithName(parent, languageName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(parent.type === "ExpressionStatement" &&
node.type === "TemplateLiteral" &&
hasLeadingBlockCommentWithName(parent, languageName))
(parent.type === "ExpressionStatement" &&
hasLeadingBlockCommentWithName(parent, languageName))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I think I realized that check wasn't strictly necessary, but I just don't know enough about how things work here, so in my mind I was playing it safe somehow. I'll update this today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, would a better description be: Format embedded GQL in template literal statements?

@keithlayne keithlayne changed the title Format embedded GQL in JS without assignment Format embedded GQL in template literal statements Feb 19, 2024
@keithlayne
Copy link
Contributor Author

Anything else I need to do here?

@sosukesuzuki
Copy link
Member

Sorry for the delay, will merge when CI goes through.

@sosukesuzuki sosukesuzuki merged commit f62bf4a into prettier:main Mar 11, 2024
28 checks passed
@keithlayne keithlayne deleted the issue-16007 branch March 11, 2024 14:35
@kachkaev kachkaev added this to the 3.3 milestone May 31, 2024
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

Successfully merging this pull request may close these issues.

Bare embedded GQL string in e.g. JS is not formatted
4 participants