Repository: graph-gophers/graphql-go (go)

Issue:
__typename not merged
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 

Pull request description:
Fixed duplicated __typename in response (fixes #369)
This pull request fixes issue reported in #369

Patch:
diff --git a/internal/exec/exec.go b/internal/exec/exec.go
index 1e409bb..7b9895e 100644
--- a/internal/exec/exec.go
+++ b/internal/exec/exec.go
@@ -129,12 +129,17 @@ func collectFieldsToResolve(sels []selected.Selection, s *resolvable.Schema, res
 			field.sels = append(field.sels, sel.Sels...)
 
 		case *selected.TypenameField:
-			sf := &selected.SchemaField{
-				Field:       s.Meta.FieldTypename,
-				Alias:       sel.Alias,
-				FixedResult: reflect.ValueOf(typeOf(sel, resolver)),
+			_, ok := fieldByAlias[sel.Alias]
+			if !ok {
+				sf := &selected.SchemaField{
+					Field:       s.Meta.FieldTypename,
+					Alias:       sel.Alias,
+					FixedResult: reflect.ValueOf(typeOf(sel, resolver)),
+				}
+				field := &fieldToExec{field: sf, resolver: resolver}
+				*fields = append(*fields, field)
+				fieldByAlias[sel.Alias] = field
 			}
-			*fields = append(*fields, &fieldToExec{field: sf, resolver: resolver})
 
 		case *selected.TypeAssertion:
 			out := resolver.Method(sel.MethodIndex).Call(nil)


Test patch:
diff --git a/gqltesting/testing.go b/gqltesting/testing.go
index b1544fd..f8b6c9d 100644
--- a/gqltesting/testing.go
+++ b/gqltesting/testing.go
@@ -23,6 +23,7 @@ type Test struct {
 	Variables      map[string]interface{}
 	ExpectedResult string
 	ExpectedErrors []*errors.QueryError
+	RawResponse    bool
 }
 
 // RunTests runs the given GraphQL test cases as subtests.
@@ -57,10 +58,22 @@ func RunTest(t *testing.T, test *Test) {
 	}
 
 	// Verify JSON to avoid red herring errors.
-	got, err := formatJSON(result.Data)
-	if err != nil {
-		t.Fatalf("got: invalid JSON: %s", err)
+	var got []byte
+
+	if test.RawResponse {
+		value, err := result.Data.MarshalJSON()
+		if err != nil {
+			t.Fatalf("got: unable to marshal JSON response: %s", err)
+		}
+		got = value
+	} else {
+		value, err := formatJSON(result.Data)
+		if err != nil {
+			t.Fatalf("got: invalid JSON: %s", err)
+		}
+		got = value
 	}
+
 	want, err := formatJSON([]byte(test.ExpectedResult))
 	if err != nil {
 		t.Fatalf("want: invalid JSON: %s", err)
diff --git a/graphql_test.go b/graphql_test.go
index 901d511..98bedce 100644
--- a/graphql_test.go
+++ b/graphql_test.go
@@ -1836,6 +1836,30 @@ func TestTypeName(t *testing.T) {
 				}
 			`,
 		},
+
+		{
+			Schema: starwarsSchema,
+			Query: `
+				{
+					hero {
+						__typename
+						name
+						... on Character {
+							...Droid
+							name
+							__typename
+						}
+					}
+				}
+				
+				fragment Droid on Droid {
+					name
+					__typename
+				}			  
+			`,
+			RawResponse:    true,
+			ExpectedResult: `{"hero":{"__typename":"Droid","name":"R2-D2"}}`,
+		},
 	})
 }