Skip to content

Commit

Permalink
feat: Change tag default value
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Mar 29, 2024
1 parent bdd2a50 commit ef587c4
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 133 deletions.
107 changes: 10 additions & 97 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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 +73,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 +121,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 +254,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,7 +266,7 @@ For example:
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": "gql"
"tag": "myGraphqlTag"
}
]
}
Expand All @@ -281,53 +276,21 @@ For example:
```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
}
const query2 = myGraphqlTag(`
query AppQuery {
__typename
}
`);
```

Configure as the following:

```js
/* tsconfig.json */

{
"compilerOptions": {
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": {
"name": "graphql",
"ignoreFunctionCallExpression": false
}
}
]
}
}
// Not recognized as GraphQL document
const str3 = `<div></div>`;
const str4 = otherTagFn`foooo`;
```

The `tag` option accepts the following type:
Expand Down Expand Up @@ -649,56 +612,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
1 change: 0 additions & 1 deletion project-fixtures/gql-errors-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": "gql",
"schema": "schema.graphql",
"localSchemaExtensions": ["local-extension.graphql"]
}
Expand Down
4 changes: 0 additions & 4 deletions project-fixtures/graphql-codegen-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
{
"name": "ts-graphql-plugin",
"schema": "schema.graphql",
"tag": {
"name": "graphql",
"ignoreFunctionCallExpression": false
},
"exclude": ["codegen.ts", "src/gql"]
}
]
Expand Down
1 change: 0 additions & 1 deletion project-fixtures/react-apollo-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
{
"name": "ts-graphql-plugin",
"schema": "schema.graphql",
"tag": "gql",
"exclude": ["src/__generated__"],
"typegen": {
"addons": ["ts-graphql-plugin/addons/typed-query-document"]
Expand Down
1 change: 0 additions & 1 deletion project-fixtures/simple-prj/tsconfig.invalid-addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"plugins": [
{
"name": "../../lib",
"tag": "gql",
"schema": "schema.graphql",
"typegen": {
"addons": ["invalid-module"]
Expand Down
1 change: 0 additions & 1 deletion project-fixtures/simple-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": "gql",
"schema": "schema.graphql",
"localSchemaExtensions": ["local-extension.graphql"],
"typegen": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": "gql",
"schema": "schema.graphql"
}
]
Expand Down
1 change: 0 additions & 1 deletion project-fixtures/transformation-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"plugins": [
{
"name": "ts-graphql-plugin",
"tag": "gql",
"schema": "schema.graphql"
}
]
Expand Down
1 change: 0 additions & 1 deletion project-fixtures/typegen-addon-prj/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{
"name": "../../lib",
"_name": "ts-graphql-plugin",
"tag": "gql",
"schema": "schema.graphql",
"typegen": {
"addons": ["./addon"]
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/type-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe(TypeGenerator, () => {
files: [
{
fileName: 'main.ts',
content: 'export query = `query MyQuery { hello }`;',
content: 'export query = gql`query MyQuery { hello }`;',
},
],
});
Expand Down
14 changes: 7 additions & 7 deletions src/transformer/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('transformer', () => {
expect(
transformAndPrint({
tsContent: `
const query = hoge\`abc\`;
const query = gql\`abc\`;
`,
docContent: `
query {
Expand All @@ -57,7 +57,7 @@ describe('transformer', () => {
expect(
transformAndPrint({
tsContent: `
const query = \`abc\`;
const query = gql\`abc\`;
`,
docContent: `
query {
Expand All @@ -73,7 +73,7 @@ describe('transformer', () => {
expect(
transformAndPrint({
tsContent: `
const query = \`abc\${def}\`;
const query = gql\`abc\${def}\`;
`,
docContent: `
query {
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('transformer', () => {
expect(
transformAndPrint({
tsContent: `
const fragment = hoge\`abc\`;
const fragment = gql\`abc\`;
`,
docContent: `
fragment X on Query {
Expand All @@ -242,7 +242,7 @@ describe('transformer', () => {
expect(
transformAndPrint({
tsContent: `
const query = hoge\`abc\`;
const query = gql\`abc\`;
`,
docContent: `
query MyQuery {
Expand All @@ -258,7 +258,7 @@ describe('transformer', () => {
expect(
transformAndPrint({
tsContent: `
const fragment = hoge\`abc\`;
const fragment = gql\`abc\`;
`,
docContent: `
fragment X on Query {
Expand All @@ -275,7 +275,7 @@ describe('transformer', () => {
expect(
transformAndPrint({
tsContent: `
const query = hoge\`abc\`;
const query = gql\`abc\`;
`,
docContent: `
query MyQuery {
Expand Down
4 changes: 0 additions & 4 deletions src/transformer/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ export function getTransformer({
templateNode = node.template;
} else if (ts.isCallExpression(node) && !!getTemplateNodeUnder(node, tag)) {
templateNode = node.arguments[0] as ts.TemplateLiteral;
} else if (tag.allowNotTaggedTemplate && ts.isNoSubstitutionTemplateLiteral(node)) {
templateNode = node;
} else if (tag.allowNotTaggedTemplate && ts.isTemplateExpression(node)) {
templateNode = node;
}

if (!templateNode) return ts.visitEachChild(node, visit, ctx);
Expand Down
14 changes: 7 additions & 7 deletions src/ts-ast-util/tag-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ describe(parseTagConfig, () => {
{
config: undefined,
expected: {
names: [],
allowNotTaggedTemplate: true,
names: ['gql', 'graphql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
allowFunctionCallExpression: true,
} as StrictTagCondition,
},
{
Expand Down Expand Up @@ -110,8 +110,8 @@ describe(getTemplateNodeUnder, () => {
};

it.each([
{ source: '`query { }`', config: undefined },
{ source: 'gql`query { }`', config: 'gql' },
{ source: 'gql`query { }`', config: undefined },
{ source: '`query { }`', config: '' },
{ source: 'gql`query { }`', config: ['gql', 'graphql'] },
{
source: 'graphql(`query { }`)',
Expand Down Expand Up @@ -151,8 +151,8 @@ describe(getTagName, () => {
};

it.each([
{ source: '`query { }`', expected: '', config: undefined },
{ source: 'gql`query { }`', expected: 'gql', config: 'gql' },
{ source: 'gql`query { }`', expected: 'gql', config: undefined },
{ source: '`query { }`', expected: '', config: '' },
{ source: 'gql`query { }`', expected: 'gql', config: ['gql', 'graphql'] },
{
source: 'graphql(`query { }`)',
Expand Down
18 changes: 13 additions & 5 deletions src/ts-ast-util/tag-utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ts from 'typescript';
import type { TagConfig, StrictTagCondition } from './types';

// TODO Change default at v4
export const DEFAULT_TAG_CONDITION = {
names: [],
allowNotTaggedTemplate: true,
names: ['gql', 'graphql'],
allowNotTaggedTemplate: false,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
allowFunctionCallExpression: true,
} satisfies StrictTagCondition;

export function parseTagConfig(tagConfig: TagConfig | undefined): StrictTagCondition {
Expand All @@ -20,10 +19,19 @@ export function parseTagConfig(tagConfig: TagConfig | undefined): StrictTagCondi
}
};

if (!tagConfig) {
if (tagConfig == null) {
return DEFAULT_TAG_CONDITION;
}

if (tagConfig === '') {
return {
names: [],
allowNotTaggedTemplate: true,
allowTaggedTemplateExpression: true,
allowFunctionCallExpression: false,
};
}

if (typeof tagConfig === 'string' || Array.isArray(tagConfig)) {
return {
names: parseName(tagConfig),
Expand Down
2 changes: 1 addition & 1 deletion src/typegen-addons/typed-query-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe(TypedQueryDocumentAddonFactory, () => {

it('should add export statement using TypedQueryDocumentNode', () => {
const { outputSourceFiles } = addonTester.generateTypes({
files: [{ fileName: 'main.ts', content: 'const query = `query MyQuery { hello }`' }],
files: [{ fileName: 'main.ts', content: 'const query = gql`query MyQuery { hello }`' }],
schemaSDL: sdl,
});
expect(outputSourceFiles.map(f => f.content)).toMatchSnapshot();
Expand Down

0 comments on commit ef587c4

Please sign in to comment.