Explore src/schema/
to see how the files are organized.
This design adheres to 3 guidelines:
-
The server should be modularized by type.
-
Each type and field is a module and should:
-
be represented by a directory or file with the same name as the type or field
-
expose a default export which implements the following standard interface:
interface GraphQLModule { typeDefs: DocumentNode | RecursiveArray<DocumentNode>; resolvers?: Resolvers | RecursiveArray<Resolvers>; }
Note: If inheritResolversFromInterfaces is enabled, only custom fields need their own resolvers and typeDefs.
-
-
A type directory can be nested within another type directory as long as it isn’t used outside of the directory in which it’s nested. If a type is used only within a single field, that field’s file can be turned into a directory which contains the type directory and an index file which exports the field resolver and typeDefs.
The result of these guidelines is a GraphQL server structured in roughly the same way as the schema it generates, with some convenient nesting where possible. I recommend using this server design alongside a type generation solution (such as GraphQL Code Generator) and makeExecutableSchema
(with inheritResolversFromInterfaces: true
).
This design can be applied flexibly in most any GraphQL server project, federated or not.
To generate types:
npm run generate
To generate types and print the schema in the terminal:
npm run generate:print
To compile the app:
npm run build
To run the server:
-
Execute
dev
scriptnpm run dev
-
Navigate to
http://localhost:4000
-
Click "Query your server"
-
Explore the schema and run queries