Skip to content

Commit

Permalink
Merge pull request #17 from rintoj/feat/scalar-types
Browse files Browse the repository at this point in the history
Feat/scalar types
  • Loading branch information
rintoj authored Jul 19, 2024
2 parents 3066c4f + d5d8a9e commit c554c35
Show file tree
Hide file tree
Showing 10 changed files with 698 additions and 76 deletions.
38 changes: 36 additions & 2 deletions src/generator/hook/graphql-document-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ts from 'typescript'
import {
createArgumentDefinition,
createInputValueDefinition,
defaultScalarTypes,
getFieldHash,
getFieldName,
hasAQuery,
Expand All @@ -19,7 +20,7 @@ import {
updateName,
updateVariableDefinitions,
} from '../../gql'
import { createArrayType, createType } from '../../ts'
import { createArrayType, createType, traverse } from '../../ts'
import { camelCase, className, toString } from '../../util'
import { GraphQLParserContext } from './graphql-parser-context'

Expand All @@ -41,6 +42,34 @@ function parseEnum(enumType: gql.GraphQLEnumType, context: GraphQLParserContext)
)
}

function findScalarType(sourceFile: ts.SourceFile, name: string) {
let typeAliasDeclaration: ts.TypeAliasDeclaration | undefined
traverse(sourceFile, {
[ts.SyntaxKind.TypeAliasDeclaration]: (node: ts.TypeAliasDeclaration) => {
if (node.name.getText() === name) {
typeAliasDeclaration = node
return true
}
},
})
return typeAliasDeclaration
}

function parseScalar(scalarType: gql.GraphQLScalarType, context: GraphQLParserContext) {
const name = scalarType.name
if (defaultScalarTypes.includes(name)) return
const existingType = findScalarType(context.sourceFile, name)
context.addScalar(
existingType ??
ts.factory.createTypeAliasDeclaration(
[ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
ts.factory.createIdentifier(scalarType.name),
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
),
)
}

function parseUnionType(unionType: gql.GraphQLUnionType, context: GraphQLParserContext) {
context.addUnion(
ts.factory.createTypeAliasDeclaration(
Expand Down Expand Up @@ -138,6 +167,8 @@ function parseInputType(schemaType: gql.GraphQLInputType, context: GraphQLParser
)
} else if (gql.isEnumType(type)) {
parseEnum(type, context)
} else if (gql.isScalarType(type)) {
parseScalar(type, context)
}
}

Expand Down Expand Up @@ -310,9 +341,10 @@ export function parseDocument(
inputDocument: gql.DocumentNode,
schema: gql.GraphQLSchema,
hookName: string | undefined,
sourceFile: ts.SourceFile,
) {
const typeInfo = new gql.TypeInfo(schema)
const context = new GraphQLParserContext(typeInfo)
const context = new GraphQLParserContext(typeInfo, sourceFile)
const document = fixDocument(inputDocument, schema, hookName, context)
gql.visit(
document,
Expand All @@ -330,6 +362,8 @@ export function parseDocument(
parseObjectType(node, type, context)
} else if (gql.isEnumType(type)) {
parseEnum(type, context)
} else if (gql.isScalarType(type)) {
parseScalar(type, context)
} else if (gql.isUnionType(type)) {
parseUnionType(type, context)
}
Expand Down
11 changes: 10 additions & 1 deletion src/generator/hook/graphql-parser-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export class GraphQLParserContext {
private readonly typeNameTracker = new TypeNameTracker()
private readonly parameterNameTracker = new ParameterNameTracker()

constructor(public readonly typeInfo: gql.TypeInfo) {}
constructor(
public readonly typeInfo: gql.TypeInfo,
public readonly sourceFile: ts.SourceFile,
) {}

getParameters() {
return Object.values(this.parameters)
Expand Down Expand Up @@ -86,6 +89,12 @@ export class GraphQLParserContext {
return this
}

addScalar(type: ts.TypeAliasDeclaration) {
const name = type.name.escapedText ?? ''
this.types[name] = type
return this
}

addParameter(propertyConfig: PropertyConfig, variableDefinition: gql.VariableDefinitionNode) {
this.parameters[propertyConfig.name] = propertyConfig
this.variableDefinition[propertyConfig.name] = variableDefinition
Expand Down
Loading

0 comments on commit c554c35

Please sign in to comment.