#How to create a .d.ts definition for this code

16 messages · Page 1 of 1 (latest)

tropic current
#

I'm using this library and function: https://github.com/mickhansen/graphql-sequelize/blob/master/src/resolver.js#L119
It's essentially a factory that returns a GraphQL Resolver function. Alongside that, you can add a property to the factory like so:

resolver.contextToOptions = {
  dataloaderContext: [EXPECTED_OPTIONS_KEY],
};

This code 100% works and is documented to be used like so. I'm just trying to create a typescript definition file for this library I'm using so I can't just fix it at the source obviously. I've created a type for the factory function like so:

type ResolverOptions = {
  after?: (result: any, args: any, context: any, info: GraphQLResolveInfo) => any;
  before?: (options: any, args: any, context: any, info: GraphQLResolveInfo) => any;
  handleConnection?: boolean;
  list?: boolean;
};

export function resolver<T>(targetMaybeThunk: T | ((...args: any[]) => T), options?: ResolverOptions): GraphQLFieldResolver;

And in my code it gives an error:

Property contextToOptions does not exist on type

I tried doing the below:

export function resolver<T>(targetMaybeThunk: T | ((...args: any[]) => T), options?: ResolverOptions): GraphQLFieldResolver;

namespace resolver {
  const contextToOptions: {
    dataloaderContext: string[];
  };
}

And now my code says:

Cannot assign to contextToOptions because it is a read-only property.

I've no idea how to type a factory function to allow adding properties to it.

solemn wedgeBOT
#
module.exports = resolverFactory;
static ember
#

I think its something like

type Resolver = {
  <T>(targetMaybeThunk: T | ((...args: any[]) => T), options?: ResolverOptions): GraphQLFieldResolver;
  contextToOptions?: {
    dataloaderContext: string[];
  };
}
export const resolver: Resolver
#

but I am not sure on the exact syntax for .d.ts files

tacit prawnBOT
#
sandiford#0

Preview:```ts
type ResolverOptions = boolean
type GraphQLFieldResolver = boolean

type Resolver = {
<T>(
targetMaybeThunk: T | ((...args: any[]) => T),
options?: ResolverOptions
): GraphQLFieldResolver
contextToOptions?: {
dataloaderContext: string[]
}
}

const r = function resolver<T>(
targetMaybeThunk: T | ((...args: any[]) => T),
options?: ResolverOptions
): GraphQLFieldResolver {
...```

static ember
#

This compiles

tropic current
#

thanks so much

#

I'm not getting an error anymore when trying to set dataloaderContext, but the only thing which isn't that important is the return type of the resolver factory is now any

static ember
#

Can I see your code?

#

You have a return type of GraphQLFieldResolver for the function?
How is GraphQLFieldResolver defined?

tropic current
#

it comes from graphql in npm

static ember
#

do you need to install types for it?

#
#

says it has types built in

#

You could check whether GraphQLFieldResolver is typed properly in your .d.ts file