Arangodb 在Foxx中使用GraphiQL
有了NodeJS,就有了Arangodb 在Foxx中使用GraphiQL,arangodb,graphql,foxx,Arangodb,Graphql,Foxx,有了NodeJS,就有了 graphQLHTTP来自express graphql,可按如下方式传递: const {Schema} = require('./data/schema'); const graphQLApp = express(); graphQLApp.use('/', graphQLHTTP({ graphiql: true, pretty: true, schema: Schema, })); 通过这种配置,我们可以使用Graphi
graphQLHTTP
来自express graphql
,可按如下方式传递:
const {Schema} = require('./data/schema');
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
}));
通过这种配置,我们可以使用GraphiQL。如何用Foxx实现这一点?从中,我可以看到Foxx使用的是graphqlsync
。我浏览了源代码,在这里找到了它:
controller.js
'use strict';
const Foxx = require('org/arangodb/foxx');
const schema = require('./schema');
const graphql = require('graphql-sync').graphql;
const formatError = require('graphql-sync').formatError;
const ctrl = new Foxx.Controller(applicationContext);
// This is a regular Foxx HTTP API endpoint.
ctrl.post('/graphql', function (req, res) {
// By just passing the raw body string to graphql
// we let the GraphQL library take care of making
// sure the query is well-formed and valid.
// Our HTTP API doesn't have to know anything about
// GraphQL to handle it.
const result = graphql(schema, req.rawBody(), null, req.parameters);
console.log(req.parameters);
if (result.errors) {
res.status(400);
res.json({
errors: result.errors.map(function (error) {
return formatError(error);
})
});
} else {
res.json(result);
}
})
.summary('GraphQL endpoint')
.notes('GraphQL endpoint for the Star Wars GraphQL example.');
是否可以将GraphiQL与Foxx一起使用?如果是,我如何实现?有什么想法吗
谢谢。您可以将GraphiQL与任何GraphQL API一起使用,即使它没有内置到服务器中。您可以通过几种不同的方式运行GraphiQL:
如果您将它用作npm的React组件(选项2),您实际上可以使用额外的选项对其进行自定义,操作它发送给服务器的请求,等等。有一个名为Chrome的扩展,它在工具栏上添加了一个按钮,用于打开GraphiQL窗口。简单易用,无需运行任何服务器我们实际上已经研究了express graphql,并正在考虑将其直接移植到ArangoDB。在我们打电话之前,这是最好的解决方案+1.