2017-04-13 57 views
0

我第一次嘗試使用Express的GraphQL。我宣佈一個模式:輸入錯誤嘗試配置graphqlExpress

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; 

但我一直無法弄清楚什麼是錯的。有任何想法嗎?謝謝!

+0

你能分享你的'tsconfig.json'嗎? – Diullei

+0

您可能想看看這個GraphQL示例項目: https://github.com/kriasoft/nodejs-api-starter –

回答

0

這不是一個解決方案的答案......但如果你嘗試使用GraphQL js標準實現及其類型來定義模式......它是否工作?

+0

而GraphQL express的簽名似乎與您正在使用的簽名不同。 ''' app.use('/ graphql',graphqlHTTP({0}){MyGraphQLSchema, graphiql:true })); ''' – Mino

0

看起來您需要使用Type Asserting讓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 
     } 
    }; 
})); 
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)); 
相關問題