简体   繁体   中英

GraphQL project structure

What is the best way to structure a graphQL project/server side? 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. Your seems fine!

Check this GraphQL APIs repo that contains several examples and repositories with many different implementations.

In my TypeScript projects I usually use a structure like this:

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

query.js : all the query resolvers related to the Product

mutations.js : all the mutation resolvers related to the Product

types.js : all the Product related GraphQL types also query and mutation included (export a string containing GraphQL types).

mode.js : the Product database schema.

resolvers.js : all the resolvers related to the Product type. 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).

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.

schema/index.js : combine all the exported Query , Mutation , type inside index.js and export them as resolvers and typeDefs

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

I have created the simplest GraphQL TypeScript project with a standard folder structure. You can refer to.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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