【发布时间】:2019-10-30 13:34:02
【问题描述】:
我真的是 Graphql 的新手(实际上就在昨天)。我正在“玩耍”并尝试生态系统的各种工具(apollo-server、graphql.js ...等)。
为了试验,我试图从 nodejs 中调用查询(而不是从浏览器中的客户端,例如反应应用程序)
首先,这是我的简单架构以及解析器:
export const mySchema = gql`
type User {
id: ID!
name:
surname: String
}
# root query has been defined in another file
extend type Query {
users: [User]
test: [User]
}
`
export const myResolvers = {
users: () => [ __array_of_users__ ],
test: () => /* this is where I would like to re-invoke the 'users query'
}
使用makeExecutableSchema 函数,我用我的类型和解析器创建了一个schema 对象,并将这个模式导出到阿波罗服务器应用程序中。到目前为止一切正常。
现在在stackoverflow suggested solution 之后,我创建了一个帮助函数,它应该允许我调用在我的架构中定义的查询,如下所示:
import { graphql } from "graphql";
import { schema } from "./my-schema";
export const execute = str => {
return graphql(schema, str );
};
有了这个辅助函数,我的解析器变成了:
import { gql } from "apollo-server-express";
import { execute } from '__path_to_helper_function__';
export const myResolvers = {
users: () => [ __array_of_users__ ],
test: () => execute( gql`
query users {
name
}
`)
}
但是在操场上,当我尝试查询时:
{
test {
name
}
}
我收到以下错误:
我什至不知道我想要做什么(从节点内调用查询)是否可以完成。任何建议将不胜感激。
感谢
【问题讨论】: