如何将类型绑定添加到graphql解析器映射

时间:2019-12-12 01:53:43

标签: typescript graphql

是否存在将类型绑定添加到graphql解析器映射的标准方法?

sampleResolver.ts:

export default {
  sampleResolver: (parent, args, context, info) => {
    ...
    return ...
  }
};

例如

sampleResolver<...>: (parent, args, context, info) => ...

or

sampleResolver: (parent, args, context, info): ... => ...

or

sampleResolver: (parent: ..., args: ..., context:..., info:...) => ...

enter image description here

2 个答案:

答案 0 :(得分:2)

我认为apollo-server-express并没有像on this issue那样为解析器实现类型定义。但是,您可以尝试graphql-code-generator为您的模式,解析器等自动生成类型定义。

答案 1 :(得分:0)

有多种方法可以执行此操作,请参见more。在您的情况下,我认为这是合适的:

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

注意export用于检查其他文件中的类型。

otherFiles.ts

import { SampleResolver } from 'sampleResolver';


var response: SampleResolver;