Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build and Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore --configuration Release

- name: Test
run: dotnet test --no-build --configuration Release --verbosity normal
32 changes: 32 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Publish NuGet Package

on:
release:
types: [published]

jobs:
build-and-publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore --configuration Release

- name: Test
run: dotnet test --no-build --configuration Release --verbosity normal

- name: Pack
run: dotnet pack GraphQLSourceGen/GraphQLSourceGen.csproj --no-build --configuration Release -o ./nupkgs

- name: Push to NuGet
run: dotnet nuget push ./nupkgs/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
Empty file added CHANGELOG.md
Empty file.
26 changes: 26 additions & 0 deletions GraphQLSourceGen.Samples/GraphQLSourceGen.Samples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\GraphQLSourceGen\GraphQLSourceGen.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="true" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="**\*.graphql" />
</ItemGroup>

<!-- Custom configuration -->
<PropertyGroup>
<GraphQLSourceGenNamespace>GraphQL.Generated</GraphQLSourceGenNamespace>
<GraphQLSourceGenUseRecords>true</GraphQLSourceGenUseRecords>
</PropertyGroup>

</Project>
76 changes: 76 additions & 0 deletions GraphQLSourceGen.Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;

namespace GraphQLSourceGen.Samples
{
// Define the classes that would normally be generated
public class UserBasicFragment
{
public string? Id { get; set; }
public string? Name { get; set; }
public string? Email { get; set; }
public bool? IsActive { get; set; }
}

public class PostWithStatsFragment
{
public string? Id { get; set; }
public string? Title { get; set; }
public int? ViewCount { get; set; }
public double? Rating { get; set; }
public bool IsPublished { get; set; }
public DateTime? PublishedAt { get; set; }
public List<string>? Tags { get; set; }
public List<string> Categories { get; set; } = new List<string>();
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine("GraphQL Source Generator Samples");
Console.WriteLine("================================");

// Create a UserBasicFragment instance
var user = new UserBasicFragment
{
Id = "user-123",
Name = "John Doe",
Email = "john.doe@example.com",
IsActive = true
};

Console.WriteLine("\nUser Basic Fragment:");
Console.WriteLine($"ID: {user.Id}");
Console.WriteLine($"Name: {user.Name}");
Console.WriteLine($"Email: {user.Email}");
Console.WriteLine($"Active: {user.IsActive}");

// Create a PostWithStatsFragment instance
var post = new PostWithStatsFragment
{
Id = "post-123",
Title = "GraphQL and C# Source Generators",
ViewCount = 1250,
Rating = 4.8,
IsPublished = true,
PublishedAt = DateTime.Now.AddDays(-14),
Tags = new List<string> { "GraphQL", "C#", "Source Generators" },
Categories = new List<string> { "Programming", "Web Development" }
};

Console.WriteLine("\nPost With Stats Fragment:");
Console.WriteLine($"ID: {post.Id}");
Console.WriteLine($"Title: {post.Title}");
Console.WriteLine($"Views: {post.ViewCount}");
Console.WriteLine($"Rating: {post.Rating}");
Console.WriteLine($"Published: {post.IsPublished}");
Console.WriteLine($"Published At: {post.PublishedAt}");
Console.WriteLine($"Tags: {string.Join(", ", post.Tags ?? new List<string>())}");
Console.WriteLine($"Categories: {string.Join(", ", post.Categories)}");

Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
}
}
Empty file.
78 changes: 78 additions & 0 deletions GraphQLSourceGen.Samples/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# This is a sample GraphQL file with fragment definitions

# A simple fragment with scalar fields
fragment UserBasic on User {
id
name
email
isActive
}

# A fragment with nested objects and lists
fragment UserDetails on User {
id
name
email
isActive
profile {
bio
avatarUrl
joinDate
}
posts {
id
title
content
createdAt
}
followers {
id
name
}
}

# A fragment with non-nullable fields
fragment RequiredUserInfo on User {
id!
name!
email
}

# A fragment with deprecated fields
fragment UserWithDeprecated on User {
id
name
email
username @deprecated(reason: "Use email instead")
oldField @deprecated
}

# A fragment that uses another fragment
fragment UserWithPosts on User {
...UserBasic
posts {
id
title
publishedAt
comments {
id
text
author {
id
name
}
}
}
}

# A fragment with various scalar types
fragment PostWithStats on Post {
id: ID!
title: String!
viewCount: Int
rating: Float
isPublished: Boolean!
publishedAt: DateTime
tags: [String]
categories: [String!]!
}
2 changes: 1 addition & 1 deletion GraphQLSourceGen.Tests/GraphQLFragmentGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fragment UserBasic on User {
var fragments = GraphQLParser.ParseContent(graphqlContent);

// Verify the fragment was parsed correctly
Assert.Equal(1, fragments.Count);
Assert.Single(fragments);

var fragment = fragments[0];
Assert.Equal("UserBasic", fragment.Name);
Expand Down
10 changes: 5 additions & 5 deletions GraphQLSourceGen.Tests/GraphQLParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fragment UserBasic on User {
var fragments = GraphQLParser.ParseContent(graphql);

// Assert
Assert.Equal(1, fragments.Count);
Assert.Single(fragments);

var fragment = fragments[0];
Assert.Equal("UserBasic", fragment.Name);
Expand Down Expand Up @@ -53,7 +53,7 @@ fragment UserDetails on User {
var fragments = GraphQLParser.ParseContent(graphql);

// Assert
Assert.Equal(1, fragments.Count);
Assert.Single(fragments);

var fragment = fragments[0];
Assert.Equal("UserDetails", fragment.Name);
Expand Down Expand Up @@ -82,7 +82,7 @@ oldField @deprecated
var fragments = GraphQLParser.ParseContent(graphql);

// Assert
Assert.Equal(1, fragments.Count);
Assert.Single(fragments);

var fragment = fragments[0];
Assert.Equal(3, fragment.Fields.Count);
Expand Down Expand Up @@ -114,7 +114,7 @@ fragment UserWithPosts on User {
var fragments = GraphQLParser.ParseContent(graphql);

// Assert
Assert.Equal(1, fragments.Count);
Assert.Single(fragments);

var fragment = fragments[0];
Assert.Equal(2, fragment.Fields.Count);
Expand Down Expand Up @@ -149,7 +149,7 @@ fragment PostWithStats on Post {
var fragments = GraphQLParser.ParseContent(graphql);

// Assert
Assert.Equal(1, fragments.Count);
Assert.Single(fragments);

var fragment = fragments[0];
Assert.Equal(8, fragment.Fields.Count);
Expand Down
30 changes: 30 additions & 0 deletions GraphQLSourceGen/Configuration/GraphQLSourceGenOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace GraphQLSourceGen.Configuration
{
/// <summary>
/// Configuration options for GraphQL Source Generator
/// </summary>
public class GraphQLSourceGenOptions
{
/// <summary>
/// The namespace to use for generated types. If null, the namespace will be "GraphQL.Generated"
/// </summary>
public string? Namespace { get; set; }

/// <summary>
/// Whether to generate records (true) or classes (false)
/// </summary>
public bool UseRecords { get; set; } = true;

/// <summary>
/// Whether to use init-only properties
/// </summary>
public bool UseInitProperties { get; set; } = true;

/// <summary>
/// Whether to include XML documentation comments in generated code
/// </summary>
public bool GenerateDocComments { get; set; } = true;
}
}
Loading