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

Express + GraphqlTs #11

Open
Bennit opened this issue Nov 17, 2017 · 4 comments
Open

Express + GraphqlTs #11

Bennit opened this issue Nov 17, 2017 · 4 comments

Comments

@Bennit
Copy link

Bennit commented Nov 17, 2017

Hi Nicolas

First of all: nice work on the library! I prefer this approach over the approach of having to write in the graphql schema language, using a tool to generate typescript interfaces and finally committing these generated interfaces into the git project.

To get it to work with express, and to be able to use @types/graphql, I had to install graphql as a separate dependency. However if you do this, then graphql-express complains about the schema generated by graphqlTs.getSchema() not being a 'proper' GraphQLSchema.
In package.json, via postinstall, I removed the nested version of graphql from node_modules/graphql-ts, making sure that graphql-ts uses the constructor from the graphql dependency I explicitly installed. After this, everything seems to work fine so perhaps a suggestion is to move the dependency to devDependencies in your project and ask for manual installation or to keep the graphql dependency more up to date.

Kind regards
Ben

@Bennit
Copy link
Author

Bennit commented Nov 17, 2017

FYI, here is an example of how to use graphql-ts in the context of express

package

{
  "name": "example-express-graphql-ts",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "start": "ts-node src/index.ts",
    "postinstall": "rm -r node_modules/graphql-ts/node_modules/graphql"
  },
  "author": "Ben Corne",
  "private": true,
  "devDependencies": {
    "@types/express": "^4.0.39",
    "@types/express-graphql": "0.0.35",
    "@types/graphql": "^0.11.6",
    "@types/node": "^8.0.51",
    "ts-node": "^3.3.0",
    "typescript": "^2.6.1"
  },
  "dependencies": {
    "express": "^4.16.2",
    "express-graphql": "^0.6.11",
    "graphql": "^0.11.7",
    "graphql-ts": "^0.1.5",
  }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "target": "es6",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": ["es7"]
    },
    "include": [
        "src/**/*.ts"
    ]
}

src/index.ts

import * as express from 'express';
import { Application } from 'express';
import * as graphqlHTTP from 'express-graphql';

import { field, objectType, graphqlTs } from 'graphql-ts';
import { GraphQLSchema } from 'graphql';

const host = '0.0.0.0';
const port = 3000;

@objectType
class Root {
    @field
    hello(): string {
        return "Hello, World!";
    }
}

async function listen (app: Application, host: string, port: number) {
    return new Promise((resolve, reject) => {
        app
            .listen(port, host, () => resolve())
            .on('error', (err) => reject(err));
    });
}

async function main () {
    graphqlTs.init(new Root());
    const schema: GraphQLSchema = graphqlTs.getSchema();

    const app = express();
    
    app.use('/', graphqlHTTP({
        schema: schema,
        graphiql: true
    }));

    await listen(app, host, port);
    console.log(`graphql server listening on http://${host}:${port}`);
}

main()
.then(() => console.log('done'))
.catch((err) => console.error('error', err));

@nicolasgere
Copy link
Owner

Hello thanks for the feedback, you re right i should keep graphql dependency up to date. I will move it to dev dependencies. Or may be you can do it, all pr are welcome!

@iamlothian
Copy link

A peer dependency is probably what you want with a minimum supported version specified.

@MichalLytek
Copy link
Contributor

According to the question - @types/graphql can't be a devDependency or peerDependency as graphql-ts rely on some internal operations. It should be constantly updated along with graphql-js dependency.

BTW, I would recommend you using graphql-yoga instead of pure express middleware where you have to take care of everything.
You can see how simple you can start a server with cors, subscriptions and others:
https://github.com/19majkel94/type-graphql/blob/master/examples/simple-subscriptions/index.ts

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

4 participants