99designs/gqlgen

Input converted to a list when it is not

オープン

#2,582 opened on 2023/03/11

 (4 件のコメント) (0 件のリアクション) (0 人の担当者)Go (1,250 件のフォーク)auto 404
help wanted

Repository metrics

Stars
 (10,737 個のスター)
PR merge metrics
 (PR metrics pending)

説明

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

コントリビューターガイド