Skip to content

Commit

Permalink
feat: modify extract provider position on build datasource configurat…
Browse files Browse the repository at this point in the history
…ion, and add judge database kind optional raw supported.
  • Loading branch information
lovestaryouth committed Jan 18, 2025
1 parent 647a7fb commit 149dae4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 33 deletions.
14 changes: 0 additions & 14 deletions pkg/api/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package api

import (
"bytes"
"fireboom-server/pkg/api/base"
"fireboom-server/pkg/common/consts"
"fireboom-server/pkg/common/models"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/wundergraph/wundergraph/pkg/datasources/database"
"github.com/wundergraph/wundergraph/pkg/wgpb"
"net/http"
"strings"
)

func DatasourceExtraRouter(_, datasourceRouter *echo.Group, baseHandler *base.Handler[models.Datasource], modelRoot *fileloader.Model[models.Datasource]) {
Expand Down Expand Up @@ -315,18 +313,6 @@ func (d *datasource) updatePrismaText(c echo.Context) (err error) {
return
}

var (
providerPrefix = []byte(`provider = "`)
providerSuffix = []byte(`"`)
)
prefixIndex := bytes.Index(body, providerPrefix)
startIndex := prefixIndex + len(providerPrefix)
suffixIndex := bytes.Index(body[startIndex:], providerSuffix)
data, _ := models.DatasourceRoot.GetByDataName(dataName)
if prefixIndex != -1 && suffixIndex != -1 && data != nil {
data.KindForPrisma = wgpb.DataSourceKind(wgpb.DataSourceKind_value[strings.ToUpper(string(body[startIndex:startIndex+suffixIndex]))])
_ = models.DatasourceRoot.InsertOrUpdate(data)
}
return c.NoContent(http.StatusOK)
}

Expand Down
1 change: 0 additions & 1 deletion pkg/common/models/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type Datasource struct {
CacheEnabled bool `json:"cacheEnabled"`

Kind wgpb.DataSourceKind `json:"kind"`
KindForPrisma wgpb.DataSourceKind `json:"kindForPrisma,omitempty"`
CustomRest *CustomRest `json:"customRest"`
CustomAsyncapi *CustomRest `json:"customAsyncapi,omitempty"`
CustomGraphql *CustomGraphql `json:"customGraphql"`
Expand Down
9 changes: 4 additions & 5 deletions pkg/engine/build/engine_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,22 @@ func (e *engineConfiguration) Resolve(builder *Builder) (err error) {
continue
}

if extend, ok := itemAction.(datasource.ActionExtend); ok {
extend.ExtendDocument(itemDocument)
}

// 调用不同的函数构建引擎所需配置
if itemConfig, err = itemAction.BuildDataSourceConfiguration(itemDocument); err != nil || itemConfig == nil {
e.printIntrospectError(err, ds.Name)
continue
}

if extend, ok := itemAction.(datasource.ActionExtend); ok {
extend.ExtendDocument(itemDocument)
}

// 将数据源名称添加到graphql命名中
itemRename = newDataSourceRename(ds.Name, itemDocument, e, itemAction)
itemRename.resolve()

itemConfig.Id = ds.Name
itemConfig.Kind = ds.Kind
itemConfig.KindForPrisma = ds.KindForPrisma
itemConfig.RootNodes = itemRename.rootNodes
itemConfig.ChildNodes = itemRename.childNodes
e.engineConfig.DatasourceConfigurations = append(e.engineConfig.DatasourceConfigurations, itemConfig)
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/datasource/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func (a *actionDatabase) RuntimeDataSourceConfiguration(config *wgpb.DataSourceC
}

func (a *actionDatabase) ExtendDocument(document *ast.SchemaDocument) {
extendOptionalRawField(document)
extendOptionalRawField(a.ds.Kind, document)
}

func (a *actionDatabase) GetFieldRealName(fieldName string) string {
return getRawFieldOriginName(fieldName)
return getRawFieldOriginName(a.ds.Kind, fieldName)
}

// 根据数据库类型组装introspectSchema
Expand Down
18 changes: 11 additions & 7 deletions pkg/engine/datasource/prisma.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {

type actionPrisma struct {
ds *models.Datasource
providerKind wgpb.DataSourceKind
prismaSchema string
}

Expand All @@ -35,12 +36,15 @@ func (a *actionPrisma) Introspect() (graphqlSchema string, err error) {
func (a *actionPrisma) BuildDataSourceConfiguration(*ast.SchemaDocument) (config *wgpb.DataSourceConfiguration, err error) {
prismaSchemaFilepath := models.DatasourceUploadPrisma.GetPath(a.ds.Name)
introspectSchema, _ := extractIntrospectSchema(prismaSchemaFilepath)
a.providerKind = extractDatabaseProvider(introspectSchema)
environmentVariable, _ := extractEnvironmentVariable(introspectSchema)
config = &wgpb.DataSourceConfiguration{CustomDatabase: &wgpb.DataSourceCustom_Database{
DatabaseURL: utils.MakeStaticVariable(prismaSchemaFilepath),
JsonInputVariables: []string{consts.ScalarJSON},
EnvironmentVariable: environmentVariable,
}}
config = &wgpb.DataSourceConfiguration{
KindForPrisma: a.providerKind,
CustomDatabase: &wgpb.DataSourceCustom_Database{
DatabaseURL: utils.MakeStaticVariable(prismaSchemaFilepath),
JsonInputVariables: []string{consts.ScalarJSON},
EnvironmentVariable: environmentVariable,
}}
return
}

Expand All @@ -51,11 +55,11 @@ func (a *actionPrisma) RuntimeDataSourceConfiguration(config *wgpb.DataSourceCon
}

func (a *actionPrisma) ExtendDocument(document *ast.SchemaDocument) {
extendOptionalRawField(document)
extendOptionalRawField(a.providerKind, document)
}

func (a *actionPrisma) GetFieldRealName(fieldName string) string {
return getRawFieldOriginName(fieldName)
return getRawFieldOriginName(a.providerKind, fieldName)
}

func (a *actionPrisma) fetchSchemaEngineInput() (engineInput EngineInput, skipGraphql bool, err error) {
Expand Down
22 changes: 22 additions & 0 deletions pkg/engine/datasource/prisma_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fireboom-server/pkg/common/utils"
"fireboom-server/pkg/plugins/fileloader"
"github.com/wundergraph/wundergraph/pkg/wgpb"
"regexp"
"slices"
"strings"
Expand Down Expand Up @@ -78,6 +79,27 @@ func extractEnvironmentVariable(prismaSchema string) (string, bool) {
return "", false
}

var (
databaseProviderPrefix = `provider = "`
databaseProviderSuffix = `"`
)

// 提取数据库类型
func extractDatabaseProvider(prismaSchema string) (kind wgpb.DataSourceKind) {
prefixIndex := strings.Index(prismaSchema, databaseProviderPrefix)
if prefixIndex == -1 {
return
}
startIndex := prefixIndex + len(databaseProviderPrefix)
suffixIndex := strings.Index(prismaSchema[startIndex:], databaseProviderSuffix)
if suffixIndex == -1 {
return
}
provider := prismaSchema[startIndex : startIndex+suffixIndex]
kind = wgpb.DataSourceKind(wgpb.DataSourceKind_value[strings.ToUpper(provider)])
return
}

func extractIntrospectSchema(prismaSchemaFilepath string) (introspectSchema string, err error) {
prismaSchemaFileText, err := utils.ReadWithCondition(prismaSchemaFilepath, nil,
func(_ int, line string) bool { return strings.HasSuffix(line, "}") })
Expand Down
16 changes: 13 additions & 3 deletions pkg/engine/datasource/prisma_extend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package datasource
import (
"fireboom-server/pkg/common/consts"
"github.com/vektah/gqlparser/v2/ast"
"github.com/wundergraph/wundergraph/pkg/datasources/database"
"github.com/wundergraph/wundergraph/pkg/wgpb"
)

const (
Expand All @@ -15,19 +17,27 @@ var originRawFields = map[string]string{
optionalRawFieldExecuteRaw: "executeRaw",
}

func getRawFieldOriginName(fieldName string) string {
func getRawFieldOriginName(kind wgpb.DataSourceKind, fieldName string) string {
if !database.SupportOptionalRaw(kind) {
return fieldName
}
if name, ok := originRawFields[fieldName]; ok {
fieldName = name
}
return fieldName
}

func extendOptionalRawField(document *ast.SchemaDocument) {
func extendOptionalRawField(kind wgpb.DataSourceKind, document *ast.SchemaDocument) {
if !database.SupportOptionalRaw(kind) {
return
}
mutations := document.Definitions.ForName(consts.TypeMutation)
if mutations == nil {
return
}
mutations.Fields = append(mutations.Fields, makeExtendRawField(optionalRawFieldQueryRaw), makeExtendRawField(optionalRawFieldExecuteRaw))
mutations.Fields = append(mutations.Fields,
makeExtendRawField(optionalRawFieldQueryRaw),
makeExtendRawField(optionalRawFieldExecuteRaw))
}

func makeExtendRawField(name string) *ast.FieldDefinition {
Expand Down

0 comments on commit 149dae4

Please sign in to comment.