【问题标题】:nodejs with Graphqlnodejs 与 Graphql
【发布时间】:2017-07-11 15:53:28
【问题描述】:

我在尝试使用 graphQL 时遇到了问题。我想我在架构或解析器上做错了什么。一切都根据文档,但仍然无法获取数据。

我需要非常简单的用户模型来在 graphiql 中运行查询。

这是我的代码:Download code (zip file)(node-7.5 的代码) 然后运行 ​​npm installnpm run build

主要文件:server.js、schema.js、models/user.js 和 resolvers/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]

解析器/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);

【问题讨论】:

  • 您遇到了什么错误,请您提一下
  • 它的意思是“获取查询丢失。”。并且没有 graphiQl UI。带有一条消息的简单空白页。

标签: javascript node.js graphql


【解决方案1】:
import { graphiqlExpress } from 'graphql-server-express';
app.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
}));

你需要使用上面的代码,

graphiql: true 不再支持我猜, 您需要导入一个 graphiql 方法并为其传递一个端点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2021-06-05
    • 2018-04-08
    • 2019-02-16
    • 1970-01-01
    • 2022-09-23
    • 2017-11-29
    相关资源
    最近更新 更多