feat: add schema aware type inference #6
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Schema-Aware Type Inference to GraphQL Fragment Generator
Overview
This PR enhances the GraphQL fragment generator with schema awareness, allowing it to accurately predict field types based on GraphQL schema definitions. This addresses a significant limitation in the current implementation, where type information had to be manually specified or was inferred with limited accuracy.
Changes Summary
Detailed Changes
1. Schema Models (
GraphQLSourceGen/Models/GraphQLSchemaModels.cs)Added a new file with models for GraphQL schema elements:
GraphQLSchema: Container for all schema elements with helper methodsGraphQLTypeDefinition: For object types with fields and interface implementationsGraphQLInterfaceDefinition: For interface types with field definitionsGraphQLUnionDefinition: For union types with possible typesGraphQLEnumDefinition: For enum types with valuesGraphQLScalarDefinition: For custom scalar typesGraphQLInputDefinition: For input object typesGraphQLFieldDefinition: For field definitions with type informationGraphQLInputValueDefinition: For input values (arguments and input fields)These models provide a comprehensive representation of GraphQL schema elements, enabling accurate type inference.
2. Schema Parser (
GraphQLSourceGen/Parsing/GraphQLSchemaParser.cs)Implemented a parser that extracts type information from GraphQL schema definitions:
@deprecatedThe parser is designed to handle complex schema features while providing meaningful error messages for invalid syntax.
3. Enhanced Fragment Generator (
GraphQLSourceGen/GraphQLFragmentGenerator.cs)Modified the fragment generator to use schema information:
EnhanceFragmentsWithSchemamethod to apply type informationThese changes enable the generator to produce more accurate C# types based on the GraphQL schema.
4. Configuration Options (
GraphQLSourceGen/Configuration/GraphQLSourceGenOptions.cs)Added new configuration options:
UseSchemaForTypeInference: Enable/disable schema-based type inferenceSchemaFilePaths: Paths to schema definition filesCustomScalarMappings: Custom mappings for scalar typesValidateNonNullableFields: Generate validation for non-nullable fieldsIncludeFieldDescriptions: Include field descriptions from schemaThese options provide flexibility in how schema information is used for type generation.
5. Diagnostics (
GraphQLSourceGen/Diagnostics/DiagnosticDescriptors.cs)Added new diagnostics for schema-related issues:
SchemaFileNotFound: When a specified schema file is not foundInvalidSchemaDefinition: For invalid schema syntaxTypeNotFoundInSchema: When a fragment references a type not in the schemaFieldNotFoundInType: When a field is not found in a typeInvalidFragmentOnInterfaceOrUnion: For fragments on interfaces/unions without__typenameThese diagnostics help developers identify and fix issues in their GraphQL schemas.
6. Parser Enhancements (
GraphQLSourceGen/Parsing/GraphQLParser.cs)Modified the existing parser:
Tokenizemethod public for reuse in schema parserThese changes ensure compatibility between fragment and schema parsing.
7. Build Properties (
GraphQLSourceGen/build/GraphQLSourceGen.props)Updated default configuration:
8. Sample Implementation (
GraphQLSourceGen.Samples/SchemaAwareExample.cs)Added a sample implementation demonstrating schema-aware fragment generation:
9. Tests
Added comprehensive tests:
GraphQLSchemaParserTests.cs: Tests for schema parsingGraphQLSchemaAwareGeneratorTests.cs: Tests for schema-aware type inferenceComplexSchemaTests.cs: Tests for complex schema featuresRationale
The primary motivation for these changes is to improve type safety and developer experience:
Edge Cases and Considerations
Breaking Changes
None. The implementation is backward compatible with existing code. Schema awareness is opt-in through configuration options.
Future Improvements
@deprecatedTesting
The implementation includes comprehensive tests covering:
All tests pass, ensuring the implementation is robust and reliable.