Thursday, 13 April 2017

Type errors trying to configure graphqlExpress

I'm trying GraphQL with Express for the first time. I have declared a schema:

import { makeExecutableSchema } from "graphql-tools";
import { interfaces } from "inversify";

const schema = `
    type Feature {
        id: Int!
        name: String
        description: String
        roles: [Feature]
    }

    type Role {
        id: Int!
        name: String
        description: String
        roles: [Feature]
    }

    type User {
        id: Int!
        fullname: String
        email: String
        profilePicUrl: String
        status: String
        roles: [Role]
    }

    type Query {
        users: [User]
        roles: [Role]
        features: [Feature]
        role(id: Int!): Author
    }

    schema {
        query: Query
    }
`;

const resolveFunctions = {
  Query: {
    users(obj: any, args: any, context: { container: interfaces.Container }) {
        return [];
    },
    roles(obj: any, args: any, context: { container: interfaces.Container }) {
        return [];
    },
    features(obj: any, args: any, context: { container: interfaces.Container }) {
        return [];
    }
  },
  User: {
    roles(userId: number) {
      return [];
    }
  },
  Feature: {
    roles(featureId: number) {
      return [];
    },
  },
  Role: {
    users(roleId: number) {
      return [];
    },
    features(roleId: number) {
      return [];
    }
  },
};

const executableSchema = makeExecutableSchema({
  typeDefs: schema,
  resolvers: resolveFunctions,
});

export { executableSchema };

I have imported the executableSchema and tried to create a graphqlExpress instance but I get a compilation error:

import { executableSchema } from "./schema";
import { graphqlExpress } from "graphql-server-express";

// ...

app.use("/graphql", graphqlExpress((req) => { // Error!
    return {
        schema: executableSchema,
        context: {
            container: container
        }
    };
}));

The error is the following:

'Argument of type '(req: Request | undefined) => { schema: GraphQLSchema; context: { container: Container; }; }'

is not assignable to parameter of type 

'GraphQLServerOptions | ExpressGraphQLOptionsFunction'

I have examined the dts file:

import * as express from 'express';
import { GraphQLOptions } from 'graphql-server-core';
import * as GraphiQL from 'graphql-server-module-graphiql';
export interface ExpressGraphQLOptionsFunction {
    (req?: express.Request, res?: express.Response): GraphQLOptions | Promise<GraphQLOptions>;
}
export interface ExpressHandler {
    (req: express.Request, res: express.Response, next: any): void;
}
export declare function graphqlExpress(options: GraphQLOptions | ExpressGraphQLOptionsFunction): ExpressHandler;
export declare function graphiqlExpress(options: GraphiQL.GraphiQLData): (req: express.Request, res: express.Response) => void;

But I have not been able to figure out what is wrong. Any ideas? Thanks!



via OweR ReLoaDeD

No comments:

Post a Comment