2016-06-24 32 views
2

Mit NodeJS gibt graphQLHTTP von express-graphql ist, die wie folgt übergeben werden können:Mit GraphiQL mit Foxx

const {Schema} = require('./data/schema'); 
    const graphQLApp = express(); 
    graphQLApp.use('/', graphQLHTTP({ 
    graphiql: true, 
    pretty: true, 
    schema: Schema, 
    })); 

Mit dieser Art von Konfiguration können wir GraphiQL verwenden. Wie erreiche ich das mit Foxx? Von this repo konnte ich sehen, dass Foxx graphql-sync stattdessen verwendet. Ich durchsucht den Quellcode und ich fand es hier:

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.'); 

Ist es möglich, GraphiQL mit Foxx zu benutzen? Wenn JA, wie erreiche ich das? Irgendwelche Gedanken?

Danke.

Antwort

2

Sie können GraphiQL mit jeder GraphQL-API verwenden, auch wenn sie nicht in den Server integriert ist. Sie können GraphiQL sich auf verschiedene Weise ausführen:

  1. Als Standalone-Anwendung: https://github.com/skevy/graphiql-app
  2. Als Reaktion Komponente in Ihrer Anwendung: https://github.com/graphql/graphiql

Wenn Sie es als eine von npm Reagieren Komponente verwenden (Option 2), Sie können es tatsächlich mit zusätzlichen Optionen anpassen, die Anforderungen bearbeiten, die es an den Server sendet, und mehr.

+0

Wow, das ist cool. Vielen Dank .. :) – asubanovsky

+0

Wir haben uns Express-Graphql angeschaut und überlegt, es direkt zu ArangoDB zu portieren. Bis wir diesen Anruf tätigen, ist dies die beste Lösung. +1 –

1

Es gibt eine Chrome-Erweiterung namens ChromeiQL, die der Symbolleiste zum Öffnen des GraphiQL-Fensters eine Schaltfläche hinzufügt. Einfach und leicht, keine Server zu betreiben