[英]How to add type bindings to graphql resolver mappings
Is there a standard way of adding type bindings to a graphql resolver mapping?是否有将类型绑定添加到 graphql 解析器映射的标准方法?
sampleResolver.ts: sampleResolver.ts:
export default {
sampleResolver: (parent, args, context, info) => {
...
return ...
}
};
eg例如
sampleResolver<...>: (parent, args, context, info) => ...
or
sampleResolver: (parent, args, context, info): ... => ...
or
sampleResolver: (parent: ..., args: ..., context:..., info:...) => ...
I don't think apollo-server-express
has implemented type definitions for resolvers as seen on this issue .我不认为apollo-server-express
已经实现了解析器的类型定义,就像在这个问题上看到的那样。 However, you could try graphql-code-generator to automatically generate type definitions for you schemas, resolvers and so on.但是,您可以尝试使用graphql-code-generator为您的架构、解析器等自动生成类型定义。
There are multiple ways to do that, see more .有多种方法可以做到这一点,请参阅更多。 In your case i think this would be suitable:在你的情况下,我认为这将是合适的:
sampleResolver.ts : sampleResolver.ts :
// Interface checking for return
export interface SampleResolver {
[name: string]: any
}
// Then assign Interface at return
export default {
sampleResolver: (parent, args, context, info) => {
...
return <SampleResolver>...
}
};
Note : The export
is used for type checking in other files.注意: export
用于其他文件中的类型检查。
otherFiles.ts其他文件.ts
import { SampleResolver } from 'sampleResolver';
var response: SampleResolver;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.