Skip to content

Commit

Permalink
Merge pull request #14 from getcronit/fix-empty-graphql-query
Browse files Browse the repository at this point in the history
fix: remove empty resolvers in graphql-handler.ts and add validation …
  • Loading branch information
schettn authored Sep 18, 2024
2 parents 798089a + 01a6149 commit de8e828
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
7 changes: 0 additions & 7 deletions packages/pylon/src/app/handler/graphql-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ export interface SchemaOptions {
export const graphqlHandler =
(c: Context) =>
({typeDefs, resolvers}: SchemaOptions) => {
// Remove empty resolvers
for (const key in resolvers) {
if (Object.keys(resolvers[key]).length === 0) {
delete resolvers[key]
}
}

resolvers = resolversToGraphQLResolvers(resolvers)

const schema = createSchema({
Expand Down
21 changes: 19 additions & 2 deletions packages/pylon/src/define-pylon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export const resolversToGraphQLResolvers = (
}
}

if (resolvers.Query) {
if (resolvers.Query && Object.keys(resolvers.Query).length > 0) {
for (const [key, value] of Object.entries(resolvers.Query)) {
if (!graphqlResolvers.Query) {
graphqlResolvers.Query = {}
Expand All @@ -261,7 +261,7 @@ export const resolversToGraphQLResolvers = (
}
}

if (resolvers.Mutation) {
if (resolvers.Mutation && Object.keys(resolvers.Mutation).length > 0) {
if (!graphqlResolvers.Mutation) {
graphqlResolvers.Mutation = {}
}
Expand All @@ -273,6 +273,23 @@ export const resolversToGraphQLResolvers = (
}
}

// Query root type must be provided.
if (!graphqlResolvers.Query) {
// Custom Error for Query root type must be provided.

throw new Error(`At least one 'Query' resolver must be provided.
Example:
export const graphql = {
Query: {
// Define at least one query resolver here
hello: () => 'world'
}
}
`)
}

return graphqlResolvers
}

Expand Down

0 comments on commit de8e828

Please sign in to comment.