Skip to content

GraphQL Support

Latest
Compare
Choose a tag to compare
@skitsanos skitsanos released this 14 Apr 13:35
· 20 commits to master since this release
1f09ce0

What's Changed

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
    }
}