GraphQL with FeathersJS

In this article I will write about how the new @feathers-plus/cli can speed up development of a GraphQL service by coupling it with FeathersJS services. Why would I want to use GraphQL with FeathersJS you might ask… Well GraphQL gives you great flexibility in requesting only the right data when it’s needed and GraphQL allows building fast service orientated applications from the get go. The two combined are a force to be reckoned with.

The @feathers-plus/cli allows you to generate your services using the JSON-schema convention for your models. This allows you to regenerate your services with a different db adapter with a single command.

Screenshot for creating a service with the new cli

The above screenshot shows a very similar interface to the existing cli. You will need to run feathers-plus generate all once you have defined your schema definitions, this will generate the required model file for your chosen db adapter.

The @feathers-plus/cli allows you to generate your services using the json schema convention for your models. This allows you to regenerate your services with a different db adapter with a single command.

To get started with using GraphQL and FeathersJS you should created all the services you require, then run feathers-plus generate graphql to create the GraphQL endpoint. Be sure to uncomment lines 6–9 in the below block which will be located within your <service>.schema.js file, without those lines the cli will not create the GraphQL resolvers so you will get errors when requesting against the endpoint.

It’s important to note on line 24 above we are defining the relation to the posts schema, this will be used by the cli to create the correct GraphQL loaders, in my case it will generate BatchLoaders.

GraphiQL

I like to use graphiql to interact with the GraphQL endpoint for development, here are a couple screenshots of it in action:

Here we are just listing users, by default it creates a find and a get top level query for each schema. Here it’s called findUser which will either do a list, or can be used in conjunction with a query like show here.

With this screenshot we can see the get operator being used. This allows you to find a record by its id.

It is possible to query nested properties but I think I will leave that for another post or you can look through the extensive docs here.

From a few commands it is very easy to get a GraphQL endpoint setup using FeathersJS. You can still use hooks within your services, GraphQL just enables a flexible approach to querying only the required data when needed.

Demo project