GraphQL project structure

10,827

Solution 1

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

Solution 2

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. e.g:

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

e.g

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

Share:
10,827
HaYa Abu Al Halawa
Author by

HaYa Abu Al Halawa

Updated on July 22, 2022

Comments

  • HaYa Abu Al Halawa
    HaYa Abu Al Halawa almost 2 years

    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
  • jastor_007
    jastor_007 over 3 years
    What do you handle in data.ts files?
  • Marco Daniel
    Marco Daniel over 3 years
    @jastor_007 it is just functions to handle data request/response from database.
  • Dan
    Dan about 3 years
    @MarcoDaniel Hi, how you connect all mutation and queries in single file?
  • Marco Daniel
    Marco Daniel about 3 years
    👋 @Dan, I connect them all in the schema.ts file. Here I have a very old example I made, it still shows the idea: github.com/MarcoDaniels/graphql-demo/blob/master/services/AP‌​I/…
  • Joshua Craven
    Joshua Craven over 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review