简单的graphQL查询给出错误消息
我正在尝试使用一个基于id的GET请求。以下是代码:简单的graphQL查询给出错误消息,graphql,angularjs,node.js,Graphql,Angularjs,Node.js,我正在尝试使用一个基于id的GET请求。以下是代码: const { graphql, buildSchema } = require('graphql'); EmployeeService.prototype.getEmployee = function() { // Construct a schema const schema = buildSchema(` type Query { employee(id="12345") { id
const { graphql, buildSchema } = require('graphql');
EmployeeService.prototype.getEmployee = function() {
// Construct a schema
const schema = buildSchema(`
type Query {
employee(id="12345") {
id
items {
id
name
}
}
}
`);
// The root provides a resolver function
let root = {
employee: () => id
};
// Run the GraphQL query
graphql(schema, '{ employee }', root).then((response) => {
console.log(response);
});
};
正在尝试按照上的文档进行操作。
我得到一个GraphQL错误:语法错误GraphQL请求3:19预期:,找到=↵↵2:类型查询{↵3:员工id=12345{↵ ^↵4:id↵
请注意。您可能有点混淆了。架构和解析程序是API的一部分,不需要在客户端上进行查询。仅出于演示目的,这里提供了一个通常在API服务器上运行的有效架构定义:
let schema = buildSchema(`
type Item {
id: Int!
name: String!
}
type Employee {
id: Int!
items: [Item]
}
type Query {
employee(id: Int!): Employee
}
`);
然后定义类型和解析器简化示例:
class Employee {
constructor(id, items) {
this.id = id;
this.items = items;
}
}
let root = {
employee: ({id}) => {
return new Employee(id, [{id: 1, name: 'Item 1'}, {id: 2, name: 'Item2'}]);
}
};
然后可以运行查询:
const query = `
{
employee(id: 1) {
id,
items {
id,
name
}
}
}
`;
graphql(schema, query, root).then((response) => {
console.log(response.data);
});
要对远程API运行实际查询,请查看like或