Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core Code Generation MVP #4073

Closed
wants to merge 52 commits into from
Closed

Conversation

benmccallum
Copy link
Collaborator

@benmccallum benmccallum commented Aug 11, 2021

Adds EF Core code generation and an analyzer so that we can go SDL-first --> EF model --> DB.

Functionality:

  • Analyzer
  • Code generation project
  • EF entities generation (from object types that will be backed by a table)
  • EF DbContext generation (with DbSet members for object types that will be backed by a table)
  • @schemaConventions directive (with pluralized table names option) (addresses cloud#116)
  • @table directive (with name property to specify custom table name mapping) (addresses cloud#117)
  • PK determination (@key, falling back to Id-looking field, falling back to auto-created field using schema convention) (addresses cloud#112)
  • A bunch of other stubbed directives that don't do anything yet
  • GQL Query type generation
  • Field resolver per table entity (with IQueryable, x attr, y attr, z attr)
  • Startup DI code generation

Concepts:

  • As a schema object type is processed, a context is built up that allows us to capture things like PK, table name, etc and build up our knowledge of the SDL as we need to generate code. This will help us in the future when we start doing more complicated directives for things like relations.

Testing:

  • Snapshooter test coverage of the raw code generated
  • Integration testing (as per Neo4J, that also compiles the source) (likely another PR)

@benmccallum benmccallum marked this pull request as ready for review August 12, 2021 20:01
@benmccallum benmccallum added the 👓 ready-for-review The PR is ready for review. label Aug 12, 2021
@benmccallum
Copy link
Collaborator Author

@michaelstaib , this is ready for review really. Tomorrow I'm gonna:

  • add a couple of tests for the PK stuff and
  • add the code generation for the DI part, which I'll try copy the solution from the Neo4J one for

But you should start reviewing it in any case if you have the time.

{
public partial class EntityFrameworkSourceGenerator
{
private const string _dll = ".dll";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do not need this once I have merged my generator improvements.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have those improvements made it in? Looks like Neo4J equivalent is doing the same thing still but let me know if I need to adjust anything, though perhaps better as a new branch.

{
public class ForeignKeyDirective
{
public string To { get; set; } = default!; // TODO: Potentially this supports nullable and can be derived by the field it annotates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once I reworked directives we could also have a constructor that passes this in.


namespace HotChocolate.CodeGeneration.EntityFramework.Types
{
public class OneToManyDirective
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is nice that you collocate the type wit the model.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I know it was against the original code but it made it a lot easier to copy-paste and easier to build out. They're fairly light on, so I think it's ok.

@michaelstaib
Copy link
Member

/AzurePipelines run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@sonarcloud
Copy link

sonarcloud bot commented Aug 30, 2021

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 23 Code Smells

0.0% 0.0% Coverage
1.5% 1.5% Duplication

@@ -17,6 +18,8 @@ public interface IDirective

T ToObject<T>();

T ToObject<T>(Type type);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelstaib , would this be a problem?

I have a scenario where I'd like to be able to loop the directives of a field, finding those that implement an interface, but then actually get the directive class, but .ToObject<T> doesn't cut it as T in my case would be an interface and the Activator code fails. Have a look at how it's used in SchemaExtensions::GetDirectivesWhereRuntimeTypeImplements

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage:
directive.ToObject<IXyzDirective>(directive.Type.RuntimeType)

@stale
Copy link

stale bot commented May 4, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the ⌛ stale Nothing happened with this issue in quite a while label May 4, 2022
@stale stale bot closed this May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌶️ hot chocolate 👓 ready-for-review The PR is ready for review. ⌛ stale Nothing happened with this issue in quite a while
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants