【问题标题】:Type errors trying to configure graphqlExpress尝试配置 graphqlExpress 时输入错误
【发布时间】:2017-09-09 19:15:28
【问题描述】:

我是第一次尝试使用 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;

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

【问题讨论】:

标签: node.js express typescript graphql apollo-server


【解决方案1】:

另一种解决方案是导入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));

【讨论】:

    【解决方案2】:

    看来您需要使用类型断言来让 TypeScript 编译器知道 executableSchema 的类型为 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
            }
        };
    }));
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 而且 GraphQL express 的签名似乎与您使用的签名不同。 ``` app.use('/graphql', graphqlHTTP({ schema: MyGraphQLSchema, graphiql: true })); ```
      猜你喜欢
      • 2018-03-25
      • 2019-05-11
      • 2022-07-07
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多