heftapp / graphql_codegen

MIT License
142 stars 52 forks source link

Deserialization error in schema with nested unions/fragments #168

Closed mwawrusch closed 2 years ago

mwawrusch commented 2 years ago

I have this schema:


union WalletContentBlock = WalletContentBlockList

type WalletContentBlockList {
  id: ID!
  blockType: WalletContentBlockType!
  caption: String
  items: [WalletContentItem!]
}

type WalletContentBlockType {
  id: ID!
}

union WalletContentItem = WalletContentItemContentPreview

type WalletContentItemContentPreview {
  id: ID!
  caption: String
  imageURL: String
  link: String
}

input WalletGetContentInput {
  id: ID
}

type WalletGetContentPayload {
  blocks: [WalletContentBlock!]
}

type Query {
  walletGetContent(input: WalletGetContentInput!): WalletGetContentPayload!
}

with this sample payload

{
  "data": {
    "walletGetContent": {
      "blocks": [
        {
          "id": "1",
          "blockType": {
            "id": "horizontal-scrolling-list"
          },
          "caption": "Featured",
          "items": [
            {
              "id": "1",
              "caption": "Remarkable Women #4740",
              "imageURL": "https://lh3.googleusercontent.com/1kXncBpOzPjZAC46GDNXvW4NL0zDv1e_E6vzsTtKEAOAvb0SwbWzwljDY5hulMqRA4zQytjYjV7B-a_jYtNG6zy9dKssov9RRRLu5ss=w600",
              "link": "https://opensea.io/assets/ethereum/0x3e69baab7a742c83499661c5db92386b2424df11/4740"
            },
            {
              "id": "2",
              "caption": "Inftspaces",
              "imageURL": "https://lh3.googleusercontent.com/8h6WQ4_Kxn0c6rBujSe0flZMTSKiWGWiMOSNaSTPUchakVq4TmyBOWJi4E0GU3_5b7v4jF-vIPR24uziltXMfQHgzEj3zBCkW_Tbsw=s0",
              "link": "https://opensea.io/assets/ethereum/0xb11288c7237765db4782dfddcc49e31bb80132b8/90"
            },
            {
              "id": "3",
              "caption": "Love on Mars",
              "imageURL": "https://lh3.googleusercontent.com/XoOJwf3DQ6ZWMpmmLkVqfZD3AqEM3G0xQCXNur0-u5-HAHpoMHyPJQL__5H0wpJsy2cafaaCSSxI5YPjnzCZ-4ME7hnJXrgd5292nA=s0",
              "link": "https://opensea.io/collection/love-on-mars-by-ruben-rojas-x-omgdrops"
            }
          ]
        },
        {
          "id": "2",
          "blockType": {
            "id": "vertical-short-list"
          },
          "caption": "New",
          "items": [
            {
              "id": "1",
              "caption": "MSO LAB Genesis #844",
              "imageURL": "https://lh3.googleusercontent.com/FCcSKSsYaNk9-bzks7ZWIEjFPiL8xrtb8aYnO2DG0piF9Eja1AkY55Yan-KQqtpcwISdpTWNz_W0yrmmmQM2hAHNG_AIXl7E4ATG2Q=w600",
              "link": "https://opensea.io/assets/ethereum/0xc2ac394984f3850027dac95fe8a62e446c5fb786/844"
            },
            {
              "id": "2",
              "caption": "The Outsiders Genesis",
              "imageURL": "https://lh3.googleusercontent.com/AM8EkaQl32uR8N1RIiJa5ogVG1gh2faX6u4a1TAa_NhsGkA2CRG0pFfMOOxcc09q2KFq5cQNljTFkDbbScWpUT70es83Di0RRWD7fjM=s0",
              "link": "https://opensea.io/collection/the-outsiders-genesis"
            },
            {
              "id": "3",
              "caption": "Bored Ape Yacht Club #6386",
              "imageURL": "https://img.seadn.io/files/968a4be8ada4a55b59de561d15522b4e.png?fit=max&w=600",
              "link": "https://opensea.io/assets/ethereum/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/6386"
            }
          ]
        }
      ]
    }
  }
}

When deserialized in flutter the items from the second block are copied over the items of the first block.

query WalletGetContent($input: WalletGetContentInput!) {
  walletGetContent(input: $input) {
    blocks {
      ... on WalletContentBlockList {
        id
        blockType {
          id
        }
        caption
        items {
          ... on WalletContentItemContentPreview {
            id
            caption
            imageURL
            link
          }
        }
      }
    }
  }
}
mwawrusch commented 2 years ago

Using: graphql_codegen: 0.10.3 json_serializable: 6.3.1

mwawrusch commented 2 years ago

Output in flutter (please ignore the red markers)

{
   "blocks":[
      {
         "__typename":"WalletContentBlockList",
         "id":1,
         "blockType":{
            "id":"horizontal-scrolling-list",
            "__typename":"WalletContentBlockType"
         },
         "caption":"Featured",
         "items":[
            {
               "__typename":"WalletContentItemContentPreview",
               "id":1,
               "caption":MSO LAB Genesis #844,
               "imageURL":"https"://lh3.googleusercontent.com/FCcSKSsYaNk9-bzks7ZWIEjFPiL8xrtb8aYnO2DG0piF9Eja1AkY55Yan-KQqtpcwISdpTWNz_W0yrmmmQM2hAHNG_AIXl7E4ATG2Q=w600,
               "link":"https"://opensea.io/assets/ethereum/0xc2ac394984f3850027dac95fe8a62e446c5fb786/844
            },
            {
               "__typename":"WalletContentItemContentPreview",
               "id":2,
               "caption":"The Outsiders Genesis",
               "imageURL":"https"://lh3.googleusercontent.com/AM8EkaQl32uR8N1RIiJa5ogVG1gh2faX6u4a1TAa_NhsGkA2CRG0pFfMOOxcc09q2KFq5cQNljTFkDbbScWpUT70es83Di0RRWD7fjM=s0,
               "link":"https":
            },
            {
               "__typename":"WalletContentItemContentPreview",
               "id":3,
               "caption":Bored Ape Yacht Club #6386,
               "imageURL":"https"://img.seadn.io/files/968a4be8ada4a55b59de561d15522b4e.png?fit=max&w=600,
               "link":"https"://opensea.io/assets/ethereum/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/6386
            }
         ]
      },
      {
         "__typename":"WalletContentBlockList",
         "id":2,
         "blockType":{
            "id":"vertical-short-list",
            "__typename":"WalletContentBlockType"
         },
         "caption":"New",
         "items":[
            {
               "__typename":"WalletContentItemContentPreview",
               "id":1,
               "caption":MSO LAB Genesis #844,
               "imageURL":"https"://lh3.googleusercontent.com/FCcSKSsYaNk9-bzks7ZWIEjFPiL8xrtb8aYnO2DG0piF9Eja1AkY55Yan-KQqtpcwISdpTWNz_W0yrmmmQM2hAHNG_AIXl7E4ATG2Q=w600,
               "link":"https"://opensea.io/assets/ethereum/0xc2ac394984f3850027dac95fe8a62e446c5fb786/844
            },
            {
               "__typename":"WalletContentItemContentPreview",
               "id":2,
               "caption":"The Outsiders Genesis",
               "imageURL":"https"://lh3.googleusercontent.com/AM8EkaQl32uR8N1RIiJa5ogVG1gh2faX6u4a1TAa_NhsGkA2CRG0pFfMOOxcc09q2KFq5cQNljTFkDbbScWpUT70es83Di0RRWD7fjM=s0,
               "link":"https":
            },
            {
               "__typename":"WalletContentItemContentPreview",
               "id":3,
               "caption":Bored Ape Yacht Club #6386,
               "imageURL":"https"://img.seadn.io/files/968a4be8ada4a55b59de561d15522b4e.png?fit=max&w=600,
               "link":"https"://opensea.io/assets/ethereum/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/6386
            }
         ]
      }
   ],
   "__typename":"WalletGetContentPayload"
}
budde377 commented 2 years ago

Thanks for reaching out with your problem. I've tried reproducing your issue here: https://github.com/heftapp/graphql_codegen/pull/169/files#diff-1069441418f33b6bf26dc7fbc305f8f1e06bfaf5b9494626cd4339684f8f95b3R89-R95 and as you can see the test passes which means that the data is identical after being de-serialzied and serialized again. This library is not copying stuff over. Instead, you should have a look at your client and the client cache setup.

mwawrusch commented 2 years ago

Thx, seems that the problem happens before deserialization probably GraphQLClient (response.data contains the wrong values already).

mwawrusch commented 2 years ago

Reposted it here: https://github.com/zino-hofmann/graphql-flutter/issues/1208