Skip to content

Commit

Permalink
Tt 2799 headers per subgraph (TykTechnologies#3706)
Browse files Browse the repository at this point in the history
* replace in memory server implementation with http.ServerMux and in memory listener

* TT-2799 headers per subgraph

* remove redudant module

* override global headers

Co-authored-by: Sergey Petrunin <[email protected]>
  • Loading branch information
Urban Ishimwe and devsergiy authored Nov 3, 2021
1 parent 31562db commit c6e8773
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
20 changes: 18 additions & 2 deletions apidef/adapter/graphql_config_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ func (g *GraphQLConfigAdapter) subgraphDataSourceConfigs() []graphqlDataSource.C
if len(apiDefSubgraphConf.SDL) == 0 {
continue
}

conf := g.graphqlDataSourceConfiguration(apiDefSubgraphConf.URL, http.MethodPost, g.apiDefinition.GraphQL.Supergraph.GlobalHeaders)
hdr := g.removeDuplicateHeaders(apiDefSubgraphConf.Headers, g.apiDefinition.GraphQL.Supergraph.GlobalHeaders)
conf := g.graphqlDataSourceConfiguration(apiDefSubgraphConf.URL, http.MethodPost, hdr)
conf.Federation = graphqlDataSource.FederationConfiguration{
Enabled: true,
ServiceSDL: apiDefSubgraphConf.SDL,
Expand Down Expand Up @@ -347,6 +347,22 @@ func (g *GraphQLConfigAdapter) convertHeadersToHttpHeaders(apiDefHeaders map[str
return engineV2Headers
}

func (g *GraphQLConfigAdapter) removeDuplicateHeaders(headers ...map[string]string) map[string]string {
hdr := make(map[string]string)
// headers priority depends on the order of arguments
for _, header := range headers {
for k, v := range header {
keyCanonical := http.CanonicalHeaderKey(k)
if _, ok := hdr[keyCanonical]; ok {
// skip because header is present
continue
}
hdr[keyCanonical] = v
}
}
return hdr
}

func (g *GraphQLConfigAdapter) determineChildNodes(planDataSources []plan.DataSourceConfiguration) error {
for i := range planDataSources {
for j := range planDataSources[i].RootNodes {
Expand Down
19 changes: 15 additions & 4 deletions apidef/adapter/graphql_config_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ func TestGraphQLConfigAdapter_supergraphDataSourceConfigs(t *testing.T) {
URL: "http://accounts.service",
Method: http.MethodPost,
Header: http.Header{
"Header1": []string{"value1"},
"Header1": []string{"override_global"},
"Header2": []string{"value2"},
"X-Tyk-Internal": []string{"true"},
"Auth": []string{"appended_header"},
},
},
Subscription: graphqlDataSource.SubscriptionConfiguration{
Expand Down Expand Up @@ -277,7 +278,8 @@ func TestGraphQLConfigAdapter_supergraphDataSourceConfigs(t *testing.T) {
URL: "http://reviews.service",
Method: http.MethodPost,
Header: http.Header{
"Header1": []string{"value1"},
"Header1": []string{"override_global"},
"Auth": []string{"appended_header"},
"Header2": []string{"value2"},
},
},
Expand Down Expand Up @@ -647,7 +649,11 @@ var graphqlEngineV2SupergraphConfigJson = `{
{
"api_id": "",
"url": "tyk://accounts.service",
"sdl": ` + strconv.Quote(federationAccountsServiceSDL) + `
"sdl": ` + strconv.Quote(federationAccountsServiceSDL) + `,
"headers": {
"header1": "override_global",
"Auth": "appended_header"
}
},
{
"api_id": "",
Expand All @@ -662,7 +668,12 @@ var graphqlEngineV2SupergraphConfigJson = `{
{
"api_id": "",
"url": "http://reviews.service",
"sdl": ` + strconv.Quote(federationReviewsServiceSDL) + `
"sdl": ` + strconv.Quote(federationReviewsServiceSDL) + `,
"headers": {
"header1": "override_global",
"header2": "value2",
"Auth": "appended_header"
}
}
],
"global_headers": {
Expand Down
9 changes: 5 additions & 4 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,11 @@ type GraphQLSupergraphConfig struct {
}

type GraphQLSubgraphEntity struct {
APIID string `bson:"api_id" json:"api_id"`
Name string `bson:"name" json:"name"`
URL string `bson:"url" json:"url"`
SDL string `bson:"sdl" json:"sdl"`
APIID string `bson:"api_id" json:"api_id"`
Name string `bson:"name" json:"name"`
URL string `bson:"url" json:"url"`
SDL string `bson:"sdl" json:"sdl"`
Headers map[string]string `bson:"headers" json:"headers"`
}

type GraphQLEngineConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions apidef/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ const Schema = `{
},
"sdl": {
"type": "string"
},
"headers": {
"type": ["object", "null"]
}
}
},
Expand Down

0 comments on commit c6e8773

Please sign in to comment.