简体   繁体   English

GraphQL 项目结构

[英]GraphQL project structure

What is the best way to structure a graphQL project/server side?构建 graphQL 项目/服务器端的最佳方法是什么? this is my current structure这是我目前的结构

  • src源文件
  • config配置

  • models楷模

  • setup设置
  • schema模式
    • queries查询
      • index指数
      • userQuery用户查询
    • resolvers解析器
      • index指数
      • userResolver用户解析器
    • types类型
      • index指数
      • userType用户类型

I don't think there is the best way to structure a GraphQL server. 我认为没有最好的方法来构造GraphQL服务器。 Your seems fine! 你看起来还好!

Check this GraphQL APIs repo that contains several examples and repositories with many different implementations. 检查此GraphQL API存储库,其中包含几个示例和具有许多不同实现的存储库。

In my TypeScript projects I usually use a structure like this: 在我的TypeScript项目中,我通常使用如下结构:

src/
├── user/
│   ├── data.ts
│   ├── mutation.ts
│   ├── query.ts
│   └── type.ts
├── bananas/
│   ├── data.ts
│   ├── mutation.ts
│   ├── query.ts
│   └── type.ts
├── utils/
│   ├── database.ts
│   └── config.ts
├── index.ts
└── schema.ts
src
  schema
     Product
        model.js
        query.js
        mutation.js
        type.js
        resolvers.js
        index.js
     Order
        query.js
        mutation.js
        model.js
        types.js
        resolvers.js
        index.js
     index.js

let's explore what's inside the Product directory让我们探索一下 Product 目录中的内容

query.js : all the query resolvers related to the Product query.js :与产品相关的所有查询解析器

mutations.js : all the mutation resolvers related to the Product mutations.js :所有与产品相关的突变解析器

types.js : all the Product related GraphQL types also query and mutation included (export a string containing GraphQL types). types.js :所有与产品相关的 GraphQL 类型也包括查询和变异(导出包含 GraphQL 类型的字符串)。

mode.js : the Product database schema. mode.js :产品数据库架构。

resolvers.js : all the resolvers related to the Product type. resolvers.js :与产品类型相关的所有解析器。 eg:例如:

let Product = {
    comments: (user: id) => {
        // whatever
    }
}

Product/index.js : combine all the files and export them as Query , Mutation , types , Product (Product type fields resolvers). Product/index.js :组合所有文件并将它们导出为QueryMutationtypesProduct (产品类型字段解析器)。

Note: you can also convert query.js or any one of them to a folder and then write each query and mutation resolver in its own file.注意:您还可以将 query.js 或其中任何一个转换为文件夹,然后将每个查询和变异解析器写入自己的文件中。

schema/index.js : combine all the exported Query , Mutation , type inside index.js and export them as resolvers and typeDefs架构/ index.js:将所有导出查询突变内index.js并导出为解析器类型定义

eg例如

export const resolvers = {
    Query: {
      ...ProductQueries,
      ...OrderQueries,
    },
    Mutation: {
      ...ProductQueries,
      ...OrderMutations,
    },
    // schema/Proudct/resolvers.js
    Product,
    Order
}

For a complete description follow this link https://theehsansarshar.hashnode.dev/scalable-graphql-architecture有关完整说明,请点击此链接https://theehsansarshar.hashnode.dev/scalable-graphql-architecture

I have created the simplest GraphQL TypeScript project with a standard folder structure.我使用标准文件夹结构创建了最简单的 GraphQL TypeScript 项目。 You can refer to.可以参考。

https://github.com/JayeshGatkal/typescript-graphql-demo https://github.com/JayeshGatkal/typescript-graphql-demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM