Skip to content

Commit

Permalink
feat: add support for using custom db functions as fields and tables #…
Browse files Browse the repository at this point in the history
…394

1. db functions can now be used as fields or tables with suppot for named parameters
2. added top level __typename to work with fluter_graphql, etc  #404
  • Loading branch information
dosco committed Dec 2, 2022
1 parent 3059852 commit b93d2c3
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 580 deletions.
374 changes: 0 additions & 374 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,377 +364,3 @@ func BenchmarkCompile(b *testing.B) {
resultJSON = res.Data
}
}
func TestCueValidationQuerySingleIntVarValue(t *testing.T) {
gql := `query @validation(cue:"id:2") {
users(where: {id:$id}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"id":2}`), nil)
if err != nil {
t.Error(err)
return
}
}
func TestCueInvalidationQuerySingleIntVarValue(t *testing.T) {
gql := `query @validation(cue:"id:2") {
users(where: {id:$id}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"id":3}`), nil)
if err == nil {
t.Error("expected validation error")
}
}
func TestCueValidationQuerySingleIntVarType(t *testing.T) {
gql := `query @validation(cue:"id:int") {
users(where: {id:$id}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"id":2}`), nil)
if err != nil {
t.Error(err)
return
}
}
func TestCueValidationQuerySingleIntVarOR(t *testing.T) {
gql := `query @validation(cue:"id: 3 | 2") {
users(where: {id:$id}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"id":2}`), nil)
if err != nil {
t.Error(err)
return
}
}
func TestCueInvalidationQuerySingleIntVarOR(t *testing.T) {
gql := `query @validation(cue:"id: 3 | 2") {
users(where: {id:$id}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"id":4}`), nil)
if err == nil {
t.Error(err)
}
}
func TestCueValidationQuerySingleStringVarOR(t *testing.T) {
// TODO: couldn't find a way to pass string inside cue through plain graphql query ( " )
// (only way is using varibales and escape \")
gql := `query @validation(cue:$validation) {
users(where: {email:$mail}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"mail":"[email protected]","validation":"mail: \"[email protected]\" | \"[email protected]\" "}`), nil)
if err != nil {
t.Error(err)
return
}
}
func TestCueInvalidationQuerySingleStringVarOR(t *testing.T) {
gql := `query @validation(cue:$validation) {
users(where: {email:$mail}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"mail":"[email protected]","validation":"mail: \"[email protected]\" | \"[email protected]\" "}`), nil)
if err == nil {
t.Error(err)
}
}
func TestCueInvalidationQuerySingleIntVarType(t *testing.T) {
gql := `query @validation(cue:"email:int") {
users(where: {email:$email}) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{"email":"[email protected]"}`), nil)
if err == nil {
t.Error("expected validation error")
}
}
func TestCueValidationMutationMapVarStringsLen(t *testing.T) {
if dbType == "mysql" {
t.SkipNow()
return
}
gql := `mutation @validation(cue:$validation) {
users(insert:$inp) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{
"inp":{
"id":105, "email":"[email protected]", "full_name":"Full Name", "created_at":"now", "updated_at":"now"
},
"validation":"import (\"strings\"), inp: {id?: int, full_name: string & strings.MinRunes(3) & strings.MaxRunes(22), created_at:\"now\", updated_at:\"now\", email: string}"
}`), nil)
if err != nil {
t.Error(err)
return
}
}
func TestCueInvalidationMutationMapVarStringsLen(t *testing.T) {
if dbType == "mysql" {
t.SkipNow()
return
}
gql := `mutation @validation(cue:$validation) {
users(insert:$inp) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{
"inp":{
"id":106, "email":"[email protected]", "full_name":"Fu", "created_at":"now", "updated_at":"now"
},
"validation":"import (\"strings\"), inp: {id?: int, full_name: string & strings.MinRunes(3) & strings.MaxRunes(22), created_at:\"now\", updated_at:\"now\", email: string}"
}`), nil)
if err == nil {
t.Error(err)
}
}

func TestCueValidationMutationMapVarIntMaxMin(t *testing.T) {
if dbType == "mysql" {
t.SkipNow()
return
}
gql := `mutation @validation(cue:$validation) {
users(insert:$inp) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{
"inp":{
"id":101, "email":"[email protected]", "full_name":"Full Name", "created_at":"now", "updated_at":"now"
},
"validation":" inp: {id?: int & >100 & <102, full_name: string , created_at:\"now\", updated_at:\"now\", email: string}"
}`), nil)
if err != nil {
t.Error(err)
return
}
}
func TestCueInvalidationMutationMapVarIntMaxMin(t *testing.T) {
if dbType == "mysql" {
t.SkipNow()
return
}
gql := `mutation @validation(cue:$validation) {
users(insert:$inp) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{
"inp":{
"id":107, "email":"[email protected]", "full_name":"Fu", "created_at":"now", "updated_at":"now"
},
"validation":"inp: {id?: int & >100 & <102, full_name: string , created_at:\"now\", updated_at:\"now\", email: string}"
}`), nil)
if err == nil {
t.Error(err)
}
}
func TestCueValidationMutationMapVarOptionalKey(t *testing.T) {
if dbType == "mysql" {
t.SkipNow()
return
}
gql := `mutation @validation(cue:$validation) {
users(insert:$inp) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{
"inp":{
"id":111, "email":"[email protected]", "full_name":"Fu", "created_at":"now", "updated_at":"now"
},
"validation":"inp: {id?: int, phone?: string, full_name: string , created_at:\"now\", updated_at:\"now\", email: string}"
}`), nil)
if err != nil {
t.Error(err)
return
}
}
func TestCueValidationMutationMapVarRegex(t *testing.T) {
if dbType == "mysql" {
t.SkipNow()
return
}
gql := `mutation @validation(cue:$validation) {
users(insert:$inp) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{
"inp":{
"id":108, "email":"[email protected]", "full_name":"Full Name", "created_at":"now", "updated_at":"now"
},
"validation":"inp: {id?: int & >100 & <110, full_name: string , created_at:\"now\", updated_at:\"now\", email: =~\"^[a-zA-Z0-9.!#$+%&'*/=?^_{|}\\\\-`+"`"+`~]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$\"}"
}`), nil) // regex from : https://cuelang.org/play/?id=iFcZKx72Bwm#cue@export@cue
if err != nil {
t.Error(err)
return
}
}
func TestCueInvalidationMutationMapVarRegex(t *testing.T) {
if dbType == "mysql" {
t.SkipNow()
return
}
gql := `mutation @validation(cue:$validation) {
users(insert:$inp) {
id
full_name
email
}
}`

conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
panic(err)
}

_, err = gj.GraphQL(context.Background(), gql, json.RawMessage(`{
"inp":{
"id":109, "email":"mail6@ex`+"`"+`ample.com", "full_name":"Full Name", "created_at":"now", "updated_at":"now"
},
"validation":"inp: {id?: int & >110 & <102, full_name: string , created_at:\"now\", updated_at:\"now\", email: =~\"^[a-zA-Z0-9.!#$+%&'*/=?^_{|}\\\\-`+"`"+`~]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$\"}"
}`), nil)
if err == nil {
t.Error(err)
}
}
Loading

0 comments on commit b93d2c3

Please sign in to comment.