Skip to content

Latest commit

 

History

History
95 lines (85 loc) · 1.95 KB

README.md

File metadata and controls

95 lines (85 loc) · 1.95 KB

goexpert-graphql

Como rodar o projeto

go mod tidy
sqlite3 cmd/server/data.db 'create table categories (id string, name string, description string);'
sqlite3 cmd/server/data.db 'create table courses (id string, name string, description string, category_id string);'

cd cmd/server
go run server.go

Passos para trabalhar com graphql no go

  1. Gerar o arquivo tools.go
printf '// +build tools\npackage tools\nimport (_ "github.com/99designs/gqlgen"\n _ "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go
go mod tidy
  1. Inicializar o gqlgen
go run github.com/99designs/gqlgen init && go mod tidy
  1. Gerar arquivo de model para cada entidade da aplicação, na pasta graph/model, lembrando que não é nessecário colocar o campo 'Foreign Key' no objeto X que depende de Y
  2. Definir, separadamente, no arquivo gqlgen.yml, os models das entidades da aplicação
  3. go run github.com/99designs/gqlgen generate
  4. Implementar os resolvers no arquivo graph/schema.resolvers.go
  5. Rodar o server
go run cmd/server/server.go
  1. Acesse: http://localhost:8080
  2. Criar as mutations e queries no playground do graphql
mutation createCategory {
  createCategory(input: {name: "Tecnologia", description: "Cursos de Tecnologia"}) {
    id
    name
    description
  }
}

query queryCategories {
  categories {
    id
    name
    description
  }
}

mutation createCourse {
  createCourse(
    input: {name: "Full Cycle", description: "Go Expert", categoryId: "a5936dbe-1a0d-46c2-a53b-79f7f809840f"}
  ) {
    id
    name
    description
  }
}

query queryCourses {
  courses {
    id
    name
    description
  }
}

query queryCategoriesWithCourses {
  categories {
    id
    name
    description
    courses {
      id
      name
      description
    }
  }
}

query queryCoursesWithCategories {
  courses {
    id
    name
    description
    category {
    	id
    	name
    	description
    }
  }
}