Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 888
HaYa Abu Al Halawa Avatar asked Jul 11 '18 12:07

HaYa Abu Al Halawa


1 Answers

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

like image 72
TheEhsanSarshar Avatar answered Sep 28 '22 04:09

TheEhsanSarshar