GraphQL 连接
GraphQL Connections
我与用户及其成员之间存在这种关系。 memberships 字段是一个用户和多个memberships 之间的连接。
const MembershipType = new GraphQLObjectType({
name: 'Membership',
interfaces: [nodeInterface],
fields: {
id: globalIdField(),
user: {
type: new GraphQLNonNull(UserType),
resolve(parent, args, ctx: Context) {
return ctx.userById.load(parent.user_id);
},
},
role: {
type: new GraphQLNonNull(GraphQLString),
},
team: {
type: GraphQLString,
},
createdAt: {
type: new GraphQLNonNull(GraphQLString),
resolve(parent) {
return parent.created_at;
},
},
updatedAt: {
type: new GraphQLNonNull(GraphQLString),
resolve(parent) {
return parent.updated_at;
},
},
},
});
export default MembershipType;
const UserType = new GraphQLObjectType({
name: 'User',
interfaces: [nodeInterface],
fields: {
id: globalIdField(),
firstName: {
type: GraphQLString,
resolve(parent) {
return parent.first_name;
},
},
lastName: {
type: GraphQLString,
resolve(parent) {
return parent.last_name;
},
},
email: {
type: GraphQLString,
resolve(parent) {
return parent.email;
},
},
memberships: {
type: new GraphQLList(MembershipType),
resolve(parent, args, ctx: Content) {
return ctx.membershipsByUserId.load(parent.id);
},
},
membershipsCount: {
type: new GraphQLNonNull(GraphQLInt),
resolve(parent, args, ctx: Context) {
return ctx.userMembershipsCount.load(parent.id);
},
},
},
});
export default UserType;
这里的问题是我有这个错误,其中 MembershipType 中的用户字段导致错误
Blockquote
/Users/nizarayari/Arise/arise-auth/node_modules/graphql/jsutils/invariant.js:19
throw new Error(message);
^
Error: Expected undefined to be a GraphQL nullable type.
at invariant (/Users/nizarayari/Arise/arise-auth/node_modules/graphql/jsutils/invariant.js:19:11)
at assertNullableType (/Users/nizarayari/Arise/arise-auth/node_modules/graphql/type/definition.js:261:51)
at new GraphQLNonNull (/Users/nizarayari/Arise/arise-auth/node_modules/graphql/type/wrappers.js:79:54)
at Object. (/Users/nizarayari/Arise/arise-auth/src/schema/membership/MembershipType.js:25:13)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Module.require (module.js:568:17)
at require (internal/module.js:11:18)
at Object. (/Users/nizarayari/Arise/arise-auth/src/schema/user/UserType.js:19:1)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
这是我对用户的查询
import UserType from './UserType';
import type Context from '../../Context';
const me = {
type: UserType,
resolve(root: any, args: any, ctx: Context) {
return ctx.user && ctx.userById.load(ctx.user.id);
},
};
export default {
me,
};
没有成员查询只是突变
您可能需要将文件中的 UserType
移动到 MembershipType
上方。
let Foo = {
bar: Bar
}
let Bar = 'bar'
console.log(Foo.bar) // undefined
Foo = {
bar: Bar
}
console.log(Foo.bar) // 'bar'
两个字段是否在同一个js文件中?您在看起来像同一个 js 文件的 Memberships 和 UserType 上都有 export default
。
您只能在每个文件的一个字段上使用 export default
。
将两个字段分开后,由于 Membership 取决于 UserType,技巧是使用 returns Membership
中的字段对象的函数
const MembershipType = new GraphQLObjectType({
name: 'Membership',
interfaces: [nodeInterface],
fields: () => ({
id: globalIdField(),
...
})
});
而不是:
const MembershipType = new GraphQLObjectType({
name: 'Membership',
interfaces: [nodeInterface],
fields: {
id: globalIdField(),
...
}
});
我与用户及其成员之间存在这种关系。 memberships 字段是一个用户和多个memberships 之间的连接。
const MembershipType = new GraphQLObjectType({
name: 'Membership',
interfaces: [nodeInterface],
fields: {
id: globalIdField(),
user: {
type: new GraphQLNonNull(UserType),
resolve(parent, args, ctx: Context) {
return ctx.userById.load(parent.user_id);
},
},
role: {
type: new GraphQLNonNull(GraphQLString),
},
team: {
type: GraphQLString,
},
createdAt: {
type: new GraphQLNonNull(GraphQLString),
resolve(parent) {
return parent.created_at;
},
},
updatedAt: {
type: new GraphQLNonNull(GraphQLString),
resolve(parent) {
return parent.updated_at;
},
},
},
});
export default MembershipType;
const UserType = new GraphQLObjectType({
name: 'User',
interfaces: [nodeInterface],
fields: {
id: globalIdField(),
firstName: {
type: GraphQLString,
resolve(parent) {
return parent.first_name;
},
},
lastName: {
type: GraphQLString,
resolve(parent) {
return parent.last_name;
},
},
email: {
type: GraphQLString,
resolve(parent) {
return parent.email;
},
},
memberships: {
type: new GraphQLList(MembershipType),
resolve(parent, args, ctx: Content) {
return ctx.membershipsByUserId.load(parent.id);
},
},
membershipsCount: {
type: new GraphQLNonNull(GraphQLInt),
resolve(parent, args, ctx: Context) {
return ctx.userMembershipsCount.load(parent.id);
},
},
},
});
export default UserType;
这里的问题是我有这个错误,其中 MembershipType 中的用户字段导致错误
Blockquote /Users/nizarayari/Arise/arise-auth/node_modules/graphql/jsutils/invariant.js:19 throw new Error(message); ^ Error: Expected undefined to be a GraphQL nullable type. at invariant (/Users/nizarayari/Arise/arise-auth/node_modules/graphql/jsutils/invariant.js:19:11) at assertNullableType (/Users/nizarayari/Arise/arise-auth/node_modules/graphql/type/definition.js:261:51) at new GraphQLNonNull (/Users/nizarayari/Arise/arise-auth/node_modules/graphql/type/wrappers.js:79:54) at Object. (/Users/nizarayari/Arise/arise-auth/src/schema/membership/MembershipType.js:25:13) at Module._compile (module.js:624:30) at Object.Module._extensions..js (module.js:635:10) at Module.load (module.js:545:32) at tryModuleLoad (module.js:508:12) at Function.Module._load (module.js:500:3) at Module.require (module.js:568:17) at require (internal/module.js:11:18) at Object. (/Users/nizarayari/Arise/arise-auth/src/schema/user/UserType.js:19:1) at Module._compile (module.js:624:30) at Object.Module._extensions..js (module.js:635:10) at Module.load (module.js:545:32) at tryModuleLoad (module.js:508:12)
这是我对用户的查询
import UserType from './UserType';
import type Context from '../../Context';
const me = {
type: UserType,
resolve(root: any, args: any, ctx: Context) {
return ctx.user && ctx.userById.load(ctx.user.id);
},
};
export default {
me,
};
没有成员查询只是突变
您可能需要将文件中的 UserType
移动到 MembershipType
上方。
let Foo = {
bar: Bar
}
let Bar = 'bar'
console.log(Foo.bar) // undefined
Foo = {
bar: Bar
}
console.log(Foo.bar) // 'bar'
两个字段是否在同一个js文件中?您在看起来像同一个 js 文件的 Memberships 和 UserType 上都有 export default
。
您只能在每个文件的一个字段上使用 export default
。
将两个字段分开后,由于 Membership 取决于 UserType,技巧是使用 returns Membership
中的字段对象的函数const MembershipType = new GraphQLObjectType({
name: 'Membership',
interfaces: [nodeInterface],
fields: () => ({
id: globalIdField(),
...
})
});
而不是:
const MembershipType = new GraphQLObjectType({
name: 'Membership',
interfaces: [nodeInterface],
fields: {
id: globalIdField(),
...
}
});