带有Graphql的nodejs [英] nodejs with Graphql
问题描述
我在尝试使用graphQL时遇到了问题。我想我在架构或解析器上做错了。一切都根据文档但仍然无法获取数据。
I am facing an issue, while trying graphQL. I guess I am doing something wrong at schema or resolver. Everything according to docs but still not able to get the data.
我需要的是非常简单的用户模型,以便在graphiql中运行查询。
What Do I need is very simple user model to run its queries in graphiql.
以下是我的代码:下载代码(zip文件) (node-7.5的代码)
然后只运行 npm install 和 npm run build 。
Here is my code: Download code (zip file) (code for node-7.5) and then just run npm install and npm run build.
主要文件:server.js,schema.js,models / user.js和resolvers / user.js。
Main files: server.js, schema.js, models/user.js and resolvers/user.js.
models / user.js
models/user.js
const typeDefinitions = `
type User {
id: String!
name: String
type: Int
isActive: Boolean
clients: [String]
}
schema {
query: User
}
`;
export default [typeDefinitions]
resolvers / user.js: -
resolvers/user.js:-
const resolverMap = {
User: {},
};
export default resolverMap
schema.js
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: -
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);
推荐答案
import { graphiqlExpress } from 'graphql-server-express';
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
您需要使用上面的代码,
You need to use above code,
graphiql:true
不再支持我想,
您需要导入graphiql方法并传递相同的端点
graphiql: true
is not supported anymore I guess,
You need to import a graphiql method and pass an endpoint for the same
这篇关于带有Graphql的nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!