Feature/turbopack * . * . * pin import-in-the-middle * update marker * revert back to using *.graphql.ts Approved-by: Linus Flood
3.4 KiB
3.4 KiB
GraphQL to TypeScript Converter
This script converts GraphQL files (.graphql) to TypeScript files (.graphql.ts) that use the gql template literal from graphql-tag.
Features
- Converts individual fragments and queries into separate TypeScript exports
- Handles GraphQL imports (
#import) and converts them to TypeScript imports - Preserves fragment references and generates proper import statements
- Groups multiple imports from the same file
- Supports fragments, queries, mutations, and subscriptions
- Handles files with multiple operations
Usage
Basic Conversion
Convert all GraphQL files in the project:
yarn graphql:convert
Convert files matching a specific pattern:
yarn graphql:convert "packages/trpc/**/*.graphql"
Convert a single file:
yarn graphql:convert "path/to/file.graphql"
Options
--dry-run: Preview what files would be converted without actually converting them--delete-originals: Delete original.graphqlfiles after successful conversion--help,-h: Show help message
Examples
Preview conversion:
yarn graphql:convert --dry-run
Convert and delete originals:
yarn graphql:convert --delete-originals
Convert specific directory:
yarn graphql:convert "packages/trpc/lib/graphql/Fragments/**/*.graphql"
Input Format
GraphQL Fragment
fragment Contact on ContactBlock {
sections {
__typename
}
}
GraphQL Query with Imports
#import "../Fragments/System.graphql"
#import "../Fragments/Metadata.graphql"
query GetData($locale: String!) {
data(locale: $locale) {
...System
...Metadata
}
}
Output Format
TypeScript Fragment
import { gql } from "graphql-tag";
export const Contact = gql`
fragment Contact on ContactBlock {
sections {
__typename
}
}
`;
TypeScript Query with Imports
import { gql } from "graphql-tag";
import { System } from "../Fragments/System.graphql";
import { Metadata } from "../Fragments/Metadata.graphql";
export const GetData = gql`
query GetData($locale: String!) {
data(locale: $locale) {
...System
...Metadata
}
}
${System}
${Metadata}
`;
How It Works
- Parse GraphQL Files: Reads
.graphqlfiles and extracts imports and operations - Handle Imports: Converts
#importstatements to TypeScript imports by:- Reading the imported file to determine export names
- Converting paths from
.graphqlto.graphql.ts - Grouping multiple imports from the same file
- Extract Operations: Identifies fragments, queries, mutations, and subscriptions
- Generate TypeScript: Creates TypeScript files with:
gqltemplate literals- Proper import statements
- Named exports for each operation
Dependencies
glob: For file pattern matchingtsx: For TypeScript execution@types/node: For Node.js typesgraphql-tag: For thegqltemplate literal (runtime dependency)
Notes
- The script preserves the original file structure and naming
- Fragment references (
...FragmentName) are preserved in the GraphQL content - Multiple operations in a single file are split into separate exports
- Import conflicts are avoided by using the exact export names from referenced files
- Generated files maintain the same directory structure with
.graphql.tsextension