I'm trying to use anilist public api while trying to set a backend using express and apollo, but I'm getting an error when I try to do a query using the ID of an anime to get some of its details. Any ideas on how to solve this?
OBS: I got the query's example from the api website.
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}`);
});
The result of that is the following:
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
};
change to this
// Resolver
const resolvers = {
getID : {"id" : 15125}
}
getID is typeName, {"id": 15125} this value object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.