Warm tip: This article is reproduced from serverfault.com, please click

javascript-“语法错误:预期名称,找到\”)\“。”,

(javascript - "Syntax Error: Expected Name, found \")\".",)

发布于 2020-11-28 08:58:27

一般来说,我是graphql和js的新手,力图尽可能多地练习。我做了以下事情:

var schema = buildSchema(`
    type Query{
        events(): Message
    }

    type Message{
        id: ID!
        random_nr: Int!
    }`);

class Message {

    calculate(){
        var output = [];
        var id = Math.random().toString();
        var random_nr = Math.floor(Math.random() * 10);
        output.push(id);
        output.push(random_nr);
        return output;
        console.log(output);
         };
    }

    var root = {  events:()=>{ return new Message(); }}

然后在浏览器中执行:

{
  events(){
    calculate()
  }
}

收到标题错误,有人能将智慧之光照射到我身上吗?

更新()现在添加到事件,我收到:

{
  "message": "Failed to fetch",
  "stack": "TypeError: Failed to fetch"
} 
Questioner
PureAbap
Viewed
0
PureAbap 2020-11-28 21:37:40

正确答案:

var schema = buildSchema(`
    type Message{
        calculate: [Int]
    }

    type Query{
        events: Message
    }   `);

并在浏览器中删除没有参数的all()