[英]Using GraphiQL with Foxx
使用graphQLHTTP
, express-graphql
,可以通過以下方式進行傳遞:
const {Schema} = require('./data/schema');
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
}));
通過這種配置,我們可以使用GraphiQL。 如何用Foxx實現? 從這個 graphql-sync
,我可以看到Foxx正在使用graphql-sync
。 我瀏覽了源代碼,並在這里找到了它:
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),則實際上可以使用其他選項來自定義它,處理發送給服務器的請求等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.