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: add graph:init command #4711

Merged
merged 12 commits into from
Aug 1, 2022
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Manage netlify functions
| [`graph:config:write`](/docs/commands/graph.md#graphconfigwrite) | Write a .graphqlrc.json file to the current directory for use with local tooling (e.g. the graphql extension for vscode) |
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
| [`graph:handler`](/docs/commands/graph.md#graphhandler) | Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations. |
| [`graph:init`](/docs/commands/graph.md#graphinit) | Initialize all the resources for Netlify Graph |
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Manage netlify functions
| [`graph:config:write`](/docs/commands/graph.md#graphconfigwrite) | Write a .graphqlrc.json file to the current directory for use with local tooling (e.g. the graphql extension for vscode) |
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
| [`graph:handler`](/docs/commands/graph.md#graphhandler) | Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations. |
| [`graph:init`](/docs/commands/graph.md#graphinit) | Initialize all the resources for Netlify Graph |
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
Expand Down
18 changes: 18 additions & 0 deletions docs/commands/graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ netlify graph
| [`graph:config:write`](/docs/commands/graph.md#graphconfigwrite) | Write a .graphqlrc.json file to the current directory for use with local tooling (e.g. the graphql extension for vscode) |
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
| [`graph:handler`](/docs/commands/graph.md#graphhandler) | Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations. |
| [`graph:init`](/docs/commands/graph.md#graphinit) | Initialize all the resources for Netlify Graph |
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
Expand Down Expand Up @@ -92,6 +93,23 @@ netlify graph:handler
- `httpProxy` (*string*) - Proxy server address to route requests through.
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server

---
## `graph:init`

Initialize all the resources for Netlify Graph

**Usage**

```bash
netlify graph:init
```

**Flags**

- `debug` (*boolean*) - Print debugging information
- `httpProxy` (*string*) - Proxy server address to route requests through.
anmonteiro marked this conversation as resolved.
Show resolved Hide resolved
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server

---
## `graph:library`

Expand Down
1 change: 1 addition & 0 deletions docs/commands/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Manage netlify functions
| [`graph:config:write`](/docs/commands/graph.md#graphconfigwrite) | Write a .graphqlrc.json file to the current directory for use with local tooling (e.g. the graphql extension for vscode) |
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
| [`graph:handler`](/docs/commands/graph.md#graphhandler) | Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations. |
| [`graph:init`](/docs/commands/graph.md#graphinit) | Initialize all the resources for Netlify Graph |
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
Expand Down
116 changes: 116 additions & 0 deletions src/commands/graph/graph-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// @ts-check
const { Buffer } = require('buffer')

const { v4: uuidv4 } = require('uuid')

const { OneGraphCliClient, ensureCLISession } = require('../../lib/one-graph/cli-client')
const { chalk, error, log } = require('../../utils')

const { ensureAppForSite, executeCreateApiTokenMutation } = OneGraphCliClient

/**
* Creates the `netlify graph:init` command
* @param {import('commander').OptionValues} options
* @param {import('../base-command').BaseCommand} command
* @returns
*/
const graphInit = async (options, command) => {
const { api, site, state } = command.netlify
const siteId = site.id

if (!siteId) {
error(
`No siteId defined, unable to start Netlify Graph. To enable, run ${chalk.yellow(
'netlify init',
)} or ${chalk.yellow('netlify link')}`,
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[sand] It looks like we're using a slightly different error log with graph-edit. In the interest of consistency, we should probably make them the same.

Looks like we're not importing NETLIFYDEVERR from utils in graph-init. Would need that to make the two error logs more consistent.


const netlifyToken = await command.authenticate()
// @ts-ignore: we need better types for our api object
const siteData = await api.getSite({ siteId })

await ensureAppForSite(netlifyToken, siteId)

await ensureCLISession({
metadata: {},
netlifyToken,
site,
state,
})

let envChanged = false

// Get current environment variables set in the UI
const {
build_settings: { env = {} },
} = siteData

const newEnv = {
...env,
}

if (!env.NETLIFY_GRAPH_WEBHOOK_SECRET) {
envChanged = true
const secret = Buffer.from(uuidv4()).toString('base64')
newEnv.NETLIFY_GRAPH_WEBHOOK_SECRET = secret
}

if (!env.NETLIFY_GRAPH_PERSIST_QUERY_TOKEN) {
const variables = {
nfToken: netlifyToken,
input: {
appId: siteId,
scopes: ['MODIFY_SCHEMA', 'PERSIST_QUERY'],
},
}

const result = await executeCreateApiTokenMutation(variables, {
siteId,
})

const token =
result.data &&
result.data.oneGraph &&
result.data.oneGraph.createApiToken &&
result.data.oneGraph.createApiToken.accessToken &&
result.data.oneGraph.createApiToken.accessToken.token

if (token) {
envChanged = true
newEnv.NETLIFY_GRAPH_PERSIST_QUERY_TOKEN = token
} else {
error(`Unable to create Netlify Graph persist query token: ${JSON.stringify(result.errors, null, 2)}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

[pebble] This error message explains the "what", but not the "why" or "what now". With error messages, we try to describe what happened, why it happened, and, if at all possible, what the user can do about it.

The error message for no side id above is a good example. Is there any way we can can explain why the error happened and, perhaps, guide users in what they can do here?

Copy link
Contributor

Choose a reason for hiding this comment

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

The errors we're printing should give us more information about what happened.

}
}

if (envChanged) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to always be true.

// Apply environment variable updates
// @ts-ignore
await api.updateSite({
siteId,
body: {
build_settings: {
env: newEnv,
},
},
})

log(`Finished updating Graph-related environment variables for site ${siteData.name}`)
}
}

/**
* Creates the `netlify graph:init` command
* @param {import('../base-command').BaseCommand} program
* @returns
*/
const createGraphInitCommand = (program) =>
program
.command('graph:init')
.description('Initialize all the resources for Netlify Graph')
.action(async (options, command) => {
await graphInit(options, command)
})

module.exports = { createGraphInitCommand }
2 changes: 2 additions & 0 deletions src/commands/graph/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const { createGraphConfigWriteCommand } = require('./graph-config-write')
const { createGraphEditCommand } = require('./graph-edit')
const { createGraphHandlerCommand } = require('./graph-handler')
const { createGraphInitCommand } = require('./graph-init')
const { createGraphLibraryCommand } = require('./graph-library')
const { createGraphOperationCommand } = require('./graph-operations')
const { createGraphPullCommand } = require('./graph-pull')
Expand All @@ -24,6 +25,7 @@ const createGraphCommand = (program) => {
createGraphConfigWriteCommand(program)
createGraphEditCommand(program)
createGraphHandlerCommand(program)
createGraphInitCommand(program)
createGraphLibraryCommand(program)
createGraphOperationCommand(program)
createGraphPullCommand(program)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/snapshots/220.command.graph.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Generated by [AVA](https://avajs.dev).
$ graph:config:write Write a .graphqlrc.json file to the current directory for use with local tooling (e.g. the graphql extension for vscode)␊
$ graph:edit Launch the browser to edit your local graph functions from Netlify␊
$ graph:handler Generate a handler for a Graph operation given its name. See \`graph:operations\` for a list of operations.␊
$ graph:init Initialize all the resources for Netlify Graph␊
$ graph:library Generate the Graph function library␊
$ graph:operations List all of the locally available operations␊
$ graph:pull Pull down your local Netlify Graph schema, and process pending Graph edit events␊
Expand Down Expand Up @@ -55,6 +56,7 @@ Generated by [AVA](https://avajs.dev).
$ graph:config:write Write a .graphqlrc.json file to the current directory for use with local tooling (e.g. the graphql extension for vscode)␊
$ graph:edit Launch the browser to edit your local graph functions from Netlify␊
$ graph:handler Generate a handler for a Graph operation given its name. See \`graph:operations\` for a list of operations.␊
$ graph:init Initialize all the resources for Netlify Graph␊
$ graph:library Generate the Graph function library␊
$ graph:operations List all of the locally available operations␊
$ graph:pull Pull down your local Netlify Graph schema, and process pending Graph edit events␊
Expand Down
Binary file modified tests/integration/snapshots/220.command.graph.test.js.snap
Binary file not shown.