dotansimha / dotansimha/graphql-code-generator-community
Support nullable DateOnly, TimeSpan, and DateTimeOffset scalar mappings in C# plugin
- Dominant language
- TypeScript
- Stars
- 137
- Forks
- 195
- Avg merge
- 6h 20m
- Merged PRs (30d)
- 16
Description
### Which packages are impacted by your issue?
@graphql-codegen/c-sharp-operations, @graphql-codegen/c-sharp-common
### Describe the bug
When GraphQL nullable scalar fields are mapped to C# value types (DateOnly, TimeSpan, DateTimeOffset) via scalar configuration, the code generator fails to generate the nullable suffix (?), producing invalid C# code. This works correctly for DateTime but fails for these other built-in .NET value types.
### Your Example Website or App
https://codesandbox.io/p/devbox/ecstatic-newton-hn4ws7?file=%2Fschema.graphql%3A11%2C49
### Steps to Reproduce the Bug or Issue
1. Create a GraphQL schema with nullable scalar fields mapped to DateOnly, TimeSpan, or DateTimeOffset
2. Run code generation with the C# plugin
3. Inspect generated C# code
4. See that the nullable fields lack the ? suffix
Example GraphQL schema:
```graphql
type Query {
createdDate: Date! # Required - OK
updatedDate: Date # Nullable - BROKEN
duration: Duration # Nullable - BROKEN
offset: DateOffset # Nullable - BROKEN
}
```
Generated C# (current - broken):
```csharp
public DateOnly? createdDate { get; set; } // OK (required)
public DateOnly updatedDate { get; set; } // BROKEN (should be DateOnly?)
public TimeSpan duration { get; set; } // BROKEN (should be TimeSpan?)
public DateTimeOffset offset { get; set; } // BROKEN (should be DateTimeOffset?)
```
### Expected behavior
All nullable value-type scalars should generate with the nullable suffix, consistent with DateTime:
Generated C# (expected):
```csharp
public DateOnly? createdDate { get; set; } // Correct
public DateOnly? updatedDate { get; set; } // Correct
public TimeSpan? duration { get; set; } // Correct
public DateTimeOffset? offset { get; set; } // Correct
```
### Screenshots or Videos
_No response_
### Platform
- OS: Windows/macOS/Linux (cross-platform)
- NodeJS: 18.x / 20.x
- graphql version: 16.x
- @graphql-codegen/* version(s): 5.x
### Codegen Config File
```yaml
schema: schema.graphql
documents: '**/*.graphql'
generates:
types.cs:
plugins:
- c-sharp
config:
scalars:
Date: 'DateOnly'
Duration: 'TimeSpan'
DateOffset: 'DateTimeOffset'
```
### Additional context
Root Cause: The allow list in [scalars.ts] only includes a limited set of value types (bool, byte, int, DateTime, etc.).
The `isValueType()` helper function checks membership in this list, and nullable suffix generation is already correctly implemented - it just needs these types in the allow list.
Current List:
```ts
export const csharpValueTypes = [
'bool', 'byte', 'sbyte', 'char', 'decimal', 'double', 'float',
'int', 'uint', 'long', 'ulong', 'short', 'ushort', 'DateTime'
];
```
Proposed Fix: Add the three missing .NET value types to the allow list (same change made in the fix branch).
Backwards Compatibility: Fully backwards compatible, only extends the type list. No existing generated code is affected. This fix corrects previously broken behavior.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in scalars.ts by tracing the csharpValueTypes allow list and the isValueType() helper used for nullable suffix generation. Reproduce the issue with the provided GraphQL schema and scalar configuration, then verify that nullable DateOnly, TimeSpan, and DateTimeOffset fields generate with the C# nullable suffix while required fields remain unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, graphql, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 88/100