question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[typescript] Why is parent/args hardcoded as optional?

See original GitHub issue

Hi there, awesome project to make server-side graphql even more enjoyable.

I just generated some typescript typings

gql-gen --schema ./schema.js --template graphql-codegen-typescript-template --out ./typings/

It generated typings/types.ts as expected:

...
export type Resolver<Result, Parent = any, Context = any, Args = any> = (
  parent?: Parent,
  args?: Args,
  context?: Context,
  info?: GraphQLResolveInfo
) => Promise<Result> | Result;
...

“parent” and “args” are optional, this is hardcoded in:

https://github.com/dotansimha/graphql-code-generator/blob/master/packages/templates/typescript/src/schema.handlebars#L5-L7

AFAIK the “parent” field is only optional for root types Query/Mutation/Subscription.

The “args” field should only be optional if the corresponding graphql field arguments are optional.

Am I doing something wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilkisielacommented, Aug 13, 2018
1reaction
kamilkisielacommented, Aug 13, 2018

I would make them non optional, because in most cases they are. This will hurt a bit while unit testing:

fooResolver({} as any, {}, context, {} as any)

But.

You can always wrap resolvers with this function in unit tests:

type MockResolver<Result, Args> = (
  parent?: any,
  args?: Args,
  context?: any,
  info?: any
) => Promise<Result> | Result;

export function mockResolver<R, A>(
  resolver: Resolver<R, A>
): MockResolver<R, A> {
  return (parent, args, context, info) => resolver(parent, args, context, info);
}

This will make all arguments optional and TS, that weird mad beast, won’t scream

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolvers - Apollo GraphQL Docs
A resolver can optionally accept four positional arguments: (parent, args, contextValue, ... The following example defines a hardcoded data set, a schema, ...
Read more >
how to use multiple optional arguments in typescript
I've been working in TypeScript for a while and completely skipped over the fact that they used Number , which would cause an...
Read more >
Optional and default parameters in Typescript - YouTube
In this video, learn what is Optional and default parameter concept in typescript and how to use it.
Read more >
Using hardcoded IP addresses is security-sensitive
TypeScript static code analysis. Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your TYPESCRIPT code.
Read more >
Documentation - TypeScript 3.7
Optional Chaining · function makeRequest(url: string, log?: (msg: string) => void) { · log?.(`Request started at ${new Date().toISOString()}`); · // roughly ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found