簡體   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