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

Add gql() call expression support #13784

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions changelog_unreleased/javascript/13784.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#### Add gql() call expression support (#13784 by @Chnapy)

<!-- prettier-ignore -->
```jsx
// Input
gql(`
{
user( id : 5 ) {
firstName
lastName
}
}
`);

// Prettier stable
gql(`
{
user( id : 5 ) {
firstName
lastName
}
}
`);

// Prettier main
gql(`
{
user(id: 5) {
firstName
lastName
}
}
`);
```
3 changes: 2 additions & 1 deletion src/language-js/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ function clean(ast, newObj, parent) {
);
if (
hasLanguageComment ||
(parent.type === "CallExpression" && parent.callee.name === "graphql") ||
(parent.type === "CallExpression" &&
(parent.callee.name === "gql" || parent.callee.name === "graphql")) ||
// TODO: check parser
// `flow` and `typescript` don't have `leadingComments`
!ast.leadingComments
Expand Down
2 changes: 1 addition & 1 deletion src/language-js/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function isGraphQL(path) {
(parent.tag.name === "gql" || parent.tag.name === "graphql")))) ||
(parent.type === "CallExpression" &&
parent.callee.type === "Identifier" &&
parent.callee.name === "graphql")))
(parent.callee.name === "gql" || parent.callee.name === "graphql"))))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,18 @@ query User {
fragment another on User { name
}\${ fragment }\`

// Using CallExpression

gql(\`
{
user( id : 5 ) {
firstName

lastName
}
}
\`);

=====================================output=====================================
import gql from "graphql-tag";

Expand Down Expand Up @@ -564,6 +576,18 @@ gql\`
\${fragment}
\`;

// Using CallExpression

gql(\`
{
user(id: 5) {
firstName

lastName
}
}
\`);

================================================================================
`;

Expand Down
12 changes: 12 additions & 0 deletions tests/format/js/multiparser-graphql/graphql-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,15 @@ ${fragment}#comment

fragment another on User { name
}${ fragment }`

// Using CallExpression

gql(`
{
user( id : 5 ) {
firstName

lastName
}
}
`);