99designs/gqlgen

Input converted to a list when it is not

Open

#2,582 opened on Mar 11, 2023

View on GitHub
 (4 comments) (0 reactions) (0 assignees)Go (1,250 forks)auto 404
help wanted

Repository metrics

Stars
 (10,737 stars)
PR merge metrics
 (PR metrics pending)

Description

What happened?

Hello all. I have a schema describing github repository, and each repository has a field admins, which is of type [Account]. [Account] in my case is an interface, thus admins is a list of interfaces:

The respective graphql (excerpt) is as follows:

  interface Account {
   ...
  }
  
  type GithubUserAccount implements Account {
    # CID is github id of form "https://github.com/[handle]"
    cid: ID!
    name: String
  }

  type GithubRepository {
    cid: ID! 
    ...
    admins: [Account!] 
  }

and the generated resolvers are

type Account interface {
	IsAccount()
	...
}

type AccountCreateInput struct {
	// Cid acts as ID
	Cid                 *string                         `json:"cid,omitempty"`
	GithubUserAccount   *GithubUserAccountCreateInput   `json:"GithubUserAccount,omitempty"`
	...
}

type GithubRepositoryCreateInput struct {
	Cid               string                         `json:"cid"`
	...
	Admins            []*AccountCreateInput          `json:"admins"
}

Now, when I try to do an insertion via the following query:

mutation testMutation($input: GithubRepositoryCreateInput!) {
  createGithubRepository(input: $input) {
  ...
  }
}

with vars:

  "input": {
    "cid": "testrepo",
    "admins": {
      "GithubUserAccount": {
        "cid": "alice",
        "name": "alice"
      },
      "GithubUserAccount": {
        "cid": "bob",
        "name": "bob"
      },
      "GithubUserAccount": {
        "cid": "carol",
        "name": "carol"
      }
    }
  }
}

Only Carol is created as an account, and the input is implicitly converted to a list:

model.GithubRepositoryCreateInput {testrepo <nil> <nil> <nil> testurl <nil> 0x400039a140 [] [] [] [] [] [0x400006df80] [] [] <nil> <nil> <nil> [] <nil> [] [] [] <nil> <nil> <nil> <nil>}

(admins are the field [0x400006df80] corresponding to a []*model.AccountCreateInput)

If I pass a correct input as below, three users are created:

{
  "input": {
    "cid": "testrepo",
    "admins": [
      {
        "GithubUserAccount": {
          "cid": "alice",
          "name": "alice"
        }
      },
      {
        "GithubUserAccount": {
          "cid": "bob",
          "name": "bob"
        }
      },
      {
        "GithubUserAccount": {
          "cid": "carol",
          "name": "carol"
        }
      }
    ]
  }
}

What did you expect?

I would expect a GraphQL validation error.

versions

  • go run github.com/99designs/gqlgen version: v0.17.22
  • go version: go1.19.7 linux/arm64

Contributor guide