Skip to content

Commit

Permalink
feat: CLI client request from file (#1503)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #1258 

## Description

Adds support to read from a file to the `client query` CLI command, via
`-f` flag, and a simple test for it, and two related examples in the
`/examples` directory.
  • Loading branch information
orpheuslummis authored May 15, 2023
1 parent 84f6c8f commit c4b33f0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
21 changes: 15 additions & 6 deletions cli/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ import (
)

func MakeRequestCommand(cfg *config.Config) *cobra.Command {
var filePath string
var cmd = &cobra.Command{
Use: "query [query request]",
Short: "Send a DefraDB GraphQL query request",
Long: `Send a DefraDB GraphQL query request to the database.
A query request can be sent as a single argument. Example command:
defradb client query 'query { ... }'
defradb client query 'query { ... }'
Do a query request from a file by using the '-f' flag. Example command:
defradb client query -f request.graphql
Or it can be sent via stdin by using the '-' special syntax. Example command:
cat request.graphql | defradb client query -
cat request.graphql | defradb client query -
A GraphQL client such as GraphiQL (https://github.com/graphql/graphiql) can be used to interact
with the database more conveniently.
Expand All @@ -48,14 +52,18 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so
return err
}

if len(args) > 1 {
if filePath != "" {
bytes, err := os.ReadFile(filePath)
if err != nil {
return ErrFailedToReadFile
}
request = string(bytes)
} else if len(args) > 1 {
if err = cmd.Usage(); err != nil {
return err
}
return errors.New("too many arguments")
}

if isFileInfoPipe(fi) && (len(args) == 0 || args[0] != "-") {
} else if isFileInfoPipe(fi) && (len(args) == 0 || args[0] != "-") {
log.FeedbackInfo(
cmd.Context(),
"Run 'defradb client query -' to read from stdin. Example: 'cat my.graphql | defradb client query -').",
Expand Down Expand Up @@ -136,5 +144,6 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so
},
}

cmd.Flags().StringVarP(&filePath, "file", "f", "", "File containing the query request")
return cmd
}
5 changes: 5 additions & 0 deletions examples/request/user_creation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation {
create_User(data: "{\"age\": 31, \"verified\": true, \"points\": 90, \"name\": \"Bob\"}") {
_key
}
}
9 changes: 9 additions & 0 deletions examples/request/user_query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query {
User {
name
age
verified
points
_key
}
}
36 changes: 36 additions & 0 deletions tests/integration/cli/client_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,39 @@ func TestRequestWithErrorNoField(t *testing.T) {

assertContainsSubstring(t, stdout, `Cannot query field \"nonexistent\"`)
}

func TestRequestQueryFromFile(t *testing.T) {
conf := NewDefraNodeDefaultConfig(t)
stopDefra := runDefraNode(t, conf)
defer stopDefra()

fname := schemaFileFixture(t, "schema.graphql", `
type User123 {
XYZ: String
}`)
stdout, _ := runDefraCommand(t, conf, []string{"client", "schema", "add", "-f", fname})
assertContainsSubstring(t, stdout, "success")

fname = schemaFileFixture(t, "query.graphql", `
query {
__schema {
types {
name
fields {
name
type {
name
kind
}
}
}
}
}`)
stdout, _ = runDefraCommand(t, conf, []string{"client", "query", "-f", fname})

assertContainsSubstring(t, stdout, "Query")

// Check that the User type is correctly returned
assertContainsSubstring(t, stdout, "User123")
assertContainsSubstring(t, stdout, "XYZ")
}

0 comments on commit c4b33f0

Please sign in to comment.