Skip to content

Convert GraphQLRequest and ArgumentValue to record types#87

Open
jonasessen wants to merge 1 commit intoLinq2GraphQL:mainfrom
jonasessen:refactor/dto-record-types
Open

Convert GraphQLRequest and ArgumentValue to record types#87
jonasessen wants to merge 1 commit intoLinq2GraphQL:mainfrom
jonasessen:refactor/dto-record-types

Conversation

@jonasessen
Copy link
Copy Markdown

Problem

GraphQLRequest and ArgumentValue are plain data-transfer objects — they exist solely to hold values with no behaviour. They are currently defined as class types, which:

  • Allows accidental mutation after construction
  • Uses reference equality (two instances with identical data are not equal)
  • Requires more boilerplate than necessary

Solution

Convert both to C# record types, which are designed exactly for this purpose.

GraphQLRequest — fully immutable. Both properties become init-only since the object is constructed once and never mutated:

// Before
public class GraphQLRequest
{
    public string Query { get; set; }
    public Dictionary<string, object> Variables { get; set; }
}

// After
public record GraphQLRequest
{
    public string Query { get; init; }
    public Dictionary<string, object> Variables { get; init; }
}

ArgumentValue — mostly immutable. GraphName, GraphType, and Value are set at construction and never changed (init). VariableName remains set because the query builder appends a uniqueness suffix to it during query construction — this is noted with a comment.

Benefits

  • Immutabilityinit-only properties prevent accidental mutation after construction
  • Value equality — two instances with identical data compare as equal, which is the natural expectation for DTOs
  • with expressions — callers can cleanly create modified copies: request with { Query = newQuery }
  • Better ToString() — records produce a readable representation automatically, useful for debugging

Compatibility

Both changes are fully backward-compatible. Object initializer syntax (new GraphQLRequest { Query = ..., Variables = ... }) works identically with init properties.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant