Graphql错误未定义添加关系 [英] Graphql Error undefined adding a relationship
问题描述
我有以下结构.每个帐户可以有一种安全类型.
因此,从SecurityType到Account是一对多的.使用代码一切正常
I have the following structure. Every Account can have one security type.
So its a one-to-many from SecurityType to Account.
Everything works using the code
File: AccountSchema.js
const SecurityType = require('./LookupSchema').SecurityType;
console.log(Account);
const Account = new GraphQLObjectType({
name: 'Account',
description: 'Account access',
fields: () =>
({
id: {
type: GraphQLString
},
security_type:
{
type: SecurityType,
resolve(parent, args, ast){
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [parent.security_type_id];
db.query(db.connection, `SELECT * FROM lookups.security_type WHERE id = $1`, parameters)
.then(result =>
{
resolve(result.entrys.rows[0]);
})
.catch(err =>
{
reject(err.message);
});
});
}
}
})
});
module.exports = {
Account : Account
}
File: LookupSchema.js
const Account = require('./AccountSchema').Account;
console.log(Account);
const SecurityType = new GraphQLObjectType({
name: 'SecurityType',
description: 'Used to for specifying security type',
fields: () =>
({
id: {
type: GraphQLString
}
})
});
module.exports = {
SecurityType: SecurityType
}
File: Query.js
const Query = new GraphQLObjectType({
name: 'Query',
description: 'Root query object',
fields: () => ({
accounts: {
type: new GraphQLList(Account),
resolve(root, args, ast) {
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [];
db.query(db.connection, `SELECT * FROM accounts.account`, parameters)
.then(result =>
{
console.log(result);
resolve(result.entrys.rows);
})
.catch(err =>
{
console.log(err);
reject(err.message);
});
});
}
},
securityTypes: {
type: new GraphQLList(SecurityType),
resolve(root){
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [];
db.query(db.connection, `SELECT * FROM lookups.security_type`, parameters)
.then(result =>
{
resolve(result.entrys.rows);
})
.catch(err =>
{
reject(err.message);
});
});
}
}
})
});
我遇到的问题是将帐户添加到LookupSchema.js文件中
The problem I have is when I add to the file LookupSchema.js the accounts
const SecurityType = new GraphQLObjectType({
name: 'SecurityType',
description: 'Used to for specifying security type',
fields: () =>
({
id: {
type: GraphQLString
},
accounts: {
type: new GraphQLList(Account),
resolve(parent, args, ast){
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [parent.id];
db.query(db.connection, `SELECT * FROM accounts.account WHERE security_type_id = $1`, parameters)
.then(result =>
{
resolve(result.entrys.rows);
})
.catch(err =>
{
reject(err.message);
});
});
}
}
})
});
启动服务时出现以下错误
I get the following error when I start the service
错误:只能创建GraphQLType的列表,但得到:未定义.
Error: Can only create List of a GraphQLType but got: undefined.
我为每个Account和SecurityType放置console.log来检查导入,并且我在LookupSchema中注意到,Account是未定义的.我做了一些研究,这可能是一个循环的问题,但不太确定解决方案.
I put console.log for each Account and SecurityType to check for the import and I noticed in LookupSchema, Account is undefined. I did some research and this might be a circular issue but not quite sure a solution for it.
任何建议将不胜感激
推荐答案
要避免出现循环问题,可以在 fields()
函数内部使用require函数.
To avoide the Cyclic Problem you can use the require function inside the fields()
function.
因此,在 AccountSchema.js
fields:()
函数内部,将首先 import
SecurityType 将与 return {}
一起使用其他字段,与其他文件相同.
So, Inside AccountSchema.js
fields:()
function will first import
the SecurityType
then only we will be using the the other fields with the return {}
, same for other files.
AccountSchema.js
AccountSchema.js
const {
GraphQLObjectType,
GraphQLString,
} = require('graphql');
const Account = new GraphQLObjectType({
name: 'Account',
description: 'Account access',
fields: () => {
const SecurityType = require('./LookUpSchema');
return {
id: {
type: GraphQLString,
},
security_type:
{
type: SecurityType,
resolve(parent, args, ast) {
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [parent.security_type_id];
db.query(db.connection, 'SELECT * FROM lookups.security_type WHERE id = $1', parameters)
.then((result) => {
resolve(result.entrys.rows[0]);
})
.catch((err) => {
reject(err.message);
});
});
},
},
};
},
});
module.exports = Account;
LookUpSchema.js
LookUpSchema.js
const {
GraphQLObjectType,
GraphQLString,
GraphQLList,
} = require('graphql');
const SecurityType = new GraphQLObjectType({
name: 'SecurityType',
description: 'Used to for specifying security type',
fields: () => {
const Account = require('./AccountSchema');
return {
id: {
type: GraphQLString,
},
accounts: {
type: new GraphQLList(Account),
resolve(parent, args, ast) {
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [parent.id];
db.query(db.connection, 'SELECT * FROM accounts.account WHERE security_type_id = $1', parameters)
.then((result) => {
resolve(result.entrys.rows);
})
.catch((err) => {
reject(err.message);
});
});
},
},
};
},
});
module.exports = SecurityType;
Query.js
const {
GraphQLList,
GraphQLObjectType,
GraphQLSchema,
} = require('graphql');
const Account = require('./AccountSchema');
const SecurityType = require('./LookUpSchema');
console.log('Account', Account);
const Query = new GraphQLObjectType({
name: 'Query',
description: 'Root query object',
fields: () => ({
accounts: {
type: new GraphQLList(Account),
resolve(root, args, ast) {
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [];
db.query(db.connection, 'SELECT * FROM accounts.account', parameters)
.then((result) => {
console.log(result);
resolve(result.entrys.rows);
})
.catch((err) => {
console.log(err);
reject(err.message);
});
});
},
},
securityTypes: {
type: new GraphQLList(SecurityType),
resolve(root) {
return new Promise((resolve, reject) => {
const db = ast.db;
const parameters = [];
db.query(db.connection, 'SELECT * FROM lookups.security_type', parameters)
.then((result) => {
resolve(result.entrys.rows);
})
.catch((err) => {
reject(err.message);
});
});
},
},
}),
});
const schema = new GraphQLSchema({
query: Query,
// mutation: MutationType,
});
module.exports = schema;
GraphiQL
这篇关于Graphql错误未定义添加关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!