Skip to content

Commit

Permalink
Merge pull request #1274 from Quramy/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
Quramy committed Mar 29, 2024
2 parents c814c58 + 04dcb0a commit 8625906
Show file tree
Hide file tree
Showing 30 changed files with 369 additions and 5,811 deletions.
142 changes: 16 additions & 126 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ This plugin has the following features:
- [`enabledGlobalFragments`](#enabledglobalfragments)
- [`localSchemaExtensions`](#localschemaextensions)
- [`typegen.addons`](#typegenaddons)
- [`removeDuplicatedFragments`](#removeduplicatedfragments)
- [Built-in Type Generator Addons](#built-in-type-generator-addons)
- [`typed-query-document`](#typed-query-document)
- [webpack custom transformer](#webpack-custom-transformer)
Expand All @@ -48,7 +47,6 @@ This plugin has the following features:
- [Transformer options](#transformer-options)
- [`removeFragmentDefinitions` optional](#removefragmentdefinitions-optional)
- [`documentTransformers` optional](#documenttransformers-optional)
- [Template strings](#template-strings)
- [Available editors](#available-editors)
- [VSCode](#vscode)
- [Contributing](#contributing)
Expand All @@ -74,8 +72,7 @@ And configure `plugins` section in your tsconfig.json, for example:
"plugins": [
{
"name": "ts-graphql-plugin",
"schema": "path-or-url-to-your-schema.graphql",
"tag": "gql"
"schema": "path-or-url-to-your-schema.graphql"
}
]
}
Expand Down Expand Up @@ -123,7 +120,6 @@ Pass plugin options to your tsconfig.json to configure this plugin.
"name": "ts-graphql-plugin",
/* plugin options */
"schema": "path-or-url-to-your-schema.graphql",
"tag": "gql",
"exclude": ["__generated__"],
...
}
Expand Down Expand Up @@ -257,9 +253,7 @@ type SchemaConfig =
### `tag`
It's optional. When it's set, this plugin works only if the target template string is tagged by a function whose name is equal to this parameter.
If not set, this plugin treats all template strings in your .ts as GraphQL query.
It's optional and the default value is `["gql", "graphql"]`. This value is used to find template literal strings of GraphQL document in your sources.
For example:
Expand All @@ -271,63 +265,31 @@ For example:
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": "gql"
"tag": "myGraphqlTag"
}
]
}
}
```
```ts
/* yourApp.ts */

import { gql } from '@apollo/client';

// Recognized as GraphQL document
const str1 = gql`
const query1 = myGraphqlTag`
query AppQuery {
__typename
}
`;

// Not recognized as GraphQL document
const str2 = `<div></div>`;
const str3 = otherTagFn`foooo`;
```
Sometimes you want to consider the arguments of a particular function calling as a GraphQL document.
For example, [graphql-codegen](https://the-guild.dev/graphql/codegen) and [octkit](https://github.com/octokit/graphql.js) use the `graphql` function as follows:
```ts
import { graphql } from './gql';

const myQuery = graphql(`
query MyQuery {
viewer {
name
}
// Function call expression is also available.
const query2 = myGraphqlTag(`
query AppQuery {
__typename
}
`);
```

Configure as the following:
```js
/* tsconfig.json */

{
"compilerOptions": {
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": {
"name": "graphql",
"ignoreFunctionCallExpression": false
}
}
]
}
}
// The followings are not recognized as GraphQL document:
const str3 = `<div></div>`;
const str4 = otherTagFn`foooo`;
const otherValue = otherFn(`foooo`);
```
The `tag` option accepts the following type:
Expand All @@ -343,6 +305,9 @@ type TagConfig =
};
```
> [!NOTE]
> The `ignoreFunctionCallExpression` key exists only for backward compatibility with old versions. You don't need to explicitly use this.
### `exclude`
It's optional. Specify an array of file or directory names when you want to exclude specific TypeScript sources from the plugin's analysis.
Expand All @@ -354,7 +319,7 @@ It's useful if other code generator copies your GraphQL Template Strings.
### `enabledGlobalFragments`
It's optional and the default value is `false`. If enabled, the plugin automatically searches for and merges the dependent fragments for the target GraphQL operation in the TypeScript project.
It's optional and the default value is `true`. If enabled, the plugin automatically searches for and merges the dependent fragments for the target GraphQL operation in the TypeScript project.
```tsx
/* Post.tsx */
Expand Down Expand Up @@ -488,31 +453,6 @@ If you learn how to create your Addon, see [type generator customization guide](
ts-graphql-plugin also provides built-in Addons. See also the [Built-in Type Generator Addons](#built-in-type-generator-addons) section.
### `removeDuplicatedFragments`
It's optional and default: `true`. By default, this plugin ignores duplicated fragment definitions such as:
```ts
const fragment = gql`
fragment A on Query {
id
}
`;

const query = gql`
${fragment}
query MyQuery {
...A
}
${fragment}
# Duplicated fragment interpolation
`;
```
This option affects all editor supporting functions, results of CLI commands and results of transformation.
If you set this option `false`, this plugin passes through query document without removing duplication.
## Built-in Type Generator Addons
### `typed-query-document`
Expand Down Expand Up @@ -649,56 +589,6 @@ const query = gql`
Default: `[]`. You can set an array of GraphQL AST document visitor functions. The visitor functions should be compatible to https://graphql.org/graphql-js/language/#visit .
## Template strings
This tool analyzes template string literals in .ts files such as:
```ts
const query = gql`
query MyQuery = {
viewer {
id
name
}
}
`;
```
> [!IMPORTANT]
> This tool cannot interpret queries containing too complex TypeScript expressions because it statically explores GraphQL queries.
```ts
/* It's ok */

const fragment = gql`
fragment MyFragment on User {
id
name
}
`;

const query = gql`
${fragment}
query MyQuery {
viewer {
...MyFragment
}
}
`;
```
```ts
/* Bad */

const query = gql`
query MyQuery {
${someComplexFunction()}
}
`;
```
Keep your queries static (see also https://blog.apollographql.com/5-benefits-of-static-graphql-queries-b7fa90b0b69a ).
## Available editors
I've checked the operation with the following editors:
Expand Down
Binary file modified capture.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "ts-graphql-plugin",
"version": "3.2.0",
"version": "4.0.0-alpha.0",
"description": "TypeScript Language Service Plugin for GraphQL",
"keywords": [
"typescript",
"graphql",
"language service"
],
"engines": {
"node": ">=18"
},
"main": "lib/index.js",
"bin": {
"tsgql": "lib/cli/cli.js",
Expand Down
2 changes: 0 additions & 2 deletions project-fixtures/gql-errors-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": "gql",
"schema": "schema.graphql",
"enabledGlobalFragments": true,
"localSchemaExtensions": ["local-extension.graphql"]
}
]
Expand Down
5 changes: 0 additions & 5 deletions project-fixtures/graphql-codegen-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
{
"name": "ts-graphql-plugin",
"schema": "schema.graphql",
"enabledGlobalFragments": true,
"tag": {
"name": "graphql",
"ignoreFunctionCallExpression": false
},
"exclude": ["codegen.ts", "src/gql"]
}
]
Expand Down
26 changes: 14 additions & 12 deletions project-fixtures/react-apollo-prj/GRAPHQL_OPERATIONS.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
# Extracted GraphQL Operations
## Queries

### GitHubQuery
### AppQuery

```graphql
fragment RepositoryFragment on Repository {
description
}

query GitHubQuery($first: Int!) {
query AppQuery($first: Int!) {
viewer {
repositories(first: $first) {
nodes {
id
...RepositoryFragment
...RepositoryItem_Repository
}
}
}
}

fragment RepositoryItem_Repository on Repository {
name
description
}
```

From [src/index.tsx:11:19](src/index.tsx#L11-L23)
From [src/App.tsx:6:19](src/App.tsx#L6-L17)

## Mutations

Expand All @@ -34,19 +35,20 @@ mutation UpdateMyRepository($repositoryId: ID!) {
}
```

From [src/index.tsx:25:22](src/index.tsx#L25-L31)
From [src/App.tsx:19:22](src/App.tsx#L19-L25)

## Fragments

### RepositoryFragment
### RepositoryItem_Repository

```graphql
fragment RepositoryFragment on Repository {
fragment RepositoryItem_Repository on Repository {
name
description
}
```

From [src/index.tsx:5:32](src/index.tsx#L5-L9)
From [src/RepositoryItem.tsx:4:53](src/RepositoryItem.tsx#L4-L9)

---
Extracted by [ts-graphql-plugin](https://github.com/Quramy/ts-graphql-plugin)
8 changes: 0 additions & 8 deletions project-fixtures/react-apollo-prj/apollo.config.js

This file was deleted.

1 change: 1 addition & 0 deletions project-fixtures/react-apollo-prj/local-extension.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
directive @nonreactive on FRAGMENT_SPREAD

0 comments on commit 8625906

Please sign in to comment.