Skip to content
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

Update getting started for 0.4.0 #6

Merged
merged 1 commit into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
601 changes: 381 additions & 220 deletions gettingstarted/graph/generated.go → gettingstarted/generated.go

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions gettingstarted/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# .gqlgen.yml example
#
# Refer to https://gqlgen.com/config/
# for detailed .gqlgen.yml documentation.

schema: schema.graphql
exec:
filename: graph/generated.go
filename: generated.go
model:
filename: model/generated.go
filename: models_gen.go
resolver:
filename: resolver.go
type: Resolver

models:
Todo:
model: github.com/vektah/gqlgen-tutorials/gettingstarted/model.Todo
User:
model: github.com/vektah/gqlgen-tutorials/gettingstarted/model.User
model: github.com/vektah/gqlgen-tutorials/gettingstarted.Todo
51 changes: 0 additions & 51 deletions gettingstarted/graph/graph.go

This file was deleted.

4 changes: 0 additions & 4 deletions gettingstarted/graph/types.json

This file was deleted.

18 changes: 0 additions & 18 deletions gettingstarted/main.go

This file was deleted.

8 changes: 0 additions & 8 deletions gettingstarted/model/generated.go

This file was deleted.

6 changes: 0 additions & 6 deletions gettingstarted/model/user.go

This file was deleted.

12 changes: 12 additions & 0 deletions gettingstarted/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion gettingstarted/readme.md

This file was deleted.

47 changes: 47 additions & 0 deletions gettingstarted/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:generate gorunpkg github.com/vektah/gqlgen

package gettingstarted

import (
context "context"
"fmt"
"math/rand"
)

type Resolver struct{
todos []Todo
}

func (r *Resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}
func (r *Resolver) Todo() TodoResolver {
return &todoResolver{r}
}

type mutationResolver struct{ *Resolver }

func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (Todo, error) {
todo := Todo{
Text: input.Text,
ID: fmt.Sprintf("T%d", rand.Int()),
UserID: input.UserID,
}
r.todos = append(r.todos, todo)
return todo, nil
}

type queryResolver struct{ *Resolver }

func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) {
return r.todos, nil
}

type todoResolver struct{ *Resolver }

func (r *todoResolver) User(ctx context.Context, obj *Todo) (User, error) {
return User{ID: obj.UserID, Name: "user " + obj.UserID}, nil
}
4 changes: 2 additions & 2 deletions gettingstarted/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type Query {

input NewTodo {
text: String!
user: String!
userId: String!
}

type Mutation {
createTodo(input: NewTodo!): Todo!
}
}
25 changes: 25 additions & 0 deletions gettingstarted/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"log"
"net/http"
"os"

gettingstarted "github.com/vektah/gqlgen-tutorials/gettingstarted"
"github.com/vektah/gqlgen/handler"
)

const defaultPort = "8080"

func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}

http.Handle("/", handler.Playground("GraphQL playground", "/query"))
http.Handle("/query", handler.GraphQL(gettingstarted.NewExecutableSchema(gettingstarted.Config{Resolvers: &gettingstarted.Resolver{}})))

log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
2 changes: 1 addition & 1 deletion gettingstarted/model/todo.go → gettingstarted/todo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package gettingstarted

type Todo struct {
ID string
Expand Down