0

我第一次尝试使用 Express 的 GraphQL。我已经声明了一个模式:

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 };

我已导入executableSchema并尝试创建一个graphqlExpress实例,但出现编译错误:

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

// ...

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

错误如下:

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

is not assignable to parameter of type 

'GraphQLServerOptions | ExpressGraphQLOptionsFunction'

我检查了 dts 文件:

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;

但我一直无法弄清楚出了什么问题。有任何想法吗?谢谢!

4

3 回答 3

0

另一种解决方案是导入GraphQLServerOptions定义并对对象进行类型转换。类似于 James 提出的解决方案,但通过这种方法,您可以使用 ES6 对象速记语法。

import { GraphQLServerOptions } from 'apollo-server-core/src/graphqlOptions';
import { graphqlExpress } from 'apollo-server-express';
import { executableSchema as schema } from "./schema";

// ...

app.use('/graphql', graphqlExpress({
  schema,
  context: { container },
} as GraphQLServerOptions));
于 2017-09-20T23:02:35.413 回答
0

看起来您需要使用类型断言来让 TypeScript 编译器知道它executableSchema是 type GraphQLSchema。尝试以下操作:

尝试这个:

import { executableSchema } from "./schema";
import { graphqlExpress } from "graphql-server-express";    
import { GraphQLSchema } from "graphql-server-core/node_modules/@types/graphql";

// ...

app.use("/graphql", graphqlExpress((req) => { // Error!
    return {
        schema: executableSchema as GraphQLSchema,
        context: {
            container: container
        }
    };
}));
于 2017-04-24T17:20:45.327 回答
0

这不是一个解决方案的答案...但是如果您尝试使用 GraphQL js 标准实现及其类型来定义架构...它有效吗?

于 2017-04-13T19:09:45.160 回答