Reactjs 从react向graphql传递数据时出现问题
我有用户名和密码,我想从前端的react发送到graphql后端。我用的是阿波罗客户机 在我的客户方面,我有这个Reactjs 从react向graphql传递数据时出现问题,reactjs,graphql,apollo-client,Reactjs,Graphql,Apollo Client,我有用户名和密码,我想从前端的react发送到graphql后端。我用的是阿波罗客户机 在我的客户方面,我有这个 const REGISTER_USER = gql` mutation RegisterUser($username: String!, $password: String!) { registerUser(username: $username, password: $password) { username, password }
const REGISTER_USER = gql`
mutation RegisterUser($username: String!, $password: String!) {
registerUser(username: $username, password: $password) {
username,
password
}
}
`;
我这样称呼它
registerUser({ variables: { username: values.username, password: values.password } })
在我的服务器上
const UserType = new GraphQLObjectType({
name: 'user',
fields: () => ({
username: { type: GraphQLString },
password: { type: GraphQLString }
})
});
const Mutations = new GraphQLObjectType({
name: 'Mutation',
fields: {
registerUser: {
type: UserType,
args: {
username: { type: GraphQLString },
password: { type: GraphQLString }
},
resolve(username, password) {
console.log(username, password, 123)
}
},
}
})
问题是-我将密码和用户名分别作为字符串传递,但我的console.log(username,password,123)
在变体中返回未定义的和带有用户名,密码字段的完整对象
我不确定我哪里做错了什么。
非常感谢所有帮助。解析函数具有源代码、参数、上下文和信息作为参数。您可以在中阅读更多关于它的信息
所以基本上你要求的是源代码和参数,这就是为什么一个是未定义的
,另一个是参数的对象。在解析器中获取变量的方法是:
const Mutations = new GraphQLObjectType({
name: 'Mutation',
fields: {
registerUser: {
type: UserType,
args: {
username: { type: GraphQLString },
password: { type: GraphQLString }
},
resolve: (source, args) {
console.log(args.username, args.password)
// HERE: you can access your arguments input as part of the 'args' object
}
},
}
})
希望有帮助。那么,解析函数的签名应该是什么?为什么您希望将用户名和密码作为参数传递给它?您可以找到API引用@DanielRearden Well我传递了两个字符串,我的参数在突变中都是类型:GraphQLString
。还是因为我在函数中设置了type:UserType,