Skip to content

__typename not merged #369

Closed
Closed
@qneyrat

Description

@qneyrat

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

Activity

pavelnikolov

pavelnikolov commented on May 1, 2020

@pavelnikolov
Member

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.

pavelnikolov

pavelnikolov commented on May 1, 2020

@pavelnikolov
Member

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

pavelnikolov

pavelnikolov commented on May 1, 2020

@pavelnikolov
Member

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"
					}
				}
			`,
		},
	})
}
pavelnikolov

pavelnikolov commented on May 1, 2020

@pavelnikolov
Member

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 🤦

qneyrat

qneyrat commented on May 2, 2020

@qneyrat
Author

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

pavelnikolov

pavelnikolov commented on Mar 22, 2021

@pavelnikolov
Member

@qneyrat any luck with this?

qneyrat

qneyrat commented on Mar 23, 2021

@qneyrat
Author

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

Before execFieldSelection

&{{{name [] String! [] The name of the character} Character 4 [] false false <nil> 0x187f1a0 GraphQL field: Character.name} name map[] <invalid Value> [] false <invalid Value>}
&{{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 <nil> 0x187f1a0 GraphQL field: Droid.name} name map[] <invalid Value> [] false <invalid Value>}
&{{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
pavelnikolov

pavelnikolov commented on Mar 24, 2021

@pavelnikolov
Member

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?

added a commit that references this issue on Mar 29, 2021
073f4a0
eko

eko commented on Mar 29, 2021

@eko
Contributor

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.

added a commit that references this issue on Mar 29, 2021
a10b2d3

6 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @eko@pavelnikolov@qneyrat

        Issue actions

          __typename not merged · Issue #369 · graph-gophers/graphql-go