apollographql / apollo-server

🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
https://www.apollographql.com/docs/apollo-server/
MIT License
13.79k stars 2.03k forks source link

Missing modules when used from TypeScript #124

Closed jahewson closed 8 years ago

jahewson commented 8 years ago

Hi, I'm trying to use apollo-server with TypeScript, however I'm unable to get the compilation working due to missing module definitions.

I've published my ts port of apollo-starter-kit. If you clone that and run:

npm install
npm run compile

You'll get the following errors:

node_modules/apollo-server/dist/core/runQuery.d.ts(1,72): error TS2307: Cannot find module 'graphql'.
node_modules/apollo-server/dist/integrations/apolloOptions.d.ts(1,26): error TS2307: Cannot find module 'graphql'.
node_modules/apollo-server/dist/integrations/hapiApollo.d.ts(1,23): error TS2307: Cannot find module 'hapi'.
node_modules/apollo-server/dist/integrations/koaApollo.d.ts(1,22): error TS2307: Cannot find module 'koa'.
node_modules/apollo-server/dist/modules/operationStore.d.ts(1,41): error TS2307: Cannot find module 'graphql'.

A couple of thoughts:

helfer commented 8 years ago

Yeah, that's a bit unfortunate. @nnance ran into the same problem a while ago. I don't know of any way around this problem that doesn't involve putting the integrations in separate modules, so that's what we'll do eventually. You'll still need the GraphQL typings, but at least you won't need the typings of servers that you don't use.

helfer commented 8 years ago

And by "eventually" I mean: probably within the next week or two.

robertohuertasm commented 8 years ago

Even though installing graphql, koa and hapi I'm still receiving some errors. @jahewson did you solve it?

This is what I'm getting:

node_modules/apollo-server/dist/core/runQuery.d.ts(1,50): error TS2305: Module ''graphql'' has no exported member 'ValidationRule'.
node_modules/apollo-server/dist/integrations/apolloOptions.d.ts(9,37): error TS2305: Module ''graphql'' has no exported member 'ValidationRule'.
typings/modules/koa/index.d.ts(118,10): error TS2305: Module '"http"' has no exported member 'ResponseHeaders'.
typings/modules/koa/index.d.ts(120,10): error TS2305: Module ''~koa/lib/application'' has no exported member 'Application'.
typings/modules/koa/index.d.ts(433,10): error TS2305: Module ''~koa/lib/application'' has no exported member 'Application'.
typings/modules/koa/index.d.ts(778,10): error TS2305: Module ''~koa/lib/application'' has no exported member 'Application'.
typings/modules/koa/index.d.ts(867,36): error TS2304: Cannot find name 'Context'.
typings/modules/koa/index.d.ts(906,40): error TS2304: Cannot find name 'Context'.
DxCx commented 8 years ago

Looks like you are missing typings for graphql.. Try npm install --save-dev graphql-typings And make sure that your compiler Has a reference to it. You can see my example repostiory note tsconfig (make sure you are using typescript 2 if you do as i did)

DxCx commented 8 years ago

Also typings for koa are missing...

robertohuertasm commented 8 years ago

@DxCx I've solved the issue by installing graphql-typings (I was about to write it here) but still receiving the other errors.

robertohuertasm commented 8 years ago

@DxCx which koa typings should I use? I've already installed one. See my typings.json:

{
  "globalDependencies": {
    "graphql": "github:nitintutlani/typed-graphql",
    "hapi": "github:DefinitelyTyped/DefinitelyTyped/hapi/hapi.d.ts#c54072093a1a3a5c28f74dd7b8a57f45d061e536",
    "node": "registry:dt/node#6.0.0+20160907103612"
  },
  "dependencies": {
    "body-parser": "registry:npm/body-parser#1.15.2+20160815132839",
    "cors": "registry:npm/cors#2.7.0+20160902012746",
    "express": "registry:npm/express#4.14.0+20160815112707",
    "koa": "registry:npm/koa#2.0.0+20160903085952"
  }
}
robertohuertasm commented 8 years ago

@DxCx I missed the reference to your repo. Looking into it right now. Thanks! :grinning:

jahewson commented 8 years ago

@robertohuertasm I installed typings for graphql, koa and hapi as a workaround. Worked fine.

robertohuertasm commented 8 years ago

I've installed them via @types npm package and it seems to be working. I'm not really sure why using typings was giving me such headaches. Anyway, thank you very much for your kind help. :grin: :+1:

DxCx commented 8 years ago

Great! Happy to hear it :) dont forget to close the ticket ;)

robertohuertasm commented 8 years ago

It wasn't opened by me so I left it open. Anyway I think the original issue still remains. Thanks @DxCx for your nice boilerplate!

jahewson commented 8 years ago

Yes, the original issue remains.

DxCx commented 8 years ago

Wait @jahewson whats the current issue? Since the above looks like typing error and you said its working :)

jahewson commented 8 years ago

Original issue is that types for hapi, express, and koa are needed to use this library with TypeScript, regardless of whether or not they are actually "needed", e.g. I don't use koa or express in my project.

DxCx commented 8 years ago

Cool, yep i agree it is frustrating.. Ill try to play with it when i have time maybe ill find a soultion ;)

DxCx commented 8 years ago

ok @jahewson I've confirmed this to be working

diff --git a/package.json b/package.json
index 1a8e0c1..c359d76 100644
--- a/package.json
+++ b/package.json
@@ -38,9 +38,7 @@
     "@types/body-parser": "0.0.31",
     "@types/cors": "0.0.31",
     "@types/express": "^4.0.31",
-    "@types/hapi": "^13.0.33",
     "@types/helmet": "0.0.30",
-    "@types/koa": "^2.0.31",
     "@types/mocha": "^2.2.31",
     "@types/morgan": "^1.7.30",
     "apollo-server": "0.2.6",
diff --git a/src/main.ts b/src/main.ts
index 534e181..cc9b682 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -1,6 +1,6 @@
 import * as express from "express";
 import * as bodyParser from "body-parser";
-import { apolloExpress, graphiqlExpress } from "apollo-server";
+import { apolloExpress, graphiqlExpress } from "apollo-server/dist/integrations/expressApollo";
 import { Schema } from "./schema";
 import * as cors from "cors";
 import * as helmet from "helmet";

so it looks like direct import works, @helfer , @stubailo what are your inputs about this? i think that it's pretty easy to create (At least until better solution will be found) exports to be like:

import { apolloExpress } from "apollo/express"

other solution that is might be worth to explore is namespacing but i haven't try it yet.

helfer commented 8 years ago

@DxCx yeah, I think that's a good idea for the time being. Do you think we should update the docs and readme?

DxCx commented 8 years ago

@helfer indeed, Leaving it as is will just result in people being affriad of using the server :/

But i do think it should be more intuitive then using dist/integrations As i said before maybe apollo-server/express apollo-server/koa etc..

helfer commented 8 years ago

yes, the following style would be great:

import apolloExpress from 'apollo-server/express';

If that's all we need to get rid of the typings errors, then I'd say we should do it for the 0.3 release (which we wanted to do today or tomorrow). @jahewson or @DxCx would one of you be willing to make a quick PR for that? I think all that's needed would be to make four folders (express, hapi, koa, connect) and put an index file with the proper exports in it.

DxCx commented 8 years ago

Hi @helfer i will look into it today which means you will probably have a PR for tomorrow (your) morning

DxCx commented 8 years ago

hi @helfer, i was just starting to investigate it and got into consultation that we need to split the whole package, making apollo-server/express / apollo-server/happi / etc work is going to be a pain. then this new issue (#138) from @nnance popped to me.

don't you think it's a better solution? because from what i understand:

  1. changing module places will cause mess in the whole source.
  2. moving them after compilation might break references emitted in js.
  3. you don't want to use webpack which can export them as dlls.
DxCx commented 8 years ago

taken care along with the monorepo solution. (#138)