Skip to content

Commit 76b6206

Browse files
authored
feat: additional graph commands, extra graph codegen tests, and type definitions (#4186)
* chore: add graph type defs, codegen snapshot test matrix, update codegen dependency * feat: add `graph:handler`, `graph:operations`, and `graph:library` commands
1 parent 1bf95f2 commit 76b6206

23 files changed

+1494
-636
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ site/src/**/*.md
3232
tests/integration/hugo-site/resources
3333
tests/integration/hugo-site/out
3434
tests/integration/hugo-site/.hugo_build.lock
35+
_test_out/**
36+

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ Manage netlify functions
160160
| Subcommand | description |
161161
|:--------------------------- |:-----|
162162
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
163+
| [`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. |
164+
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
165+
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
163166
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
164167

165168

docs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ Manage netlify functions
117117
| Subcommand | description |
118118
|:--------------------------- |:-----|
119119
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
120+
| [`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. |
121+
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
122+
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
120123
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
121124

122125

docs/commands/graph.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ netlify graph
2323
| Subcommand | description |
2424
|:--------------------------- |:-----|
2525
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
26+
| [`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. |
27+
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
28+
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
2629
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
2730

2831

@@ -50,6 +53,61 @@ netlify graph:edit
5053
- `httpProxy` (*string*) - Proxy server address to route requests through.
5154
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server
5255

56+
---
57+
## `graph:handler`
58+
59+
Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations.
60+
61+
**Usage**
62+
63+
```bash
64+
netlify graph:handler
65+
```
66+
67+
**Arguments**
68+
69+
- name - Operation name
70+
71+
**Flags**
72+
73+
- `debug` (*boolean*) - Print debugging information
74+
- `httpProxy` (*string*) - Proxy server address to route requests through.
75+
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server
76+
77+
---
78+
## `graph:library`
79+
80+
Generate the Graph function library
81+
82+
**Usage**
83+
84+
```bash
85+
netlify graph:library
86+
```
87+
88+
**Flags**
89+
90+
- `debug` (*boolean*) - Print debugging information
91+
- `httpProxy` (*string*) - Proxy server address to route requests through.
92+
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server
93+
94+
---
95+
## `graph:operations`
96+
97+
List all of the locally available operations
98+
99+
**Usage**
100+
101+
```bash
102+
netlify graph:operations
103+
```
104+
105+
**Flags**
106+
107+
- `debug` (*boolean*) - Print debugging information
108+
- `httpProxy` (*string*) - Proxy server address to route requests through.
109+
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server
110+
53111
---
54112
## `graph:pull`
55113

docs/commands/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ Manage netlify functions
9898
| Subcommand | description |
9999
|:--------------------------- |:-----|
100100
| [`graph:edit`](/docs/commands/graph.md#graphedit) | Launch the browser to edit your local graph functions from Netlify |
101+
| [`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. |
102+
| [`graph:library`](/docs/commands/graph.md#graphlibrary) | Generate the Graph function library |
103+
| [`graph:operations`](/docs/commands/graph.md#graphoperations) | List all of the locally available operations |
101104
| [`graph:pull`](/docs/commands/graph.md#graphpull) | Pull down your local Netlify Graph schema, and process pending Graph edit events |
102105

103106

npm-shrinkwrap.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"url": "https://github.com/netlify/cli/issues"
4444
},
4545
"scripts": {
46+
"snap": "ava --verbose -u",
4647
"prepare": "husky install node_modules/@netlify/eslint-config-node/.husky/",
4748
"start": "node ./bin/run",
4849
"test": "run-s format test:dev",
@@ -149,7 +150,7 @@
149150
"multiparty": "^4.2.1",
150151
"netlify": "^11.0.0",
151152
"netlify-headers-parser": "^6.0.1",
152-
"netlify-onegraph-internal": "0.0.18",
153+
"netlify-onegraph-internal": "0.0.28",
153154
"netlify-redirect-parser": "^13.0.2",
154155
"netlify-redirector": "^0.2.1",
155156
"node-fetch": "^2.6.0",

src/commands/graph/graph-edit.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
const gitRepoInfo = require('git-repo-info')
23

34
const { OneGraphCliClient, generateSessionName, loadCLISession } = require('../../lib/one-graph/cli-client')
@@ -15,7 +16,7 @@ const { createCLISession, createPersistedQuery, ensureAppForSite, updateCLISessi
1516
/**
1617
* Creates the `netlify graph:edit` command
1718
* @param {import('commander').OptionValues} options
18-
* @param {import('../base-command').BaseCommand} program
19+
* @param {import('../base-command').BaseCommand} command
1920
* @returns
2021
*/
2122
const graphEdit = async (options, command) => {
@@ -46,7 +47,7 @@ const graphEdit = async (options, command) => {
4647
let oneGraphSessionId = loadCLISession(state)
4748
if (!oneGraphSessionId) {
4849
const sessionName = generateSessionName()
49-
const oneGraphSession = await createCLISession(netlifyToken, site.id, sessionName)
50+
const oneGraphSession = await createCLISession(netlifyToken, site.id, sessionName, null)
5051
state.set('oneGraphSessionId', oneGraphSession.id)
5152
oneGraphSessionId = state.get('oneGraphSessionId')
5253
}

src/commands/graph/graph-handler.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// @ts-check
2+
const {
3+
buildSchema,
4+
generateHandlerByOperationName,
5+
getNetlifyGraphConfig,
6+
readGraphQLSchemaFile,
7+
} = require('../../lib/one-graph/cli-netlify-graph')
8+
const { error } = require('../../utils')
9+
10+
/**
11+
* Creates the `netlify graph:handler` command
12+
* @param {string} operationName
13+
* @param {import('commander').OptionValues} options
14+
* @param {import('../base-command').BaseCommand} command
15+
* @returns
16+
*/
17+
const graphHandler = async (operationName, options, command) => {
18+
const netlifyGraphConfig = await getNetlifyGraphConfig({ command, options })
19+
20+
const schemaString = readGraphQLSchemaFile(netlifyGraphConfig)
21+
22+
let schema
23+
24+
try {
25+
schema = buildSchema(schemaString)
26+
} catch (buildSchemaError) {
27+
error(`Error parsing schema: ${buildSchemaError}`)
28+
}
29+
30+
if (!schema) {
31+
error(`Failed to parse Netlify GraphQL schema`)
32+
}
33+
34+
generateHandlerByOperationName(netlifyGraphConfig, schema, operationName, {})
35+
}
36+
37+
/**
38+
* Creates the `netlify graph:handler` command
39+
* @param {import('../base-command').BaseCommand} program
40+
* @returns
41+
*/
42+
const createGraphHandlerCommand = (program) =>
43+
program
44+
.command('graph:handler')
45+
.argument('<name>', 'Operation name')
46+
.description(
47+
'Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations.',
48+
)
49+
.action(async (operationName, options, command) => {
50+
await graphHandler(operationName, options, command)
51+
})
52+
53+
module.exports = { createGraphHandlerCommand }

src/commands/graph/graph-library.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// @ts-check
2+
const {
3+
buildSchema,
4+
defaultExampleOperationsDoc,
5+
extractFunctionsFromOperationDoc,
6+
generateFunctionsFile,
7+
getNetlifyGraphConfig,
8+
parse,
9+
readGraphQLOperationsSourceFile,
10+
readGraphQLSchemaFile,
11+
} = require('../../lib/one-graph/cli-netlify-graph')
12+
const { error } = require('../../utils')
13+
14+
/**
15+
* Creates the `netlify graph:library` command
16+
* @param {import('commander').OptionValues} options
17+
* @param {import('../base-command').BaseCommand} command
18+
* @returns
19+
*/
20+
const graphLibrary = async (options, command) => {
21+
const netlifyGraphConfig = await getNetlifyGraphConfig({ command, options })
22+
23+
const schemaString = readGraphQLSchemaFile(netlifyGraphConfig)
24+
25+
let schema
26+
27+
try {
28+
schema = buildSchema(schemaString)
29+
} catch (buildSchemaError) {
30+
error(`Error parsing schema: ${buildSchemaError}`)
31+
}
32+
33+
if (!schema) {
34+
error(`Failed to parse Netlify GraphQL schema`)
35+
}
36+
37+
let currentOperationsDoc = readGraphQLOperationsSourceFile(netlifyGraphConfig)
38+
if (currentOperationsDoc.trim().length === 0) {
39+
currentOperationsDoc = defaultExampleOperationsDoc
40+
}
41+
42+
const parsedDoc = parse(currentOperationsDoc)
43+
const { fragments, functions } = extractFunctionsFromOperationDoc(parsedDoc)
44+
45+
generateFunctionsFile({ netlifyGraphConfig, schema, operationsDoc: currentOperationsDoc, functions, fragments })
46+
}
47+
48+
/**
49+
* Creates the `netlify graph:library` command
50+
* @param {import('../base-command').BaseCommand} program
51+
* @returns
52+
*/
53+
const createGraphLibraryCommand = (program) =>
54+
program
55+
.command('graph:library')
56+
.description('Generate the Graph function library')
57+
.action(async (options, command) => {
58+
await graphLibrary(options, command)
59+
})
60+
61+
module.exports = { createGraphLibraryCommand }

0 commit comments

Comments
 (0)