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

feat: allow multiple operation names in graph:hander #4978

Merged
merged 5 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/commands/graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ netlify graph:handler

**Arguments**

- name - Operation name
- name - Operation name(s)

**Flags**

Expand Down
36 changes: 20 additions & 16 deletions src/commands/graph/graph-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ const graphHandler = async (args, options, command) => {
error(`Error parsing schema: ${buildSchemaError}`)
}

const userOperationName = args.operationName
const userOperationNames = args.operationNames
const userCodegenId = options.codegen

const handlerOptions = options.data ? JSON.parse(options.data) : {}

let operationName = userOperationName
if (!operationName) {
operationName = await autocompleteOperationNames({ netlifyGraphConfig })
let operationNames = userOperationNames
if (!operationNames || operationNames.length === 0) {
const operationName = await autocompleteOperationNames({ netlifyGraphConfig })
operationNames = [operationName]
}

if (!operationName) {
if (!operationNames || operationNames.length === 0) {
error(`No operation name provided`)
}

Expand All @@ -66,14 +67,17 @@ const graphHandler = async (args, options, command) => {
}

if (schema) {
generateHandlerByOperationName({
generate: codeGenerator.generateHandler,
logger: log,
netlifyGraphConfig,
schema,
operationName,
handlerOptions,
})
/* eslint-disable fp/no-loops */
for (const operationName of operationNames) {
await generateHandlerByOperationName({
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any async behaviour in generateHandlerByOperationName, so do you need to await here? And if not, you could use a forEach and avoid disabling the ESLint rule?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch, @eduardoboucas . All of the generateHandler need to be async now (since they're no longer pure functions) - @dwwoelfel is this PR missing some of the upstream changes?

Copy link
Contributor Author

@dwwoelfel dwwoelfel Aug 19, 2022

Choose a reason for hiding this comment

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

@eduardoboucas Whenever Sean's submits his PR for https://github.com/netlify/cli/compare/sg/graph-async-handler-gen, generateHandlerByOperationName will be async and the await will be necessary.

I think it's fine to merge this now and not make his PR a dependency on this one.

Copy link
Contributor

Choose a reason for hiding this comment

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

@dwwoelfel I'm fine to merge as-is if you want to follow up with the other changes separately.

generate: codeGenerator.generateHandler,
logger: log,
netlifyGraphConfig,
schema,
operationName,
handlerOptions,
})
}
} else {
error(`Failed to parse Netlify GraphQL schema`)
}
Expand All @@ -87,14 +91,14 @@ const graphHandler = async (args, options, command) => {
const createGraphHandlerCommand = (program) =>
program
.command('graph:handler')
.argument('[name]', 'Operation name')
.argument('[name...]', 'Operation name(s)')
.option('-c, --codegen <id>', 'The id of the specific code generator to use')
.option("-d, --data '<json>'", 'Optional data to pass along to the code generator')
.description(
'Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations.',
)
.action(async (operationName, options, command) => {
await graphHandler({ operationName }, options, command)
.action(async (operationNames, options, command) => {
await graphHandler({ operationNames }, options, command)
})

module.exports = { createGraphHandlerCommand }