Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graphql codegen to starwars example #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wKich
Copy link
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

@wKich wKich requested review from cowboyd and taras November 13, 2023 11:17
@@ -119,5 +102,5 @@ export const StarWars = createModule({
}));
},
},
},
} as Resolvers,
Copy link
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
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
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
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