@@ -754,6 +754,7 @@ type User {
754
754
` , resp .Body .String ())
755
755
}
756
756
757
+ // TestDataAndErrorsBothReturnFromOneServicePartialSuccess verifies fix for https://github.com/nautilus/gateway/issues/212
757
758
func TestDataAndErrorsBothReturnFromOneServicePartialSuccess (t * testing.T ) {
758
759
t .Parallel ()
759
760
schema , err := graphql .LoadSchema (`
@@ -801,3 +802,157 @@ type Query {
801
802
}
802
803
` , resp .Body .String ())
803
804
}
805
+
806
+ // TestGatewayRunsResponseMiddlewaresOnError verifies part of fix for https://github.com/nautilus/gateway/issues/212
807
+ // The issue included the 'id' field not getting scrubbed when an error was returned, and scrubbing is a builtin response middleware.
808
+ func TestGatewayRunsResponseMiddlewaresOnError (t * testing.T ) {
809
+ t .Parallel ()
810
+ schema , err := graphql .LoadSchema (`
811
+ type Query {
812
+ foo: String
813
+ }
814
+ ` )
815
+ require .NoError (t , err )
816
+ queryerFactory := QueryerFactory (func (ctx * PlanningContext , url string ) graphql.Queryer {
817
+ return graphql .QueryerFunc (func (input * graphql.QueryInput ) (interface {}, error ) {
818
+ return map [string ]interface {}{
819
+ "foo" : nil ,
820
+ }, graphql.ErrorList {
821
+ & graphql.Error {
822
+ Message : "foo is broken" ,
823
+ Path : []interface {}{"foo" },
824
+ },
825
+ }
826
+ })
827
+ })
828
+ executedResponseMiddleware := false
829
+ gateway , err := New ([]* graphql.RemoteSchema {
830
+ {Schema : schema , URL : "boo" },
831
+ }, WithQueryerFactory (& queryerFactory ), WithMiddlewares (ResponseMiddleware (func (* ExecutionContext , map [string ]interface {}) error {
832
+ executedResponseMiddleware = true
833
+ return nil
834
+ })))
835
+ require .NoError (t , err )
836
+
837
+ req := httptest .NewRequest (http .MethodPost , "/" , strings .NewReader (`{"query": "query { foo }"}` ))
838
+ resp := httptest .NewRecorder ()
839
+ gateway .GraphQLHandler (resp , req )
840
+ assert .Equal (t , http .StatusOK , resp .Code )
841
+ assert .JSONEq (t , `
842
+ {
843
+ "data": {
844
+ "foo": null
845
+ },
846
+ "errors": [
847
+ {
848
+ "message": "foo is broken",
849
+ "path": ["foo"],
850
+ "extensions": null
851
+ }
852
+ ]
853
+ }
854
+ ` , resp .Body .String ())
855
+ assert .True (t , executedResponseMiddleware , "All response middleware should run, even on responses with errors" )
856
+ }
857
+
858
+ // TestPartialSuccessAlsoResolvesValidNodeIDs verifies fix for https://github.com/nautilus/gateway/issues/214
859
+ func TestPartialSuccessAlsoResolvesValidNodeIDs (t * testing.T ) {
860
+ t .Parallel ()
861
+ schemaFoo , err := graphql .LoadSchema (`
862
+ type Query {
863
+ foo: Foo
864
+ }
865
+
866
+ type Foo {
867
+ bar: Bar
868
+ boo: String
869
+ }
870
+
871
+ interface Node {
872
+ id: ID!
873
+ }
874
+
875
+ type Bar implements Node {
876
+ id: ID!
877
+ }
878
+ ` )
879
+ require .NoError (t , err )
880
+ schemaBar , err := graphql .LoadSchema (`
881
+ type Query {
882
+ node(id: ID!): Node
883
+ }
884
+
885
+ interface Node {
886
+ id: ID!
887
+ }
888
+
889
+ type Bar implements Node {
890
+ id: ID!
891
+ baz: String
892
+ }
893
+ ` )
894
+ require .NoError (t , err )
895
+ const query = `
896
+ query {
897
+ foo {
898
+ bar {
899
+ baz
900
+ }
901
+ }
902
+ }
903
+ `
904
+ queryerFactory := QueryerFactory (func (ctx * PlanningContext , url string ) graphql.Queryer {
905
+ return graphql .QueryerFunc (func (input * graphql.QueryInput ) (interface {}, error ) {
906
+ t .Log ("Received request:" , input .Query )
907
+ if strings .Contains (input .Query , "node(" ) {
908
+ return map [string ]interface {}{
909
+ "node" : map [string ]interface {}{
910
+ "baz" : "biff" ,
911
+ },
912
+ }, nil
913
+ }
914
+ return map [string ]interface {}{
915
+ "foo" : map [string ]interface {}{
916
+ "bar" : map [string ]interface {}{
917
+ "id" : "bar-id" ,
918
+ },
919
+ "boo" : nil ,
920
+ },
921
+ }, graphql.ErrorList {
922
+ & graphql.Error {
923
+ Message : "boo is broken" ,
924
+ Path : []interface {}{"foo" , "boo" },
925
+ },
926
+ }
927
+ })
928
+ })
929
+ gateway , err := New ([]* graphql.RemoteSchema {
930
+ {Schema : schemaFoo , URL : "foo" },
931
+ {Schema : schemaBar , URL : "bar" },
932
+ }, WithQueryerFactory (& queryerFactory ))
933
+ require .NoError (t , err )
934
+
935
+ req := httptest .NewRequest (http .MethodPost , "/" , strings .NewReader (fmt .Sprintf (`{"query": %q}` , query )))
936
+ resp := httptest .NewRecorder ()
937
+ gateway .GraphQLHandler (resp , req )
938
+ assert .Equal (t , http .StatusOK , resp .Code )
939
+ assert .JSONEq (t , `
940
+ {
941
+ "data": {
942
+ "foo": {
943
+ "bar": {
944
+ "baz": "biff"
945
+ },
946
+ "boo": null
947
+ }
948
+ },
949
+ "errors": [
950
+ {
951
+ "message": "boo is broken",
952
+ "path": ["foo", "boo"],
953
+ "extensions": null
954
+ }
955
+ ]
956
+ }
957
+ ` , resp .Body .String ())
958
+ }
0 commit comments