Skip to content

Add graphql codegen to starwars example#13

Open
wKich wants to merge 1 commit intomainfrom
dl/add-codegen-example
Open

Add graphql codegen to starwars example#13
wKich wants to merge 1 commit intomainfrom
dl/add-codegen-example

Conversation

@wKich
Copy link
Copy Markdown
Member

@wKich wKich commented Nov 13, 2023

Motivation

GraphQL Yoga example doesn't have codegen config

Approach

Add simple codegen config and use generated types in GraphQL resolvers

Possible Drawbacks or Risks

The example is pretty simple, so it doesn't show potential issues with generated types

Signed-off-by: Dmitriy Lazarev <w@kich.dev>
@wKich wKich requested review from cowboyd and taras November 13, 2023 11:17
},
},
},
} as Resolvers,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cowboyd @wKich This current setup just makes typescript not complain but it doesn't help people writing the resolvers. Is there a better way to type this so we can actually use TypeScript for guidance in parameters?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right. It's better to declare resolvers:

const resolvers: Resolvers = { /* ... */ }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to wrap createModule() to require the type of resolvers to be Resolvers. Another pattern I've become fond of is to use a function:

function moduleResolvers(): Resolvers {
}

and then invoke it like:

createModule({
  id: "starwars",
  dirname: import.meta.url,
  resolvers: moduleResolvers(),
});

Or as something that can be spread in:

function moduleResolvers(): { resolvers: Resolvers } {
 //....
}
createModule({
  id: "starwars",
  dirname: import.meta.url,
  ...moduleResolvers(),
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with the ...moduleResolvers() or moduleResolves() - both work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants