-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Multiple schema file support in schema add #3352
Changes from 8 commits
74331e3
e4c55ad
746e13e
e006561
9cb6ecd
58e71f5
15b8d22
80a507b
a1740f4
88159c4
943173a
25f533d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
) | ||
|
||
func MakeSchemaAddCommand() *cobra.Command { | ||
var schemaFile string | ||
var schemaFiles []string | ||
var cmd = &cobra.Command{ | ||
Use: "add [schema]", | ||
Short: "Add new schema", | ||
|
@@ -36,40 +36,56 @@ | |
Example: add from file: | ||
defradb client schema add -f schema.graphql | ||
|
||
Example: add from multiple files: | ||
defradb client schema add -f schema1.graphql -f schema2.graphql | ||
|
||
Example: add from stdin: | ||
cat schema.graphql | defradb client schema add - | ||
|
||
Learn more about the DefraDB GraphQL Schema Language on https://docs.source.network.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
store := mustGetContextStore(cmd) | ||
|
||
var schema string | ||
var combinedSchema string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: This is a nice and simple way of ensure the whole operation takes place in a single txn. thought: This does introduce a difference between the 3 clients - it might be nicer for users if instead we changed the other 2 to accept multiple strings. You would then be able to test this using integration tests instead of CLI specific stuff. I think there is also an argument to be had that without changing the other 2 clients, the CLI should not have this feature, and instead users can just use |
||
switch { | ||
case schemaFile != "": | ||
data, err := os.ReadFile(schemaFile) | ||
if err != nil { | ||
return err | ||
case len(schemaFiles) > 0: | ||
// Read schemas from files and concatenate them | ||
for _, schemaFile := range schemaFiles { | ||
data, err := os.ReadFile(schemaFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to read file %s: %w", schemaFile, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: please move the errors to the |
||
} | ||
combinedSchema += string(data) + "\n" | ||
} | ||
schema = string(data) | ||
|
||
case len(args) > 0 && args[0] == "-": | ||
// Read schema from stdin | ||
data, err := io.ReadAll(cmd.InOrStdin()) | ||
if err != nil { | ||
return err | ||
return fmt.Errorf("failed to read schema from stdin: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: please move the errors to the |
||
} | ||
schema = string(data) | ||
combinedSchema += string(data) + "\n" | ||
|
||
case len(args) > 0: | ||
schema = args[0] | ||
// Read schema from argument string | ||
combinedSchema += args[0] + "\n" | ||
|
||
default: | ||
return fmt.Errorf("schema cannot be empty") | ||
} | ||
|
||
cols, err := store.AddSchema(cmd.Context(), schema) | ||
// Process the combined schema | ||
cols, err := store.AddSchema(cmd.Context(), combinedSchema) | ||
if err != nil { | ||
return fmt.Errorf("failed to add schema: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: please move the errors to the |
||
} | ||
if err := writeJSON(cmd, cols); err != nil { | ||
return err | ||
} | ||
return writeJSON(cmd, cols) | ||
|
||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&schemaFile, "file", "f", "", "File to load a schema from") | ||
cmd.Flags().StringSliceVarP(&schemaFiles, "file", "f", []string{}, "File(s) to load schema from") | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ Example: add from an argument string: | |
Example: add from file: | ||
defradb client schema add -f schema.graphql | ||
|
||
Example: add from multiple files: | ||
defradb client schema add -f schema1.graphql -f schema2.graphql | ||
|
||
Example: add from stdin: | ||
cat schema.graphql | defradb client schema add - | ||
|
||
|
@@ -29,8 +32,8 @@ defradb client schema add [schema] [flags] | |
### Options | ||
|
||
``` | ||
-f, --file string File to load a schema from | ||
-h, --help help for add | ||
-f, --file strings File(s) to load schema from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: saying
rather than:
non-blocking: because maybe it might just be me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer your suggestion, but I have a vague memory of the team having this discussion and deciding on the multiple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have an opinion on this, but not a strong one. Happy to go with the team consensus (or majority opinion.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a strong prefer at all on the approach we take. My thought was only on the changed documentation in this PR, I think it should say Happy with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see where you are coming from now. Hmm... I think I agree. I will be changing that to the singular as you suggest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry commented on the main PR thread, can continue here if you like: #3352 (comment) |
||
-h, --help help for add | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: IMO it would be nice to standardise this, so that all file-based inputs can be expected to work the same way.