简单的 graphql 模式抛出语法错误,也无法从日志中追踪问题行
Simple graphql schema throwing syntax error, also unable to trace the issue line from logs
我正在尝试在我的节点项目中创建一个 GraphQL 实现。我在单独的 js 中创建了一个模式和解析器,我从 index.js 文件中调用它们。以下是架构、解析器和索引 js 文件中的代码
架构
const schema = `
type Query {
testFunction(): String!
}
schema {
query: Query
}
`;
module.exports = schema;
解析器
const resolvers = ()=>({
Query:{
testFunction(){
return "returned from custom test function";
}
}
});
module.exports = resolvers;
索引
const graphqlSchema = require('./graphql/schema');
const createResolvers = require('./graphql/resolvers');
const executableSchema = makeExecutableSchema({
typeDefs: [graphqlSchema],
resolvers: createResolvers()
});
const server=hapi.server({
port: 4000,
host:'localhost'
});
server.register({
register: apolloHapi,
options: {
path: '/graphql',
apolloOptions: () => ({
pretty: true,
schema: executableSchema,
}),
},
});
server.register({
register: graphiqlHapi,
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql',
},
},
});
const init= async()=>{
routes(server);
await server.start();
console.log(`Server is running at: ${server.info.uri}`);
}
init();
启动服务器时出现以下错误
node_modules\graphql\language\parser.js:1463
throw (0, _error.syntaxError)(lexer.source, token.start, "Expected >".concat(kind, ", found ").concat((0, _lexer.getTokenDesc)(token)));
请帮助我了解我哪里出了问题以及如何解决这个问题。
模式中的任何字段都可以有参数。如果字段包含参数,则应将它们括在一对括号中,如下所示:
type Query {
testFunction(someArg: Int): String!
}
但是,当没有参数时,只有一对空括号是无效的语法。你应该完全忽略它们,像这样:
type Query {
testFunction: String!
}
我正在尝试在我的节点项目中创建一个 GraphQL 实现。我在单独的 js 中创建了一个模式和解析器,我从 index.js 文件中调用它们。以下是架构、解析器和索引 js 文件中的代码
架构
const schema = `
type Query {
testFunction(): String!
}
schema {
query: Query
}
`;
module.exports = schema;
解析器
const resolvers = ()=>({
Query:{
testFunction(){
return "returned from custom test function";
}
}
});
module.exports = resolvers;
索引
const graphqlSchema = require('./graphql/schema');
const createResolvers = require('./graphql/resolvers');
const executableSchema = makeExecutableSchema({
typeDefs: [graphqlSchema],
resolvers: createResolvers()
});
const server=hapi.server({
port: 4000,
host:'localhost'
});
server.register({
register: apolloHapi,
options: {
path: '/graphql',
apolloOptions: () => ({
pretty: true,
schema: executableSchema,
}),
},
});
server.register({
register: graphiqlHapi,
options: {
path: '/graphiql',
graphiqlOptions: {
endpointURL: '/graphql',
},
},
});
const init= async()=>{
routes(server);
await server.start();
console.log(`Server is running at: ${server.info.uri}`);
}
init();
启动服务器时出现以下错误
node_modules\graphql\language\parser.js:1463 throw (0, _error.syntaxError)(lexer.source, token.start, "Expected >".concat(kind, ", found ").concat((0, _lexer.getTokenDesc)(token)));
请帮助我了解我哪里出了问题以及如何解决这个问题。
模式中的任何字段都可以有参数。如果字段包含参数,则应将它们括在一对括号中,如下所示:
type Query {
testFunction(someArg: Int): String!
}
但是,当没有参数时,只有一对空括号是无效的语法。你应该完全忽略它们,像这样:
type Query {
testFunction: String!
}