使用 NodeJS,可以
graphQLHTTP
从express-graphql
which 中传递,如下所示:
const {Schema} = require('./data/schema');
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
}));
有了这种配置,我们就可以使用 GraphiQL。如何使用 Foxx 实现这一目标?从这个 repo中,我可以看到 Foxx 正在使用graphql-sync
。我浏览了源代码,并在这里找到了它:
控制器.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 一起使用?如果YES,我该如何实现?有什么想法吗?
谢谢。