diff --git a/Gopkg.lock b/Gopkg.lock index 0eb9b6f3931..ba7c4bd22be 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -118,6 +118,14 @@ revision = "792786c7400a136282c1664665ae0a8db921c6c2" version = "v1.0.0" +[[projects]] + digest = "1:b0c25f00bad20d783d259af2af8666969e2fc343fa0dc9efe52936bbd67fb758" + name = "github.com/rs/cors" + packages = ["."] + pruneopts = "UT" + revision = "9a47f48565a795472d43519dd49aac781f3034fb" + version = "v1.6.0" + [[projects]] branch = "master" digest = "1:7ca2584fa7da0520cd2d1136a10194fe5a5b220bdb215074ab6f7b5ad91115f4" @@ -175,6 +183,7 @@ ] pruneopts = "UT" revision = "e805d08bb209b1accdea76bd2327811858d81985" + version = "v1.0.0" [[projects]] branch = "master" @@ -239,6 +248,7 @@ "github.com/mitchellh/mapstructure", "github.com/opentracing/opentracing-go", "github.com/pkg/errors", + "github.com/rs/cors", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", "github.com/urfave/cli", diff --git a/Gopkg.toml b/Gopkg.toml index 494e8df5e56..7876142a7ea 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -14,7 +14,7 @@ required = ["github.com/vektah/dataloaden"] [[constraint]] name = "github.com/vektah/gqlparser" - revision = "^1.0.0" + version = "^1.0.0" [prune] go-tests = true @@ -23,3 +23,7 @@ required = ["github.com/vektah/dataloaden"] [[constraint]] name = "gopkg.in/yaml.v2" version = "2.2.1" + +[[constraint]] + name = "github.com/rs/cors" + version = "1.6.0" diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 4fb8f0746e5..3f97c620b14 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -2978,7 +2978,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -3005,15 +3005,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -3224,7 +3228,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -3251,15 +3255,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/codegen/testserver/generated_test.go b/codegen/testserver/generated_test.go index 94dd09c728f..704869a0813 100644 --- a/codegen/testserver/generated_test.go +++ b/codegen/testserver/generated_test.go @@ -222,6 +222,32 @@ func TestIntrospection(t *testing.T) { var resp interface{} err := c.Post(introspection.Query, &resp) require.NoError(t, err) + + t.Run("does not return empty deprecation strings", func(t *testing.T) { + q := `{ + __type(name:"InnerObject") { + fields { + name + deprecationReason + } + } + }` + + c := client.New(srv.URL) + var resp struct { + Type struct { + Fields []struct { + Name string + DeprecationReason *string + } + } `json:"__type"` + } + err := c.Post(q, &resp) + require.NoError(t, err) + + require.Equal(t, "id", resp.Type.Fields[0].Name) + require.Nil(t, resp.Type.Fields[0].DeprecationReason) + }) }) t.Run("disabled by middleware", func(t *testing.T) { @@ -244,6 +270,7 @@ func TestIntrospection(t *testing.T) { err := c.Post(introspection.Query, &resp) require.EqualError(t, err, "[{\"message\":\"introspection disabled\",\"path\":[\"__schema\"]}]") }) + } var _ graphql.Tracer = (*testTracer)(nil) diff --git a/example/chat/generated.go b/example/chat/generated.go index 34c769f7c9d..d1bb62f7d3d 100644 --- a/example/chat/generated.go +++ b/example/chat/generated.go @@ -1176,7 +1176,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1203,15 +1203,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1422,7 +1426,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1449,15 +1453,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/example/chat/package.json b/example/chat/package.json index 119c7ea2ed1..ec70c1f671b 100644 --- a/example/chat/package.json +++ b/example/chat/package.json @@ -3,14 +3,18 @@ "version": "0.1.0", "private": true, "dependencies": { - "apollo-cache-inmemory": "^1.1.9", - "apollo-client": "^2.2.5", - "graphql-tag": "^2.9.1", - "graphql": "^0.13.2", - "react": "^16.2.0", - "react-apollo": "^2.1.0-beta.2", - "react-dom": "^16.2.0", - "react-scripts": "1.1.1", + "apollo-cache-inmemory": "^1.3.11", + "apollo-client": "^2.4.7", + "apollo-link": "^1.2.4", + "apollo-link-http": "^1.5.7", + "apollo-link-ws": "^1.0.10", + "apollo-utilities": "^1.0.26", + "graphql": "^14.0.2", + "graphql-tag": "^2.10.0", + "react": "^16.6.3", + "react-apollo": "^2.3.1", + "react-dom": "^16.6.3", + "react-scripts": "^2.1.1", "subscriptions-transport-ws": "^0.9.5" }, "scripts": { @@ -18,5 +22,11 @@ "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" - } + }, + "browserslist": [ + ">0.2%", + "not dead", + "not ie <= 11", + "not op_mini all" + ] } diff --git a/example/chat/server/server.go b/example/chat/server/server.go index b3f313acd3e..0bcee178723 100644 --- a/example/chat/server/server.go +++ b/example/chat/server/server.go @@ -10,6 +10,7 @@ import ( "github.com/99designs/gqlgen/handler" "github.com/gorilla/websocket" "github.com/opentracing/opentracing-go" + "github.com/rs/cors" "sourcegraph.com/sourcegraph/appdash" appdashtracer "sourcegraph.com/sourcegraph/appdash/opentracing" "sourcegraph.com/sourcegraph/appdash/traceapp" @@ -18,13 +19,18 @@ import ( func main() { startAppdashServer() + c := cors.New(cors.Options{ + AllowedOrigins: []string{"http://localhost:3000"}, + AllowCredentials: true, + }) + http.Handle("/", handler.Playground("Todo", "/query")) - http.Handle("/query", handler.GraphQL(chat.NewExecutableSchema(chat.New()), + http.Handle("/query", c.Handler(handler.GraphQL(chat.NewExecutableSchema(chat.New()), handler.WebsocketUpgrader(websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true }, - })), + }))), ) log.Fatal(http.ListenAndServe(":8085", nil)) } diff --git a/example/chat/src/index.js b/example/chat/src/index.js index 7f412654dc4..c3a501c9419 100644 --- a/example/chat/src/index.js +++ b/example/chat/src/index.js @@ -3,16 +3,35 @@ import ReactDOM from 'react-dom'; import { ApolloProvider } from 'react-apollo'; import ApolloClient from 'apollo-client'; import App from './App'; -import { SubscriptionClient } from 'subscriptions-transport-ws'; import { InMemoryCache } from 'apollo-cache-inmemory'; +import { split } from 'apollo-link'; +import { HttpLink } from 'apollo-link-http'; +import { WebSocketLink } from 'apollo-link-ws'; +import { getMainDefinition } from 'apollo-utilities'; - -const client = new SubscriptionClient('ws://localhost:8085/query', { - reconnect: true, +const wsLink = new WebSocketLink({ + uri: `ws://localhost:8085/query`, + options: { + reconnect: true + } }); +const httpLink = new HttpLink({ uri: 'http://localhost:8085/query' }); + + +// depending on what kind of operation is being sent +const link = split( + // split based on operation type + ({ query }) => { + const { kind, operation } = getMainDefinition(query); + return kind === 'OperationDefinition' && operation === 'subscription'; + }, + wsLink, + httpLink, +); + const apolloClient = new ApolloClient({ - link: client, + link: link, cache: new InMemoryCache(), }); diff --git a/example/config/generated.go b/example/config/generated.go index b6da2da5fac..f408b8e5fa3 100644 --- a/example/config/generated.go +++ b/example/config/generated.go @@ -1066,7 +1066,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1093,15 +1093,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1312,7 +1316,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1339,15 +1343,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index 747d64dc162..a3d4f0ffdb2 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -1527,7 +1527,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1554,15 +1554,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1773,7 +1777,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1800,15 +1804,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/example/scalars/generated.go b/example/scalars/generated.go index a00f8d95ce2..af67c2b40dd 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -1159,7 +1159,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1186,15 +1186,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1405,7 +1409,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1432,15 +1436,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/example/selection/generated.go b/example/selection/generated.go index 32563000eca..b8f99c7e32c 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -988,7 +988,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1015,15 +1015,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1234,7 +1238,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1261,15 +1265,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/example/starwars/generated.go b/example/starwars/generated.go index f9ebe4f644b..a58a5750e6b 100644 --- a/example/starwars/generated.go +++ b/example/starwars/generated.go @@ -2969,7 +2969,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -2996,15 +2996,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -3215,7 +3219,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -3242,15 +3246,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/example/todo/generated.go b/example/todo/generated.go index b0674342506..2fbe0d159e9 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -1073,7 +1073,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1100,15 +1100,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1319,7 +1323,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1346,15 +1350,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/example/type-system-extension/generated.go b/example/type-system-extension/generated.go index 2cf4ed08804..065da51a593 100644 --- a/example/type-system-extension/generated.go +++ b/example/type-system-extension/generated.go @@ -992,7 +992,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1019,15 +1019,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1238,7 +1242,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1265,15 +1269,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"} diff --git a/graphql/introspection/introspection.go b/graphql/introspection/introspection.go index baff882ef29..ca0b065f83e 100644 --- a/graphql/introspection/introspection.go +++ b/graphql/introspection/introspection.go @@ -12,19 +12,17 @@ type ( } EnumValue struct { - Name string - Description string - IsDeprecated bool - DeprecationReason string + Name string + Description string + deprecation *ast.Directive } Field struct { - Name string - Description string - Type *Type - Args []InputValue - IsDeprecated bool - DeprecationReason string + Name string + Description string + Type *Type + Args []InputValue + deprecation *ast.Directive } InputValue struct { @@ -39,20 +37,36 @@ func WrapSchema(schema *ast.Schema) *Schema { return &Schema{schema: schema} } -func isDeprecated(directives ast.DirectiveList) bool { - return directives.ForName("deprecated") != nil +func (f *EnumValue) IsDeprecated() bool { + return f.deprecation != nil +} + +func (f *EnumValue) DeprecationReason() *string { + if f.deprecation == nil { + return nil + } + + reason := f.deprecation.Arguments.ForName("reason") + if reason == nil { + return nil + } + + return &reason.Value.Raw +} + +func (f *Field) IsDeprecated() bool { + return f.deprecation != nil } -func deprecationReason(directives ast.DirectiveList) string { - deprecation := directives.ForName("deprecated") - if deprecation == nil { - return "" +func (f *Field) DeprecationReason() *string { + if f.deprecation == nil { + return nil } - reason := deprecation.Arguments.ForName("reason") + reason := f.deprecation.Arguments.ForName("reason") if reason == nil { - return "" + return nil } - return reason.Value.Raw + return &reason.Value.Raw } diff --git a/graphql/introspection/type.go b/graphql/introspection/type.go index dce144e0c48..b963aa0e27d 100644 --- a/graphql/introspection/type.go +++ b/graphql/introspection/type.go @@ -81,12 +81,11 @@ func (t *Type) Fields(includeDeprecated bool) []Field { } fields = append(fields, Field{ - Name: f.Name, - Description: f.Description, - Args: args, - Type: WrapTypeFromType(t.schema, f.Type), - IsDeprecated: isDeprecated(f.Directives), - DeprecationReason: deprecationReason(f.Directives), + Name: f.Name, + Description: f.Description, + Args: args, + Type: WrapTypeFromType(t.schema, f.Type), + deprecation: f.Directives.ForName("deprecated"), }) } return fields @@ -150,10 +149,9 @@ func (t *Type) EnumValues(includeDeprecated bool) []EnumValue { var res []EnumValue for _, val := range t.def.EnumValues { res = append(res, EnumValue{ - Name: val.Name, - Description: val.Description, - IsDeprecated: isDeprecated(val.Directives), - DeprecationReason: deprecationReason(val.Directives), + Name: val.Name, + Description: val.Description, + deprecation: val.Directives.ForName("deprecated"), }) } return res diff --git a/integration/generated.go b/integration/generated.go index 96ab81b20a6..008cf1959ab 100644 --- a/integration/generated.go +++ b/integration/generated.go @@ -1225,7 +1225,7 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1252,15 +1252,19 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __FieldImplementors = []string{"__Field"} @@ -1471,7 +1475,7 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.IsDeprecated, nil + return obj.IsDeprecated(), nil }) if resTmp == nil { if !ec.HasError(rctx) { @@ -1498,15 +1502,19 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) resTmp := ec.FieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.DeprecationReason, nil + return obj.DeprecationReason(), nil }) if resTmp == nil { return graphql.Null } - res := resTmp.(string) + res := resTmp.(*string) rctx.Result = res ctx = ec.Tracer.StartFieldChildExecution(ctx) - return graphql.MarshalString(res) + + if res == nil { + return graphql.Null + } + return graphql.MarshalString(*res) } var __InputValueImplementors = []string{"__InputValue"}