美文网首页
gqlgen初体验(一)

gqlgen初体验(一)

作者: 戈壁堂 | 来源:发表于2021-05-01 18:43 被阅读0次

    Building a GraphQL API in Go using gqlgen

    依次执行以下命令,则完成了gqlgen站点的getting started的内容。

    cd mygplprj/src
    go mod init gitlab.com/jigar_xyz/mygplprj
    cd cmd/go-graphql
    go get github.com/99designs/gqlgen
    go run github.com/99designs/gqlgen init --verbose
    

    执行go run ./server.go就可以起到一个GraghQL站点。目录结构说明——

    ├── gqlgen.yml               - The gqlgen config file, knobs for controlling the generated code.
    ├── graph
    │   ├── generated            - A package that only contains the generated runtime
    │   │   └── generated.go
    │   ├── model                - A package for all your graph models, generated or otherwise
    │   │   └── models_gen.go
    │   ├── resolver.go          - The root graph resolver type. This file wont get regenerated
    │   ├── schema.graphqls      - Some schema. You can split the schema into as many graphql files as you like
    │   └── schema.resolvers.go  - the resolver implementation for schema.graphql
    └── server.go                - The entry point to your app. Customize it however you see fit
    
    • gqlgen.xml 配置文件,说明如何自动生成代码。更详细的介绍参考config。结合目前工程中的gqlgen.xml可以更好理解
    • generated/generated.go 根据配置自动生成的服务端文件,相当于GraphQL的运行时。可在上述配置文件中做对应的声明(文件名,文件地址,所属的包)
    • models_gen.go 对应各个*.graphqls文件绑定的model,gqlgen会自己进行匹配,如果找到则忽略(相当于人工写),否则会根据定义自动生成。Generated models required to build the graph. Often you will override these with your own models. Still very useful for input types.
    • resolver.go 根类型resolver
    • schema.graphqls 定义的schema,可以分割到多个不同的文件中
    • schema.resolvers.go 对定义的实现

    Introducing gqlgen: a GraphQL Server Generator for Go 项目开发人员对gqlgen的介绍

    相关文章

      网友评论

          本文标题:gqlgen初体验(一)

          本文链接:https://www.haomeiwen.com/subject/dymwrltx.html