繁体   English   中英

(Graphql) 解析器值不是对象类型

[英](Graphql) The resolver value isn't of type object

我在尝试使用 express 和 apollo 设置后端时尝试使用anilist public api ,但是当我尝试使用动漫的 ID 进行查询以获取其一些详细信息时出现错误。 关于如何解决这个问题的任何想法?

OBS:我从 api 网站上得到了查询的例子。

const {ApolloServer} = require("apollo-server-express");
const express = require("express");
const app = express();
const {gql} = require("apollo-server-express");

// Schema
let typeDefs = gql`
query { # Define which variables will be used in the query (id)
Media (id: $id, type: ANIME) { # Insert our variables into the query arguments (id) (type: ANIME is hard-coded in the query)
    id
    title {
      romaji
      english
      native
    }
  }
}
`;

// Resolver
let resolvers = {
    id: 15125
};


// Server
const server = new ApolloServer({typeDefs, resolvers});

// Define the config we'll need for our Api request
var url = 'https://graphql.anilist.co',
    options = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
        },
        body: JSON.stringify({
            typeDefs: typeDefs,
            resolvers: resolvers
        })
    };

// Make the HTTP Api request
fetch(url, options).then(handleResponse)
                   .then(handleData)
                   .catch(handleError);

function handleResponse(response) {
    return response.json().then(function (json) {
        return response.ok ? json : Promise.reject(json);
    });
}

function handleData(data) {
    console.log(data);
}

function handleError(error) {
    alert('Error, check console');
    console.error(error);
}

// Apollo
async () => {
    await server.start();
    server.applyMiddleware({app});
};

// Express
app.get("/",(req,res) => res.send("Response from the GET request"));
//app.get("/graphql",(req,res) => res.send("Response from the GET request"));

app.listen({port: 4000}, () => {
    console.log(`Server running on localhost:4000${server.graphqlPath}`);
});

结果如下:

throw new Error(`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". The resolver's value must be of type object.`);
// Resolver
let resolvers = {
    id: 15125
};

改成这个

// Resolver
const resolvers = {
   getID : {"id" : 15125}
}

getID 是 typeName, {"id": 15125} 这个值对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM