github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/introspection_test.go (about) 1 package followschema 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/operandinc/gqlgen/client" 8 "github.com/operandinc/gqlgen/graphql" 9 "github.com/operandinc/gqlgen/graphql/handler" 10 "github.com/operandinc/gqlgen/graphql/handler/transport" 11 "github.com/operandinc/gqlgen/graphql/introspection" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestIntrospection(t *testing.T) { 16 t.Run("disabled when creating your own server", func(t *testing.T) { 17 resolvers := &Stub{} 18 19 srv := handler.New(NewExecutableSchema(Config{Resolvers: resolvers})) 20 srv.AddTransport(transport.POST{}) 21 c := client.New(srv) 22 23 var resp interface{} 24 err := c.Post(introspection.Query, &resp) 25 require.EqualError(t, err, "[{\"message\":\"introspection disabled\",\"path\":[\"__schema\"]}]") 26 }) 27 28 t.Run("enabled by default", func(t *testing.T) { 29 resolvers := &Stub{} 30 31 c := client.New(handler.NewDefaultServer( 32 NewExecutableSchema(Config{Resolvers: resolvers}), 33 )) 34 35 var resp interface{} 36 err := c.Post(introspection.Query, &resp) 37 require.NoError(t, err) 38 39 t.Run("does not return empty deprecation strings", func(t *testing.T) { 40 q := `{ 41 __type(name:"InnerObject") { 42 fields { 43 name 44 deprecationReason 45 } 46 } 47 }` 48 49 var resp struct { 50 Type struct { 51 Fields []struct { 52 Name string 53 DeprecationReason *string 54 } 55 } `json:"__type"` 56 } 57 err := c.Post(q, &resp) 58 require.NoError(t, err) 59 60 require.Equal(t, "id", resp.Type.Fields[0].Name) 61 require.Nil(t, resp.Type.Fields[0].DeprecationReason) 62 }) 63 }) 64 65 t.Run("disabled by middleware", func(t *testing.T) { 66 resolvers := &Stub{} 67 68 srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})) 69 srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler { 70 graphql.GetOperationContext(ctx).DisableIntrospection = true 71 return next(ctx) 72 }) 73 c := client.New(srv) 74 75 var resp interface{} 76 err := c.Post(introspection.Query, &resp) 77 require.EqualError(t, err, "[{\"message\":\"introspection disabled\",\"path\":[\"__schema\"]}]") 78 }) 79 }