How the ShopifySharp GraphQL Parser pipeline works
masterThe ShopifySharp.GraphQL.Parser project follows a multi-phase pipeline to transform a raw GraphQL schema into C# models and fluent query builders.
The Processing Pipeline
Parsing & AST Mapping: The entrypoint
Parser.ParseAndWriteAsyncusesGraphQLParser.Parserto create aGraphQLDocumentAST. A customVisitortraverses this AST and maps GraphQL definitions to internal structures viaAstNodeMapper.fs:GraphQLObjectTypeDefinition$\rightarrow$ClassGraphQLInterfaceTypeDefinition$\rightarrow$InterfaceGraphQLEnumTypeDefinition$\rightarrow$VisitedEnumGraphQLInputObjectTypeDefinition$\rightarrow$InputObjectGraphQLUnionTypeDefinition$\rightarrow$UnionTypeThese are stored in aParserContextfor later use.
Reachability Analysis: To avoid generating dead code, the
ReachabilityAnalyzerstarts at theQueryRootandMutationoperations. It recursively discovers all referenced return types, argument types, interfaces, and union cases. Only types tracked by theTypeReferenceTrackerproceed to code generation.Code Generation: Two parallel processes generate code:
- Model Generation (
VisitedTypeWriter.fs): Creates C#record,interface, andenumrepresentations of reachable types, including necessary serialization attributes like[JsonPropertyName],[JsonPolymorphic], and[JsonDerivedType]. - Query Builder Generation (
QueryBuilderWriter.fs): Generates fluent query builders. This includes field builders, argument builders, and specialized builders for unions (UnionsBuilderWriter.fs) and interfaces (InterfacesBuilderWriter.fs) to handle inline fragment selections (e.g.,.OnSomeUnionType(...)).
- Model Generation (
Roslyn Splitting & File Writing: The generated output is passed to the
FileSystem.fsmodule. It uses Roslyn'sCSharpSyntaxTreeAPI to parse the entire output, extract individualBaseTypeDeclarationSyntaxitems, and split them into separate.generated.csfiles organized by namespace-mapped subdirectories.