This example uses GraphQL Code Generator custom templates feature, to generate GraphQL resolvers based on the GraphQL schema, using MongoDB.
- Start by cloning this project, then run
yarn
to install the project's dependencies. - Generate the resolvers code according to your GraphQL Schema by running
yarn generate
- Execute the server by running
yarn start
- Open GraphiQL (default to:
http://localhost:3000/graphiql
) to test the GraphQL endpoint.
The schema defines a custom GraphQL Directives to declare links, mapping and DB object structure between the GraphQL schema entites, then, using a custom GraphQL Code Generator template, we generate a custom TypeScript file with a resolver, that access the DB and fetches the data.
These directives are available in this example:
-
@resolvers
- decalres that this GraphQLtype
is a root type, and it's fields and plain queries (put it overQuery
type) -
@query
- goes with GraphQL field inside a type with@resolvers
: declares the query that needed to be executed (including skip, limit and custom MongoDB cursor) -
@entity
- declares that this GraphQLtype
comes the the DB, and the generator should generate Type resolvers for it -
@map
- goes with GraphQL field inside a type with@entity
: declare the mapping between the MongoDB field to GraphQL field, for exampleid
->_id
-
@util
- goes with GraphQL field inside a type with@entity
: declares either a context function to execute or a simple MomentJS formating for Date -
@link
- goes with GraphQL field inside a type with@entity
: declares a MongoDB query that needed to execute in order to fetch the object, use it for table links
See
src/typedefs/schema.ts
for the GraphQL definitions and GraphQL Schema.