Integrating with Apollo Server
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 - please post some code. Hard to diagnose without it.
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?
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?
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?