reactql icon indicating copy to clipboard operation
reactql copied to clipboard

Integrating with Apollo Server

Open ghiblin opened this issue 7 years ago • 4 comments

Hi,

I'd like to integrate ReactQL Koa Server with an Apollo Server. I've tried to setup a middleware ad described in the server documentation just before the router middleware in the /src/runner/app.ts file, but that does not work. Any ideas?

Thank you

ghiblin avatar Nov 27 '18 06:11 ghiblin

@ghiblin - please post some code. Hard to diagnose without it.

leebenson avatar Nov 27 '18 12:11 leebenson

I've defined a basic apollo server:

// apollo.ts
import { ApolloServer, gql } from "apollo-server-koa";

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => "Hello world!",
  },
};

const server = new ApolloServer({ typeDefs, resolvers });
export default server;

then in runner/app.ts I've imported it and tried to setup the middleware with import apollo from './apollo'; apollo.applyMiddleware({ app }); I've placed this line of code in different point (just after app initialization, before static middleware or router), but when I point to http://localhost:8080/graphql I don't see graphql playground, but my layout page.

Any ideas?

ghiblin avatar Nov 28 '18 06:11 ghiblin

I found a solution with a monkey patch: in entry/server.tsx I check for ctx.path

  // Create Koa middleware to handle React requests
  return async (ctx: Context, next: () => Promise<any>) => {
    ...
    if (ctx.path === "/graphql") {
      return next();
    }
    ...
}

In this way I can access to apollo server playground on http://localhost:3000/graphql, but it's not the way to resolve this issue. I think I have to create a new entry in src/entry and then import it in src/runner/development,ts. Is it correct?

ghiblin avatar Dec 03 '18 14:12 ghiblin

I'm trying to use the koa context in GraphQL, but it doesn't pass through even with context: ({ ctx }:any) => ctx, in ApolloServer option. Does anyone got it working?

ZimboQC avatar Feb 01 '19 12:02 ZimboQC