-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add graphql function to js script engine
- Loading branch information
Showing
4 changed files
with
164 additions
and
23 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
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
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 |
---|---|---|
|
@@ -1041,10 +1041,52 @@ func Example_queryWithScriptDirective() { | |
} else { | ||
fmt.Println(string(res.Data)) | ||
} | ||
|
||
// Output: {"usersbyid":{"email":"[email protected]","id":2}} | ||
} | ||
|
||
func Example_queryWithScriptDirectiveUsingGraphQL() { | ||
gql := `query @script(name: "test.js") { | ||
usersById(id: 2) { | ||
id | ||
} | ||
}` | ||
|
||
script := ` | ||
function response(json) { | ||
let val = graphql('query { users(id: 1) { id email } }') | ||
json.usersbyid.email = val.users.email | ||
return json; | ||
} | ||
` | ||
|
||
dir, err := ioutil.TempDir("", "test") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
err = ioutil.WriteFile(path.Join(dir, "test.js"), []byte(script), 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
conf := &core.Config{DBType: dbType, DisableAllowList: true, ScriptPath: dir} | ||
gj, err := core.NewGraphJin(conf, db) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
res, err := gj.GraphQL(context.Background(), gql, nil, nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
fmt.Println(string(res.Data)) | ||
} | ||
|
||
// Output: {"usersbyid":{"email":"[email protected]","id":2}} | ||
} | ||
|
||
func Example_queryWithView() { | ||
gql := `query { | ||
hot_products(limit: 3) { | ||
|
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