我在浏览器中使用GraphQL-js。 GraphQL-js不支持在解析器中编写查询。而且内部解析器无法获取父项。
这里是一个在线示例。 https://codesandbox.io/s/xenodochial-hopper-d8vh0。
非常感谢您。
var { graphql, buildSchema } = require("graphql");
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
a: A
}
type A {
data: String
b: B
}
type B {
data: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return "Hello world!";
},
a: {
data: () => "AAA",
b: parent => {
// Why parent is empty object?
console.log(parent);
return {
data: "bbb"
};
}
},
// Why Query does't work?
Query: {
hello: () => {
return "Hello world!";
}
}
};
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, "{ hello a{data b{data}} }", root).then(response => {
document.body.innerHTML = `<pre>${JSON.stringify(response, null, 2)}</pre>`;
});