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.

Why are fields on QueryResolver optional?

See original GitHub issue

Hello,

I’m generating resolvers using resolvers template

gql-gen --template graphql-codegen-typescript-resolvers-template --config gql-gen.json --out __generated/resolver-types.ts -s src/graph-api/schema.graphql (using no-namespaces version for babel-typescript)

Here’s my query type:

type Query {
  users: [User!]!
}

Here’s my query resolver type:

export interface QueryResolvers<Context = RequestContext, TypeParent = never> {
  users?: QueryUsersResolver<User[], TypeParent, Context>;
}

Here’s how I use it:

export const Query: QueryResolvers = {
  users: async (_parent, _args, ctx) => ctx.db.users()
};

Why is users field optional? The result of users query can’t be null. Am I missing something? If there’s a reason for it to be optional, can we get an option to make it required? I’d like to be notified by typescript if I don’t implement a resolver that is required in my schema. Would be very helpful.

Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
borekbcommented, Jun 10, 2019

As a small note, there’s a built-in Required<T> type:

const queryResolvers: Required<QueryResolvers> = ...;
2reactions
P4sca1commented, Mar 20, 2019

I would prefer if it would be required by default and if you want to split it into modules, you use the already existing Partial type. Guess that’s just personal preference. Anyways, thank you for your answer! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolvers - Apollo GraphQL Docs
A resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data...
Read more >
Resolvers – GraphQL Tools
To respond to queries, a schema needs to have resolvers for all fields. Resolvers are per field functions that are given a parent...
Read more >
In GraphQL, how do I implement optional queries?
GraphQL APIs are organized in terms of types and fields, I recommend creating separate queries for User types and Club types, if someone...
Read more >
GraphQL Resolvers: Best Practices | by Mark Stuart
Root Query fields, like user and album, are executed in parallel but ... We will test out the first option: adding it to...
Read more >
Arguments
field :search_posts, [PostType], null: false do argument :category, ... are optional and the query does not provide any arguments, then the resolver method ......
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