Graphql-go: __typename not merged

Created on 19 Nov 2019  路  9Comments  路  Source: graph-gophers/graphql-go

When i start server with go run example/starwars/server/server.go and execute this query :

query TestMerge {
  hero {
    __typename
    name

    ... on Character {
      ...Droid
      name
      __typename
    }

  }
}

fragment Droid on Droid {
  name
  __typename
}

GraphiQL merge __typename field but the server response has multiple __typename.
Result with server response (browser network inspect) :

{"data":{"hero":{"__typename":"Droid","name":"R2-D2","__typename":"Droid","__typename":"Droid"}}}

Expected :

{"data":{"hero":{"name":"R2-D2","__typename":"Droid"}}}

With graphql-js __typename is merged.

link #365

help wanted

All 9 comments

Hi, I just ran the following test:

  1. I checked out latest version of this library.
  2. Then, I ran go run example/starwars/server/server.go
  3. I rand the query you provided:
query TestMerge {
  hero {
    __typename
    name

    ... on Character {
      ...Droid
      name
      __typename
    }

  }
}

fragment Droid on Droid {
  name
  __typename
}
  1. I got the expected response:
    Screen Shot 2020-05-01 at 10 59 02 am

I am closing this issue because I am unable to reproduce it in the latest version of the library.

Oh, I just read more carefully. Indeed, the GraphiQL UI is merging this and is hiding the real problem. I am reopening the issue.
Screen Shot 2020-05-01 at 11 03 20 am

Apparently there is an issue with our unit tests too. I just wrote this unit test and it passes:

func TestMergeTypename(t *testing.T) {
    gqltesting.RunTests(t, []*gqltesting.Test{
        {
            Schema: starwarsSchema,
            Query: `
                query TestMerge {
                    hero {
                        __typename
                        name

                        ... on Character {
                            ...Droid
                            name
                            __typename
                        }
                    }
                }

                fragment Droid on Droid {
                    name
                    __typename
                }
            `,
            ExpectedResult: `
                {
                    "hero": {
                        "name": "R2-D2",
                        "__typename": "Droid"
                    }
                }
            `,
        },
    })
}

In our testing.go file we have this function:

func formatJSON(data []byte) ([]byte, error) {
    var v interface{}
    if err := json.Unmarshal(data, &v); err != nil {
        return nil, err
    }
    formatted, err := json.Marshal(v)
    if err != nil {
        return nil, err
    }
    return formatted, nil
}

This function hides the real issue during tests 馃う

Oh! yes json.Unmarshal merge same keys 馃
Any idea to fix that? I'll investigate.

@qneyrat any luck with this?

I dont know why field is merged but typename no.
Typename and name has different level in execFieldSelection but field name merged.

Before execFieldSelection
```&{{Character map[appearsIn:0xc000150a80 friends:0xc000150240 friendsConnection:0xc000150300 id:0xc0001500c0 name:0xc000150180] map[Droid:0xc00015e760 Human:0xc00015e020]} __typename}
&{{{name [] String! [] The name of the character} Character 4 [] false false 0x187f1a0 GraphQL field: Character.name} name map[] [] false }
&{{Character map[appearsIn:0xc000150a80 friends:0xc000150240 friendsConnection:0xc000150300 id:0xc0001500c0 name:0xc000150180] map[Droid:0xc00015e760 Human:0xc00015e020]} __typename}
&{{5 0xc00015eb60} [0xc0001063c0 0xc0001b6090]}
&{{{name [] String! [] What others call this droid} Droid 4 [] false false 0x187f1a0 GraphQL field: Droid.name} name map[] [] false }
&{{Droid map[appearsIn:0xc000151c80 friends:0xc000151800 friendsConnection:0xc0001518c0 id:0xc000151680 name:0xc000151740 primaryFunction:0xc000151d40] map[]} __typename}


After exec (alias of field

0 __typename
1 name
2 __typename
3 __typename
0 hero
```

I haven't had the time to investigate but __typename is supposed to be also a key in the map. Could it be stored with different keys?

Hi,

I just proposed a fix, all tests seems to be still OK and I added a new test to ensure this issue is fixed :-)

Please let me know if you see something I should change because I don't know the code of this library.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JitinGuglani picture JitinGuglani  路  6Comments

MrMatten picture MrMatten  路  6Comments

Hagbarth picture Hagbarth  路  6Comments

jakubdal picture jakubdal  路  6Comments

0xTanvir picture 0xTanvir  路  5Comments