GraphQL Modules (fn)
UNDER HEAVY DEVELOPMENT
Motivation
Apollo's GraphQL server supports modules
which uses plain GraphQL's schema
language allowing developers to separate type definitions and resolvers by domains.
This feature should it be separated from Apollo's server so we can
use this concept with other server enabling this feature on the browser with
somehting like apollo-link-schema.
The (fn) on the name is because it has a more "functional" style than the graphql-modules.
Why not GraphQL Modules?
GraphQL Modules is a very nice battle tested, well documented and complete set of tools. But for some cases it can be an overhead for a team which just wants to organize their code in modules, no dependency injection or injectable providers. It has a strong opinion of how to use DI, how to handle context or resolvers composition. Also it has a more object (class) oriented programming which can be overwelming for some developers which prefer the functional style. Not a good option to use GraphQL on the browser since the @graphql-modules/core@0.6.6 is 55.7 kB minified + gzipped without the rest of the suite of tools like @graphql-modules/di@0.6.6 which needs the Reflect Metada api that doesn't exist in the browser yet.
Usage
Installation
npm i graphql-modules-fn
or
yarn add graphql-modules-fn
Example of a simple blog.
File: modules/user.js
const typeDefs = gql` type User { id: ID! name: String! } extend type Query { users: [User]! }` const resolvers = Query: id: '1' name: 'Sebas' id: '2' name: 'Rick' id: '3' name: 'Morty' typeDefs resolvers
File: modules/comment.js
const typeDefs = gql` type Comment { id: ID! title: String! body: String! author: User! } extend type Content { comments: [Comment]! }` const resolvers = Content: id: '1' title: 'Proident senectus' body: 'Cras varius proident senectus!' author: id: '1' name: 'Sebas' id: '2' title: 'Faucibus feugiat pulvinar quam' body: 'Consectetur soluta, incidunt semper.' author: id: '2' name: 'Rick' typeDefs resolvers
File: modules/content.js
const typeDefs = gql` type Content { id: ID! title: String! body: String } extend type User { articles: [Content]! }` const resolvers = User: id: '1' title: 'Sapiente quidem architecto' body: 'Augue tempora excepteur, cras varius proident senectus minima fuga proident temporibus fuga!' id: '2' title: 'Fuga curae illum suscipit eget' body: 'Faucibus feugiat pulvinar quam, consectetur soluta, incidunt semper! Nobis ipsum, aliquid excepteur.' typeDefs resolvers
File: createSchema.js
const modules = user comment content { return //=> { schema, context }}
File: createServer.js
{ const schema context = await return schema context }
File: index.js
const PORT = 3000 } = processenv const server =
More examples:
- Simple blog using Apollo's graphql server.
- GraphQL on the browser with
apollo-link-schema
.
Running GraphQL in the browser
Base modules
10.3kb
| @apollographql/apollo-tools40.9kb
| graphql3.7kb
| apollo-link-schema984b
| graphql-tag2.5kb
| react-apollo-hooks16kb
| apollo-client9.7kb
| apollo-cache-inmemory
Bundle size: ~84kb
.
GraphQL Modules (fn)
1.1kb
| graphql-modules-fn
Bundle size: ~85kb
.
Comparing using @graphql-modules/core
55.2kb
| @graphql-modules/core
Bundle size: ~139kb
. Incrementing ~40% of your bundle size.
TODO:
- Add usage of contexts.
- Start a documentation site.
- Subscriptions support (waiting for PR #1047 to be merged).
- Tests with 100% of converage.