Releases: skitsanos/foxx-builder
Releases · skitsanos/foxx-builder
GraphQL Support
What's Changed
- feat: GraphQL support [beta] by @skitsanos
Creating GraphQL endpoints
Add graphql.js
to your route path as did until now with HTTP verbs (get.js
, post.js
...). The minimal set options for your handler would just exposing schema
:
module.exports = {
schema: new GraphQLSchema({...})
);
GraphQL endpoint handler example
const {GraphQLString, GraphQLSchema, GraphQLObjectType, GraphQLList} = require('graphql');
const {query} = require('@arangodb');
const AuthorType = new GraphQLObjectType({
name: 'Author',
fields: {
_key: {type: GraphQLString},
name: {type: GraphQLString}
},
resolve: (_, author) =>
{
const result = query`
FOR author IN Authors
FILTER author._key == ${author._key}
RETURN author
`;
return result.next();
}
});
const ArticleType = new GraphQLObjectType({
name: 'Article',
fields: {
_key: {type: GraphQLString},
title: {type: GraphQLString},
authorKey: {type: GraphQLString},
author: {
type: AuthorType,
resolve: (parent) =>
{
const authorKey = parent.authorKey;
const result = query`
FOR author IN Authors
FILTER author._key == ${authorKey}
RETURN author
`;
return result.next();
}
}
},
resolve: (_, article) =>
{
const result = query`
FOR author IN Authors
FILTER author._key == ${article.authorKey}}
RETURN author
`;
return result.next();
}
});
module.exports = {
schema: new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve()
{
return 'world';
}
},
getArticle: {
type: ArticleType,
args: {
_key: {type: GraphQLString}
},
resolve(_, {_key})
{
const result = query`FOR article IN Articles
FILTER article._key == ${_key}
RETURN article`;
return result.next();
}
},
getAllArticles: {
type: new GraphQLList(ArticleType),
resolve()
{
const result = query`
FOR article IN Articles
RETURN article
`;
return result.toArray();
}
}
}
})
}),
graphiql: true
};
Testing your endpoint with Hurl
POST {{URL}}/examples/graphql_demo
{
hello
getArticle(_key: "3704"){
title,
author {name}
},
getAllArticles {
title
}
}
v.2.1.20230102
- Added
auth
utility intomodule.context
withencode
,decode
andisExpired
methods to handle JWT tokens - Routes can require JWT authentication:
module.context.use((req, res, next) =>
{
if (req.path === '/' || req.path.match(/\/(login|signup)/igu))
{
next();
}
else
{
const {authorization} = req.headers;
if (!authorization)
{
res.throw(403, 'Missing authorization header');
}
const token = authorization && authorization.split(' ')[1];
try
{
const {auth} = module.context;
if (auth.isExpired(token))
{
res.throw(403, 'The token is expired');
}
next();
}
catch (e)
{
res.throw(403, e.message);
}
}
});
- CircleCI flow updated to use Node 18
v2.0.20220626
What's new:
- New, cleaner, folder structure, basically everything moved into the
src
folder. - Added
docker-compose.yml
and a few shortcuts listed inpackage.json
so developers can start making and testing their APIs on docker - Updated documentation
- Example of how to use Hurl for API testing, can be found in the
.api-test
folder. - CircleCI integration to demonstrate how to run a pipeline for API testing.
Support for running Tasks and Scripts
Example of using runTask
/**
* Run Google Analytics on each API endpoint request
*/
module.context.use((req, res, next) =>
{
const {runTask} = module.context;
runTask(
'Google Analytics PageView recording',
'ga',
{
clientId: req.headers['x-bb-client-request-uuid'],
path: req.path,
headers: req.headers
});
next();
});
Session enabled API building
Added support for Session Manager middleware that works via HTTP headers.
To enable it, just add the following into your index.js:
const sessions = require('./sessions/index');
/*sessions.allowedResources = [
...sessions.allowedResources,
'/echo'
];*/
sessions.init();
Uncomment lines to allow more API endpoints to be accessible without authentication.
Basic login, logout, and signup operations are added as well.
Bare minimum to start creating your APIs
Minimal set that required to start building ArangoDB Foxx Microservices