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

Inconsistencies in generated files #3414

Open
evellior opened this issue Dec 9, 2024 · 6 comments
Open

Inconsistencies in generated files #3414

evellior opened this issue Dec 9, 2024 · 6 comments

Comments

@evellior
Copy link

evellior commented Dec 9, 2024

What happened?

Each time gqlgen runs it almost randomly picks whether it will use interface{} or any in some generated code. I know they function the same, but is there a way to make the generated output more consistent?

I don't think my use case is specifically important, I assume having a consistent output when generating code would be good regardless. But the use case I have and how I'm reproducing the issue is with an automated test intended to catch when people change .graphql schema files and forget to regenerate the application code. I have the following test workflow:

name: GQLGen Test

on:
  workflow_call: # Allows this to be executed by other workflows
  workflow_dispatch: # Allows this to be manually executed on any branch
  pull_request:
    branches: [ development ]

jobs:
  test:
    name: Generate graph
    runs-on: ubuntu-latest
    steps:

    - uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: 1.23.1

    - name: Run test gqlgen
      run: |
        go get -d github.com/99designs/gqlgen@v0.17.53
        cd app/api/graphql; go run github.com/99designs/gqlgen

    - name: Check file diff
      run: |
        if [ -n "$(git status --porcelain)" ]; then 
          echo "::error::The generated graphql resolvers do not match with those in the branch!"
          git status -v -v
          exit 1
        fi

The test failed as the generated files had the following diff:

Error: The generated graphql resolvers do not match with those in the branch!
On branch development
Your branch is up to date with 'origin/development'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   app/api/graphql/generated/graph/schema_gen.go

--------------------------------------------------
Changes not staged for commit:
diff --git i/app/api/graphql/generated/graph/schema_gen.go w/app/api/graphql/generated/graph/schema_gen.go
index f9ceeff..50af593 100644
--- i/app/api/graphql/generated/graph/schema_gen.go
+++ w/app/api/graphql/generated/graph/schema_gen.go
@@ -[23](https://github.com/orijinplus/backend/actions/runs/12228380133/job/34106552653#step:5:24)6836,12 +236836,12 @@ func (ec *executionContext) marshalNAnalyticsResult2ᚖorijinplusᚋappᚋapiᚋ
 	return ec._AnalyticsResult(ctx, sel, v)
 }
 
-func (ec *executionContext) unmarshalNAny2interface(ctx context.Context, v interface{}) (any, error) {
+func (ec *executionContext) unmarshalNAny2interface(ctx context.Context, v interface{}) (interface{}, error) {
 	res, err := graphql.UnmarshalAny(v)
 	return res, graphql.ErrorOnPath(ctx, err)
 }
 
-func (ec *executionContext) marshalNAny2interface(ctx context.Context, sel ast.SelectionSet, v any) graphql.Marshaler {
+func (ec *executionContext) marshalNAny2interface(ctx context.Context, sel ast.SelectionSet, v interface{}) graphql.Marshaler {
 	if v == nil {
 		if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
 			ec.Errorf(ctx, "the requested element is null which the schema does not allow")
no changes added to commit (use "git add" and/or "git commit -a")
Error: Process completed with exit code 1.

So I pushed exactly that change and nothing else:
image

And ran the test again but got the exact opposite from gqlgen:

Error: The generated graphql resolvers do not match with those in the branch!
On branch gqlgen-test
Your branch is up to date with 'origin/gqlgen-test'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   app/api/graphql/generated/graph/schema_gen.go

--------------------------------------------------
Changes not staged for commit:
diff --git i/app/api/graphql/generated/graph/schema_gen.go w/app/api/graphql/generated/graph/schema_gen.go
index 50af593..f9ceeff 100644
--- i/app/api/graphql/generated/graph/schema_gen.go
+++ w/app/api/graphql/generated/graph/schema_gen.go
@@ -[23](https://github.com/orijinplus/backend/actions/runs/12228411305/job/34106627491#step:5:24)6836,12 +236836,12 @@ func (ec *executionContext) marshalNAnalyticsResult2ᚖorijinplusᚋappᚋapiᚋ
 	return ec._AnalyticsResult(ctx, sel, v)
 }
 
-func (ec *executionContext) unmarshalNAny2interface(ctx context.Context, v interface{}) (interface{}, error) {
+func (ec *executionContext) unmarshalNAny2interface(ctx context.Context, v interface{}) (any, error) {
 	res, err := graphql.UnmarshalAny(v)
 	return res, graphql.ErrorOnPath(ctx, err)
 }
 
-func (ec *executionContext) marshalNAny2interface(ctx context.Context, sel ast.SelectionSet, v interface{}) graphql.Marshaler {
+func (ec *executionContext) marshalNAny2interface(ctx context.Context, sel ast.SelectionSet, v any) graphql.Marshaler {
 	if v == nil {
 		if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
 			ec.Errorf(ctx, "the requested element is null which the schema does not allow")
no changes added to commit (use "git add" and/or "git commit -a")
Error: Process completed with exit code 1.

What did you expect?

There should be no diff in the generated files if no changes are made to the source files they're generated from.

versions

  • go run github.com/99designs/gqlgen version 0.17.53
  • go version 1.23.1
@StevenACoffman
Copy link
Collaborator

Ok, that is definitely not the intention. If you upgrade to gqlgen v0.17.58 does the problem persist? I seem to recall some changes since then that might already have fixed this issue.

@evellior
Copy link
Author

That may have worked, but the test has always sometimes worked.
image

Leave this issue for now and I'll comment again to let you know if that list starts looking consistent. I've enabled it to run on all PRs so it shouldn't take long to get a good sample of tests.

@evellior
Copy link
Author

Sorry to say, but the issue seems to still be present in v0.17.60

The issue is easily reproduced by both myself and others on my team. All you should have to do is run the same commands as that github action (delete the generated files and running go get -d github.com/99designs/gqlgen@v0.17.60 to generate them again).

@evellior
Copy link
Author

evellior commented Dec 17, 2024

From the action above updated to:

name: GQLGen Test

on:
  workflow_call: # Allows this to be executed by other workflows
  workflow_dispatch: # Allows this to be manually executed on any branch
  pull_request:
    branches: [ development ]

jobs:
  test:
    name: Generate graph
    runs-on: ubuntu-latest
    steps:

    - uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: 1.23.1

    - name: Run test gqlgen
      run: |
        go get -d github.com/99designs/gqlgen@v0.17.60
        cd app/api/graphql; go run github.com/99designs/gqlgen

    - name: Check file diff
      run: |
        if [ -n "$(git status --porcelain)" ]; then 
          echo "::error::The generated graphql resolvers do not match with those in the branch!"
          git status -v -v
          exit 1
        fi

Pull request opened and action failed
image

Committed this to branch
image

Action failed again with opposite diff
image

@StevenACoffman
Copy link
Collaborator

So I merged #3436 which may help with this, but I then tried to change all the interface{} to any in #3438 but there seems to be a small problem with the modelgen over there.

I was also wondering if there was something that is running gofmt -w -r 'interface{} -> any' on a subset of the files you are seeing. For instance, if a generated directory is excluded from being linted, but only some of your generated files are not in a directory with that name and are still being linted, it would seem sort of random which ones had this problem.

Very puzzling. I'll try to work on #3438 after day job and maybe that will just prevent it altogether. If you (or your folks) get a chance to take a look at #3438 that might speed things up. Thanks!

@StevenACoffman
Copy link
Collaborator

StevenACoffman commented Dec 20, 2024

@evellior Hey, can you try the new gqlgen v0.17.61 and see if #3436 and #3438 fixed it to be consistent for you?

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

No branches or pull requests

2 participants