0
votes

Is it possible to connect to an Apollo GraphQL server using a non-Apollo client such as graphql.js - https://github.com/f/graphql.js?

If so what endpoint should one use? Or is there another approach?

This fails with a HTTP 500 server error:

const graph = graphql('http://localhost:3013/graphql', {
        method: 'POST' // POST by default.
    });

    const res = graph(`query getQuestions {
        questions {
          id,
          question
        }
      }
    `);

    res().
        then((result) => console.log(result))
        .catch((err) => console.log(err));
1

1 Answers

0
votes

Of course, you can use any GraphQL client as long as the client follows the GraphQL spec.

E.g.

server.ts:

import { ApolloServer, gql } from 'apollo-server';
import graphql from 'graphql.js';

const typeDefs = gql`
  type Query {
    _: String
  }
`;
const resolvers = {
  Query: {
    _: () => 'Hello',
  },
};
const server = new ApolloServer({
  typeDefs,
  resolvers,
});
server.listen().then(async ({ url }) => {
  console.log(`Apollo server is listening on ${url}graphql`);
  const graph = graphql(`${url}graphql`, { asJSON: true });
  const helloQuery = graph(`
    query {
      _
    }
  `);
  const actual = await helloQuery();
  console.log('actual: ', actual);
  server.stop();
});

Output:

Apollo server is listening on http://localhost:4000/graphql
actual:  { _: 'Hello' }