Ich habe ein Problem, während Sie versuchen, GraphQL. Ich schätze, ich mache etwas falsch an Schema oder Resolver. Alles laut Dokumentation aber trotzdem nicht in der Lage die Daten zu bekommen.nodejs mit Graphql
Was brauche ich, ist sehr einfaches Benutzermodell, um seine Abfragen in graphiql auszuführen.
Hier ist mein Code: Download code (zip file) (Code für Knoten-7.5) und führen nur npm installieren und npm laufen bauen.
Hauptdateien: server.js, schema.js, models/user.js und resolvers/user.js.
Modelle/user.js
const typeDefinitions = `
type User {
id: String!
name: String
type: Int
isActive: Boolean
clients: [String]
}
schema {
query: User
}
`;
export default [typeDefinitions]
Resolvern/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);
welche Fehler passieren, werden Sie immer, Sie können Bitte erwähnen Sie –
Es sagt "Abfrage fehlt.". Und es gibt keine graphiQl UI. Einfache leere Seite mit einer Nachricht. – user1893034