简体   繁体   中英

GraphQL Interface With GqlGen

I'm trying to implement the GraphQL Interface by using Go GqlGen but it's not working. I'm getting following error:

  "errors": [
    {
      "message": "json: cannot unmarshal object into Go struct field Segment.segments.contents of type models.Content",
      "path": [
        "GetData"
      ]
    }
  ],
  "data": {
    "GetData": null
  }
}

graphql schema:

type Root {
  id: String
  title: String
  segments: [Segment]
}

type Segment {
  id: String
  contents: [Content]
}

type Team {
  name: String
}

type A implements Content {
  id: String
  title: String
  teams: [Team]
}

type B implements Content {
  id: String
  title: String
  score: Int
}

interface Content {
  id: String
  title: String
}

type Query {
  GetData: Root
}

Here is complete code: Link

versions

  • gqlgen v0.17.20
  • go version go1.19 darwin/amd64

Thanks for the help!

It's because of your mock data structure is not match with your defined structs.

https://github.com/shashank-sachan/graphql-interface-go/blob/3e76667fae8f35cb63eee5a84baedaa2b93b12d8/resolvers/root.resolvers.go#L17

Your mock data get both A and B data inside contents array. But their structure is different(teams vs score)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM