GraphQL 字段作为函数
GraphQL fields as a function
我正在研究 GraphQL
,在编写 GraphQLObjectType
的 fields
时,我对特定问题的不同实现感到有点困惑。
这两种实现有什么区别?
1.
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: { // as object
echo: {
type: GraphQLString,
args: {
email: { type: EmailType }
},
resolve: (root, {email}) => {
return email;
}
}
}
})
});
var ComplicatedArgs = new GraphQLObjectType({
name: 'ComplicatedArgs',
fields: () => ({ // as function
complexArgField: {
type: GraphQLString,
args: {
complexArg: { type: ComplexInput }
},
}
}),
});
当你需要进行循环引用时。
在这里寻找我的类似答案
这是 CLOSURE 的一个很好的例子。假设您在一个文件中有两种类型并且它们相互引用。
const BookType= new GraphQLObjectType({
name: 'BookType',
fields: { // as object
author: {
type: AuthorType,
resolve: (parentValue, args) => {
// query the author based on your db implementation.
}
} }
})
BookType 有一个作者字段并引用 AuthorType。现在假设您在引用 BookType
的“BookType”下定义了 AuthorType
const AuthorType= new GraphQLObjectType({
name: 'AuthorType',
fields: { // as object
books: {
type: new GraphQLList(BookType), //one author might have multiple books
resolve: (parentValue, args) => {
// query the books based on your db implementation.
}
} }
})
所以当Javascript 引擎需要使用BookType
时,它会看到fields.author.type
是AuthorType 而AuthorType 没有在上面定义。所以它会给
reference error:AuthorType is not defined
为了避免这种情况,我们将字段转换为函数。这个函数是一个 CLOSURE 函数。这是为什么闭包如此有用的一个很好的例子。
js引擎首先读取文件时,会将函数内部引用的所有变量保存到内存堆中,作为该函数的闭包存储。 BookType.fields
需要的所有变量都存储在 BookType.fields() 的闭包环境中。所以现在如果 javascript 执行 Booktype.fields(),它会检查函数内部是否定义了“AuthorType”,它没有定义所以它会检查它的闭包存储,AuthorType一开始已经存储在那里,所以它使用它。
我正在研究 GraphQL
,在编写 GraphQLObjectType
的 fields
时,我对特定问题的不同实现感到有点困惑。
这两种实现有什么区别?
1.
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: { // as object
echo: {
type: GraphQLString,
args: {
email: { type: EmailType }
},
resolve: (root, {email}) => {
return email;
}
}
}
})
});
var ComplicatedArgs = new GraphQLObjectType({
name: 'ComplicatedArgs',
fields: () => ({ // as function
complexArgField: {
type: GraphQLString,
args: {
complexArg: { type: ComplexInput }
},
}
}),
});
当你需要进行循环引用时。
在这里寻找我的类似答案
这是 CLOSURE 的一个很好的例子。假设您在一个文件中有两种类型并且它们相互引用。
const BookType= new GraphQLObjectType({
name: 'BookType',
fields: { // as object
author: {
type: AuthorType,
resolve: (parentValue, args) => {
// query the author based on your db implementation.
}
} }
})
BookType 有一个作者字段并引用 AuthorType。现在假设您在引用 BookType
的“BookType”下定义了 AuthorTypeconst AuthorType= new GraphQLObjectType({
name: 'AuthorType',
fields: { // as object
books: {
type: new GraphQLList(BookType), //one author might have multiple books
resolve: (parentValue, args) => {
// query the books based on your db implementation.
}
} }
})
所以当Javascript 引擎需要使用BookType
时,它会看到fields.author.type
是AuthorType 而AuthorType 没有在上面定义。所以它会给
reference error:AuthorType is not defined
为了避免这种情况,我们将字段转换为函数。这个函数是一个 CLOSURE 函数。这是为什么闭包如此有用的一个很好的例子。
js引擎首先读取文件时,会将函数内部引用的所有变量保存到内存堆中,作为该函数的闭包存储。 BookType.fields
需要的所有变量都存储在 BookType.fields() 的闭包环境中。所以现在如果 javascript 执行 Booktype.fields(),它会检查函数内部是否定义了“AuthorType”,它没有定义所以它会检查它的闭包存储,AuthorType一开始已经存储在那里,所以它使用它。