2017-02-21 37 views
0

我正在嘗試graphQL時遇到問題。我想我在模式或解析器上做錯了什麼。根據文檔的一切,但仍然無法獲得數據。nodejs與Graphql

我需要什麼是非常簡單的用戶模型來在graphiql中運行它的查詢。

這裏是我的代碼:Download code (zip file)(節點-7.5代碼) 然後只需運行NPM安裝NPM運行建立

主要文件:server.js,schema.js,models/user.js和resolvers/user.js。

型號/ user.js的

const typeDefinitions = ` 
type User { 
    id: String! 
    name: String 
    type: Int 
    isActive: Boolean 
    clients: [String] 
} 
    schema { 
    query: User 
} 
`; 
export default [typeDefinitions] 

解析器/ user.js的: -

const resolverMap = { 
    User: {}, 
}; 
export default resolverMap 

schema.js

import { makeExecutableSchema } from 'graphql-tools'; 
import resolver from '../resolvers/user' 
import User from './user'; 

export default makeExecutableSchema({ 
    typeDefs: User, 
    resolvers: resolver, 
}); 

server.js: -

import express from 'express'; 
import bodyParser from 'body-parser'; 
import routes from './app/routers/index'; 
import { graphqlExpress } from 'graphql-server-express'; 
import schema from './app/models/schema' 

var app = express(); // create our app w/ express 

var database = require('./app/configs/database'); 
var port = process.env.PORT || 8888; // set the port 

app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users 
app.use(morgan('dev')); // log every request to the console 
app.use(bodyParser.urlencoded({ 'extended': 'true' })); // parse application/x-www-form-urlencoded 
app.use(bodyParser.json()); // parse application/json 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json 


app.use('/graphql', bodyParser.json(), graphqlExpress({ 
    graphiql: true, 
    pretty: true, 
    schema: schema 
})); 

app.listen(port); 
console.log("App listening on port : " + port); 
+0

什麼樣的錯誤你好嗎,你能請提及 –

+1

這是說「Get query missing。」。並沒有圖形用戶界面。帶一條消息的簡單空白頁面。 – user1893034

回答

0
import { graphiqlExpress } from 'graphql-server-express'; 
app.use('/graphiql', graphiqlExpress({ 
    endpointURL: '/graphql', 
})); 

你需要使用上面的代碼,

graphiql: true不支持了我猜想, 您需要導入graphiql方法和傳遞終點爲同