Skip to content

Initial GraphQL setup #3483

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

Closed
wants to merge 12 commits into from
Next Next commit
Add graphql command
  • Loading branch information
MichaelJCompton committed May 28, 2019
commit 6d3f5b90db75e4989ae6e9ae4be3cbb08cd640df
101 changes: 101 additions & 0 deletions dgraph/cmd/graphql/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package graphql

import (
"fmt"
"os"

"github.com/dgraph-io/dgraph/x"
"github.com/spf13/cobra"
)

type options struct {
schemaFile string
alpha string
useCompression bool
}

var opt options

var GraphQL x.SubCommand

func init() {
GraphQL.Cmd = &cobra.Command{
Use: "graphql",
Short: "Run the Dgraph GraphQL tool",
Run: func(cmd *cobra.Command, args []string) {
defer x.StartProfile(GraphQL.Conf).Stop()
run()
},
}

GraphQL.EnvPrefix = "DGRAPH_GRAPHQL"

flags := GraphQL.Cmd.Flags()
flags.StringP("alpha", "a", "127.0.0.1:9080",
"Comma-separated list of Dgraph alpha gRPC server addresses")
flags.BoolP("use_compression", "C", false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why capital C specifically?

"Enable compression on connection to alpha server")
flags.StringP("schema", "s", "schema.graphql",
"Location of GraphQL schema file")

cmdInit := &cobra.Command{
Use: "init",
Short: "Initializes Dgraph for a GraphQL schema",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
defer x.StartProfile(GraphQL.Conf).Stop()
if err := initDgraph(); err != nil {
os.Exit(1)
}
},
}
cmdInit.Flags().AddFlag(GraphQL.Cmd.Flag("alpha"))
cmdInit.Flags().AddFlag(GraphQL.Cmd.Flag("schema"))
GraphQL.Cmd.AddCommand(cmdInit)

// TLS configuration
x.RegisterClientTLSFlags(flags)
}

func run() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, very long function with too many nested blocks. Should break it down into multiple functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above - will get spilt up as more parts implemented

x.PrintVersion()
opt = options{
schemaFile: GraphQL.Conf.GetString("schema"),
alpha: GraphQL.Conf.GetString("alpha"),
useCompression: GraphQL.Conf.GetBool("use_compression"),
}

fmt.Printf("Bringing up GraphQL API\n")
fmt.Printf("...Go make a cup of tea while I implement this\n")
}

func initDgraph() error {
x.PrintVersion()
opt = options{
schemaFile: GraphQL.Conf.GetString("schema"),
alpha: GraphQL.Conf.GetString("alpha"),
}

fmt.Printf("\nProcessing schema file %q\n", opt.schemaFile)
fmt.Printf("Loading into Dgraph at %q\n", opt.alpha)

fmt.Printf("...Go make a cup of tea while I implement this\n")

return nil
}
3 changes: 2 additions & 1 deletion dgraph/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/dgraph-io/dgraph/dgraph/cmd/conv"
"github.com/dgraph-io/dgraph/dgraph/cmd/counter"
"github.com/dgraph-io/dgraph/dgraph/cmd/debug"
"github.com/dgraph-io/dgraph/dgraph/cmd/graphql"
"github.com/dgraph-io/dgraph/dgraph/cmd/live"
"github.com/dgraph-io/dgraph/dgraph/cmd/version"
"github.com/dgraph-io/dgraph/dgraph/cmd/zero"
Expand Down Expand Up @@ -72,7 +73,7 @@ var rootConf = viper.New()
// subcommands initially contains all default sub-commands.
var subcommands = []*x.SubCommand{
&bulk.Bulk, &cert.Cert, &conv.Conv, &live.Live, &alpha.Alpha, &zero.Zero, &version.Version,
&debug.Debug, &counter.Increment, &migrate.Migrate,
&debug.Debug, &counter.Increment, &migrate.Migrate, &graphql.GraphQL,
}

func initCmds() {
Expand Down