うさぎ小屋(仮)

📅  2021-11-15

gqlgen.ymlを編集して指定の場所にスケルトンを配置する


前回の続き。 デフォルトのディレクトリ構成のままでは使いづらいので、

生成されたgqlgen.ymlを編集してスキーマやリゾルバの配置先などを変更していきます。

ディレクトリの全体像をどうするか考える必要がありますが、graphqlまわりのコードは最低でもプレゼンテーション層に置くのが良いと思うので、下記のようにディレクトリを作成してそこに配置していきます。

.
├── app
│   └── presentation
│       └── graphql
│           ├── generated
│           │   └── generated.go
│           ├── model
│           │   └── models_gen.go
│           ├── resolver.go
│           ├── schema.graphqls
│           └── schema.resolvers.go
├── go.mod
├── go.sum
├── gqlgen.yml
└── server.go

gqlgen.ymlの更新。ディレクトリ指定箇所のみ変更しています。

各generateするファイルのファイル名、パッケージ名、ディレクトリなどを指定できます。

schemaやリゾルバはtypeごとにファイルを分けることもできそうですね。

# Where are all the schema files located? globs are supported eg  src/**/*.graphqls
schema:
  - app/presentation/graphql/*.graphqls

# Where should the generated server code go?
exec:
  filename: app/presentation/graphql/generated/generated.go
  package: generated

# Uncomment to enable federation
# federation:
#   filename: app/presentation/graphql/generated/federation.go
#   package: generated

# Where should any generated models go?
model:
  filename: app/presentation/graphql/model/models_gen.go
  package: model

# Where should the resolver implementations go?
resolver:
  layout: follow-schema
  dir: app/presentation/graphql
  package: graphql

# Optional: turn on use `gqlgen:"fieldName"` tags in your models
# struct_tag: json

# Optional: turn on to use []Thing instead of []*Thing
# omit_slice_element_pointers: false

# Optional: set to speed up generation time by not performing a final validation pass.
# skip_validation: true

# gqlgen will search for any type names in the schema in these go packages
# if they match it will use them, otherwise it will generate them.
autobind:
  - "github.com/amasok/sample-graphql/app/presentation/graphql/model"
# This section declares type mapping between the GraphQL and go type systems
#
# The first line in each type will be used as defaults for resolver arguments and
# modelgen, the others will be allowed when binding to fields. Configure them to
# your liking
models:
  ID:
    model:
      - github.com/99designs/gqlgen/graphql.ID
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int64
      - github.com/99designs/gqlgen/graphql.Int32
  Int:
    model:
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int64
      - github.com/99designs/gqlgen/graphql.Int32

gitのdiffは以下な感じ。

renamed:    graph/generated/generated.go -> app/presentation/graphql/generated/generated.go
renamed:    graph/model/models_gen.go -> app/presentation/graphql/model/models_gen.go
renamed:    graph/resolver.go -> app/presentation/graphql/resolver.go
renamed:    graph/schema.graphqls -> app/presentation/graphql/schema.graphqls
renamed:    graph/schema.resolvers.go -> app/presentation/graphql/schema.resolvers.go

あとは、gqlgenのコマンドでエラーがでないか確認をして出来上がり。

# schema.graphqlsを更新したら下記コマンドを実行してgoのganeratedファイルを更新していく。
gqlgen generate