精简后的项目:仅保留 api/ui 源码、文档、构建依赖
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package graphql_endpoint
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
graphql_handler "github.com/99designs/gqlgen/graphql/handler"
|
||||
"github.com/99designs/gqlgen/graphql/handler/extension"
|
||||
"github.com/99designs/gqlgen/graphql/handler/lru"
|
||||
"github.com/99designs/gqlgen/graphql/handler/transport"
|
||||
photoview_graphql "github.com/photoview/photoview/api/graphql"
|
||||
"github.com/photoview/photoview/api/graphql/auth"
|
||||
"github.com/photoview/photoview/api/graphql/resolvers"
|
||||
"github.com/photoview/photoview/api/server"
|
||||
"github.com/photoview/photoview/api/utils"
|
||||
"github.com/vektah/gqlparser/v2/ast"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GraphqlEndpoint(db *gorm.DB) *graphql_handler.Server {
|
||||
graphqlResolver := resolvers.NewRootResolver(db)
|
||||
graphqlDirective := photoview_graphql.DirectiveRoot{}
|
||||
graphqlDirective.IsAdmin = photoview_graphql.IsAdmin
|
||||
graphqlDirective.IsAuthorized = photoview_graphql.IsAuthorized
|
||||
|
||||
graphqlConfig := photoview_graphql.Config{
|
||||
Resolvers: &graphqlResolver,
|
||||
Directives: graphqlDirective,
|
||||
}
|
||||
|
||||
graphqlServer := graphql_handler.New(photoview_graphql.NewExecutableSchema(graphqlConfig))
|
||||
graphqlServer.AddTransport(transport.Websocket{
|
||||
KeepAlivePingInterval: 10 * time.Second,
|
||||
Upgrader: server.WebsocketUpgrader(utils.DevelopmentMode()),
|
||||
InitFunc: auth.AuthWebsocketInit(),
|
||||
})
|
||||
graphqlServer.AddTransport(transport.Options{})
|
||||
graphqlServer.AddTransport(transport.GET{})
|
||||
graphqlServer.AddTransport(transport.POST{})
|
||||
graphqlServer.AddTransport(transport.MultipartForm{})
|
||||
|
||||
graphqlServer.SetQueryCache(lru.New[*ast.QueryDocument](1000))
|
||||
|
||||
graphqlServer.Use(extension.AutomaticPersistedQuery{
|
||||
Cache: lru.New[string](100),
|
||||
})
|
||||
|
||||
if utils.DevelopmentMode() {
|
||||
graphqlServer.Use(extension.Introspection{})
|
||||
}
|
||||
|
||||
return graphqlServer
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package graphql_endpoint_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
graphql_endpoint "github.com/photoview/photoview/api/graphql/endpoint"
|
||||
"github.com/photoview/photoview/api/test_utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
test_utils.UnitTestRun(m)
|
||||
}
|
||||
|
||||
func TestGraphqlEndpoint(t *testing.T) {
|
||||
db := test_utils.DatabaseTest(t)
|
||||
|
||||
t.Run("creates server successfully", func(t *testing.T) {
|
||||
server := graphql_endpoint.GraphqlEndpoint(db)
|
||||
assert.NotNil(t, server)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user