99designs/gqlgen

Input converted to a list when it is not

Aperta

#2582 aperta il 11 mar 2023

 (4 commenti) (0 reazioni) (0 assegnatari)Go (1250 fork)auto 404
help wanted

Metriche repository

Star
 (10.737 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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

Guida contributor