decapi
?decapi is set of decorators allowing creating GraphQL APIs quickly and in type-safe way.
Schema is main building block of any graphql
schema. It'll join all parts of our api together.
In decapi
to create schema, we need to pass a class decorated with @SchemaRoot
to compileSchema
function
compiledSchema
from example above is standard, regular graphql
schema.
You can also pass an array of classes decorated with @SchemaRoot
-any non trivial app will probably require more than one class to expose all it's API root logic.
Any working schema requires at least one Query field. There are special decorators - @Query
and @Mutation
used to register root fields of schema.
Very simple fully working schema like
Could be implemented as:
Let's add some customization to our schema:
With tiny change in our code:
For now, our query field returned scalar (string). Let's return something more complex. Schema will look like:
Such query will have a bit more code and here it is:
Since now, decapi
was able to guess type of every field from typescript type definitions.
There are, however, some cases where we'd have to define them explicitly.
Promise<SomeType>
while field itself is typed as SomeType
Reflect
api is not able to guess type of single array item. This might change in the future)Let's modify our Product
so it has additional categories
field that will return array of strings. For the sake of readability, let's ommit all fields we've defined previously.
We've added { type: [String] }
as @Field
options. Type can be anything that is resolvable to GraphQL
type
String
, Number
, Boolean
.graphql
eg. GraphQLFloat
or any type from external graphql library etc@ObjectType
[String]
or [GraphQLFloat]
Every field function we write can be async
and return Promise
. Let's say, instead of hard-coding our categories, we want to fetch it from some external API:
Important! setup steps are simple, but required. Make sure to check setup section.