-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for using custom db functions as fields and tables #…
…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
Showing
11 changed files
with
665 additions
and
580 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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 | ||
} | ||
}` | ||
|
||
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) | ||
} | ||
} |
Oops, something went wrong.