问题描述
我们正在中继中形成查询.我们的用户数据库设置如下:
We are forming a query in relay. We have user database set as follows:
function User(id, name, des) {
this.id = id.toString()
this.name = name
this.des = des
}
var users = [new User(1, 'abc', 'Hello abc'), new User(2, 'xyz', 'Hello xyz')]
module.exports = {
User: User,
getAnonymousUser: function() {return users[0] }
}
我们的schema.js文件如下:
Our schema.js file is as follows:
var nodeDefinitions = GraphQLRelay.nodeDefinitions(function(globalId) {
var idInfo = GraphQLRelay.fromGlobalId(globalId)
if (idInfo.type == 'User') {
return db.getUser(idInfo.id)
}
return null
})
var userType = new GraphQL.GraphQLObjectType({
name: 'User',
description: 'A person who uses our app',
isTypeOf: function(obj) { return obj instanceof db.User },
fields: function() {
return {
id: GraphQLRelay.globalIdField('User'),
des: {
type: GraphQL.GraphQLString,
description: 'The des of the user',
},
name: {
type: GraphQL.GraphQLString,
description: 'The name of the user',
}
}
},
interfaces: [nodeDefinitions.nodeInterface],
})
module.exports = new GraphQL.GraphQLSchema({
query: new GraphQL.GraphQLObjectType({
name: 'Query',
fields: {
node: nodeDefinitions.nodeField,
user: {
type: userType,
resolve: function() { return db.getAnonymousUser() },
},
},
}),
})
我们将中继容器创建为:
we have created our relay container as:
exports.Container = Relay.createContainer(App, {
fragments: {
user: () => Relay.QL`
fragment on User {
name
}
`,
},
})
exports.queries = {
name: 'AppQueries',
params: {
userID: '1',
},
queries: {
//user: () => Relay.QL`query { user }`,
user: () => Relay.QL `query { user(id: $userID) }`
},
}
但是我们无法通过userId获取用户,并且在运行npm run build命令时出现以下错误:
But we are not able to get the user by userId and getting following error on running npm run build command:
错误:类型为查询"的字段用户"上的未知参数"id".档案:App.js来源:>
Error: Unknown argument "id" on field "user" of type "Query".File: App.jsSource:>
----------------------- 在此处输入代码
-----------------------enter code here
有人可以在这个问题上帮助我们吗?
Can someone help us on this issue?
推荐答案
您的架构未为用户字段定义任何参数:
Your schema doesn't define any arguments for the user field:
user: {
type: userType,
resolve: function() { return db.getAnonymousUser() },
},
要按ID提取用户,请定义 id
参数,然后按该ID提取用户:
To fetch user by ID, define the id
argument, and fetch the user by that ID:
user: {
args: {
id: { type: GraphQLString }
},
resolve: function(root, args) {
return db.findUserById(args.id); // you don't have this method but it's an example of how to use the arg
}
}
这篇关于收到错误:未知参数"id"现场“用户"类型为“查询"的[GraphQL,中继,React]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!