You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to connect my GraphQL server to my localhost RESTful backend server. However, the connection keeps getting refused. Here's the code to reproduce:
import { ApolloServer, gql } from 'apollo-server';
import { RESTDataSource } from 'apollo-datasource-rest';
//Schema
const typeDefs = gql`
type Query {
#Query to get health of myapi
getHealth: String
}
`;
// api
class myAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'http://localhost:8080';
}
async getHealth(){
try {
const response = await this.get('/v1/health');
return response;
} catch (error) {
console.error(error);
throw new Error('Unable to fetch health information from server');
}
}
}
//resolvers
const resolvers = {
Query: {
getHealth: async (_, __, {dataSources}) => {
return await dataSources.myAPI.getHealth();
}
}
}
// server
const server = new ApolloServer({
typeDefs,
resolvers,
csrfPrevention: false,
cors: { origin: '*'},
cache: 'bounded',
dataSources: () => {
return {
myAPI: new myAPI(),
};
},
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
What I've Tried
The restful API is fully functional and running. This was proven by successful responses being returned when trying the following:
Querying via Postman
Curl
The Browser
The text was updated successfully, but these errors were encountered:
Can you provide an actual runnable reproduction so I don't have to be creative with standing up a REST service? A codesandbox or git repo that I can clone and run would be what I'm looking for.
Also you're on old versions of both Apollo Server and the RESTDataSource packages (the new versions are published under new package names @apollo/server and @apollo/datasource-rest).
The Problem
I am trying to connect my GraphQL server to my localhost RESTful backend server. However, the connection keeps getting refused. Here's the code to reproduce:
What I've Tried
The restful API is fully functional and running. This was proven by successful responses being returned when trying the following:
The text was updated successfully, but these errors were encountered: