遵循本教程: http://graphql.org/graphql-js/mutations-and-input-types/
+----+------------------+----------+------------+----------+-----------------------+---------------+
| id | email_name | password | date | ph_num | secret_question | secret_answer |
+----+------------------+----------+------------+----------+-----------------------+---------------+
| 1 | jsmith@gmail.com | 123456 | 20.03.2013 | 56546513 | Name of first pet? | Rex |
| | | | | | Mother's maiden name? | Anna |
| 2 | asmith@gmail.com | 6021512 | 02.06.2015 | 23169584 | Real name? | Shaquesha |
| | | | | | First car? | BMW |
| 3 | rsmith@gmail.com | 123564 | 30.07.2008 | 13546849 | Secret number? | 90321 |
+----+------------------+----------+------------+----------+-----------------------+---------------+
我可以创建教程之类的项目:
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
input MessageInput {
content: String
author: String
}
type Message {
id: ID!
content: String
author: String
}
type Query {
getMessage(id: ID!): Message
}
type Mutation {
createMessage(input: MessageInput): Message
updateMessage(id: ID!, input: MessageInput): Message
}
`);
// If Message had any complex fields, we'd put them on this object.
class Message {
constructor(id, {content, author}) {
this.id = id;
this.content = content;
this.author = author;
}
}
// Maps username to content
var fakeDatabase = {};
var root = {
getMessage: function ({id}) {
if (!fakeDatabase[id]) {
throw new Error('no message exists with id ' + id);
}
return new Message(id, fakeDatabase[id]);
},
createMessage: function ({input}) {
// Create a random id for our "database".
var id = require('crypto').randomBytes(10).toString('hex');
fakeDatabase[id] = input;
return new Message(id, input);
},
updateMessage: function ({id, input}) {
if (!fakeDatabase[id]) {
throw new Error('no message exists with id ' + id);
}
// This replaces all old data, but some apps might want partial update.
fakeDatabase[id] = input;
return new Message(id, input);
},
}
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000, () => {
console.log('Running a GraphQL API server at localhost:4000/graphql');
});
我收到回复:
mutation {
createMessage(input: {
author: "andy2",
content: "hope is a good thing2",
}) {
id
}
}
但页面中没有信息如何获取数据,我尝试了这个:
{
"data": {
"createMessage": {
"id": "d08ae7d739110c04f657"
}
}
}
但我收到错误:
{
getMessage(id:"d08ae7d739110c04f657") {
}
}
任何想法?
答案 0 :(得分:0)
您是否尝试使用图表(i)QL?
在查询中,您缺少要获取的邮件参数,如下所示:
query {
getMessage(id: "d08ae7d739110c04f657") {
content
author
}
}