[英]I am using angular translate to label or string conversion, i don't know how to translate the placeholder text
[英]Am I using GraphQL incorrectly? I don't know how it sends me back data
因此,我正在一个项目中,希望我使用GraphQL创建我的API。 我也在使用NodeJS和Express。 通常,我只是在express中设置一个端点,并使用axios从客户端调用它。 然后,在服务器的端点内部,我可以从req.body中获取信息并根据需要进行处理。 我在哪里可以使用GraphQL以相同的方式操作数据? 感觉就像我正在做的是查询数据,而不是像我想要的那样操作并发送回。
这是我所拥有的:
模式图qql:
import GraphQLDate from 'graphql-date'; const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLSchema, GraphQLList, GraphQLNonNull, } = require('graphql'); const SecretMessage = new GraphQLObjectType({ name: 'secretMessage', fields: () => ({ id: { type: GraphQLString }, name: { type: GraphQLString }, message: { type: GraphQLString }, expirDate: { type: GraphQLDate }, }), }); const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: { secretMessage: { type: SecretMessage, args: { name: { type: GraphQLString }, }, resolve(parVal, args) { return `${args.name}test`; }, }, }, }); module.exports = new GraphQLSchema({ query: RootQuery, });
我的节点服务器:
import express from 'express'; import path from 'path'; import bodyParser from 'body-parser'; import webpack from 'webpack'; const config = require('../webpack.config.js'); const expressGraphQL = require('express-graphql'); const schema = require('./schema.js'); const compiler = webpack(config); const port = process.env.PORT || 3000; const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static('public')); app.use(express.static('css')); app.use(express.static('images')); app.use('/graphql', expressGraphQL({ schema, graphiql: true, })); app.get('/', (req, res) => { res.sendFile(path.join(__dirname, '../public/src', 'index.html')); }); app.get('#', (req, res) => { console.log('Pound hashtag works'); }); app.post('passphrase', (req, res) => { console.log('passphrase tag works'); }); app.get('*', (req, res) => { res.sendFile(path.join(__dirname, '../public/src', 'index.html')); }); app.listen(port, (error) => { if (error) { console.log('This is the express error: ', error); } else { console.log('Express is listening on port: ', port); } });
axios({ method: 'post', url: '/graphql', data: JSON.stringify({ query: `secretMessage(name: "${this.state.name}") {name}`, }), }).then((response) => { console.log('This is the response from the axios call: ', response); }).catch((error) => { console.log('This is the error from the main axios call: ', error); });
当我对/ graphql进行axios post调用时,我仅收到400错误的请求错误。
字符串插值旁边的“测试”是我只是想看看我是否可以操纵那里的数据。 我不知道如何在邮递员中进行测试。 有什么想法吗?
假设您使用的是express-graphql
或类似的中间件,则可以使用POST和GET请求访问您的GraphQL端点。
使用POST:
Body
,选择x-www-form-urlencoded
query
添加为key
并将整个查询字符串添加为value
variables
键并将其作为value
包括在内(它们需要使用正确格式的JSON)。 使用GET:
query
作为key
和整个查询字符串的value
下Params
variables
参数并将其包括为value
(它们需要使用正确格式的JSON)。 另外...
如果要构建GraphQL服务器,可能会发现公开Graph i QL端点以测试查询要容易得多。 您可以在此处了解更多信息。
它烤成两个graphql-server-express
(见文档在这里 ),并express-graphql
(文件在这里 )。
编辑:至于处理您的请求中的数据:是的,您可以在解析器函数中读取请求并指定要返回的数据。 但是,每个resolve()
函数都绑定到一个特定的字段,该字段返回特定的类型。 查询和变异本身不过是“根查询”或“根变异”类型上的字段,这就是为什么它们也具有解析功能的原因。
你secretMessage
查询解析为一个类型的secretMessage
,但你它解析为一个字符串。 如果尝试运行该查询,它将始终返回null。 相反,如果希望它返回一个对象,该对象具有根据您传入的参数修改的name属性,则可以执行以下操作:
resolve(parVal, args) {
return { name: `${args.name}test` };
},
现在,如果执行如下查询:
query MyQuery {
secretMessage(name: "John") {
name
}
}
您应该回来:
{
"data": {
"secretMessage": {
"name": "Johntest"
}
}
}
您也可以为您的name
字段指定一个解析器,以实现相同的效果。 如何以及何时使用解析器取决于您要实现的目标。 更详细的解释超出了此问题的范围,但是我鼓励您深入研究正式文档并更好地了解解析器的工作方式。 然后,您可以在此处提出后续问题(如果尚未被问到!)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.