簡體   English   中英

在Foxx中使用GraphiQL

[英]Using GraphiQL with Foxx

使用graphQLHTTPexpress-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:

  1. 作為獨立的應用程序: https : //github.com/skevy/graphiql-app
  2. 作為應用程序內部的React組件: https : //github.com/graphql/graphiql

如果您將其用作npm中的React組件(選項2),則實際上可以使用其他選項來自定義它,處理發送給服務器的請求等。

有一個名為ChromeiQL的Chrome擴展程序 ,該擴展程序在工具欄中添加了一個用於打開GraphiQL窗口的按鈕。 簡單易用,無需運行任何服務器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM