首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试配置graphqlExpress时出现类型错误

尝试配置graphqlExpress时出现类型错误
EN

Stack Overflow用户
提问于 2017-04-13 23:20:20
回答 3查看 1.3K关注 0票数 0

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

代码语言:javascript
运行
复制
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实例,但得到一个编译错误:

代码语言:javascript
运行
复制
import { executableSchema } from "./schema";
import { graphqlExpress } from "graphql-server-express";

// ...

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

错误如下:

代码语言:javascript
运行
复制
'Argument of type '(req: Request | undefined) => { schema: GraphQLSchema; context: { container: Container; }; }'

is not assignable to parameter of type 

'GraphQLServerOptions | ExpressGraphQLOptionsFunction'

我已经检查了dts文件:

代码语言:javascript
运行
复制
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;

但我还没能弄清楚哪里出了问题。有什么想法吗?谢谢!

EN

回答 3

Stack Overflow用户

发布于 2017-04-14 03:09:45

这不是一个解决方案..。但是,如果您尝试使用GraphQL js标准实现及其类型来定义模式...它起作用了吗?

票数 0
EN

Stack Overflow用户

发布于 2017-04-25 01:20:45

看起来您需要使用类型断言来让TypeScript编译器知道executableSchemaGraphQLSchema类型的。尝试以下操作:

试试这个:

代码语言:javascript
运行
复制
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
        }
    };
}));
票数 0
EN

Stack Overflow用户

发布于 2017-09-21 07:02:35

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

代码语言:javascript
运行
复制
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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43396102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档