Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to Localhost server #183

Closed
khalilgreenidge opened this issue Apr 6, 2023 · 1 comment
Closed

Unable to connect to Localhost server #183

khalilgreenidge opened this issue Apr 6, 2023 · 1 comment

Comments

@khalilgreenidge
Copy link

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:

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:

  1. Querying via Postman
  2. Curl
  3. The Browser
@trevor-scheer
Copy link
Member

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).

Happy to reopen this with a reproduction.

@trevor-scheer trevor-scheer closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants