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: additional graph commands, extra graph codegen tests, and type definitions #4186

Merged
merged 30 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7189c02
chore: add ts-check and fix type errors
sgrove Feb 6, 2022
4277d18
chore: add graph type defs, codegen snapshot test matrix, graph:handl…
sgrove Feb 6, 2022
302a472
chore: fix formatting
sgrove Feb 6, 2022
52a6f2c
fix: handle filenames with spaces in them when running prettier
sgrove Feb 6, 2022
4d39302
feat: add graph:operations command and update codegen
sgrove Feb 7, 2022
a363463
chore: fix formatting
sgrove Feb 7, 2022
61946f3
chore: fix formatting failure
sgrove Feb 7, 2022
dc36c98
chore: update snapshots
sgrove Feb 7, 2022
c3e35a8
chore: update graph:handler docstring with guidance
sgrove Feb 7, 2022
535e5fb
chore: update docs
sgrove Feb 7, 2022
440ad45
chore: fix test and update snapshots
sgrove Feb 7, 2022
dff5876
chore: fix comment
sgrove Feb 7, 2022
42670d1
chore: fix test and update snapshots
sgrove Feb 7, 2022
b757fb4
chore: fix formatting
sgrove Feb 7, 2022
af827fb
chore: slightly improved types
sgrove Feb 7, 2022
cb4622c
fix: function naming and functions path resolution in userSpecified s…
sgrove Feb 7, 2022
ca48ba7
feat: add graph:library command and update the docs
sgrove Feb 7, 2022
50bee80
Merge branch 'main' into graph-codegen-extra-tests
sgrove Feb 7, 2022
eb3cca2
chore: fix comment
sgrove Feb 7, 2022
f9ecb3e
Merge branch 'main' into graph-codegen-extra-tests
sgrove Feb 7, 2022
b18671b
chore: add moduleType to test matrix and regenerate snapshots
sgrove Feb 7, 2022
bebe693
chore: remove commonjs + typescript combo tests in graphql codegen fo…
sgrove Feb 7, 2022
eb9c1d1
chore: fix formatting
sgrove Feb 7, 2022
b7da312
Merge branch 'main' into graph-codegen-extra-tests
erezrokah Feb 7, 2022
363a3dc
Merge branch 'main' into graph-codegen-extra-tests
kodiakhq[bot] Feb 7, 2022
f38cd2c
Merge branch 'main' into graph-codegen-extra-tests
sgrove Feb 7, 2022
f9018aa
fix: grab the operation id from the appropriate location when generat…
sgrove Feb 8, 2022
106846b
chore: add a newline
sgrove Feb 8, 2022
21a78c0
chore: update dep and generate snapshots
sgrove Feb 8, 2022
cb3f22b
fix: integration tests
anmonteiro Feb 8, 2022
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ site/src/**/*.md
tests/integration/hugo-site/resources
tests/integration/hugo-site/out
tests/integration/hugo-site/.hugo_build.lock
_test_out/**

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ Manage netlify functions
| Subcommand | description |
|:--------------------------- |:-----|
| [`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: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
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ Manage netlify functions
| Subcommand | description |
|:--------------------------- |:-----|
| [`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: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
58 changes: 58 additions & 0 deletions docs/commands/graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ netlify graph
| Subcommand | description |
|:--------------------------- |:-----|
| [`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: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 @@ -50,6 +53,61 @@ netlify graph:edit
- `httpProxy` (*string*) - Proxy server address to route requests through.
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server

---
## `graph:handler`

Generate a handler for a Graph operation given its name. See `graph:operations` for a list of operations.

**Usage**

```bash
netlify graph:handler
```

**Arguments**

- name - Operation name

**Flags**

- `debug` (*boolean*) - Print debugging information
- `httpProxy` (*string*) - Proxy server address to route requests through.
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server

---
## `graph:library`

Generate the Graph function library

**Usage**

```bash
netlify graph:library
```

**Flags**

- `debug` (*boolean*) - Print debugging information
- `httpProxy` (*string*) - Proxy server address to route requests through.
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server

---
## `graph:operations`

List all of the locally available operations

**Usage**

```bash
netlify graph:operations
```

**Flags**

- `debug` (*boolean*) - Print debugging information
- `httpProxy` (*string*) - Proxy server address to route requests through.
- `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server

---
## `graph:pull`

Expand Down
3 changes: 3 additions & 0 deletions docs/commands/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ Manage netlify functions
| Subcommand | description |
|:--------------------------- |:-----|
| [`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: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
14 changes: 7 additions & 7 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"url": "https://github.com/netlify/cli/issues"
},
"scripts": {
"snap": "ava --verbose -u",
"prepare": "husky install node_modules/@netlify/eslint-config-node/.husky/",
"start": "node ./bin/run",
"test": "run-s format test:dev",
Expand Down Expand Up @@ -149,7 +150,7 @@
"multiparty": "^4.2.1",
"netlify": "^11.0.0",
"netlify-headers-parser": "^6.0.1",
"netlify-onegraph-internal": "0.0.18",
"netlify-onegraph-internal": "0.0.28",
"netlify-redirect-parser": "^13.0.2",
"netlify-redirector": "^0.2.1",
"node-fetch": "^2.6.0",
Expand Down
5 changes: 3 additions & 2 deletions src/commands/graph/graph-edit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
const gitRepoInfo = require('git-repo-info')

const { OneGraphCliClient, generateSessionName, loadCLISession } = require('../../lib/one-graph/cli-client')
Expand All @@ -15,7 +16,7 @@ const { createCLISession, createPersistedQuery, ensureAppForSite, updateCLISessi
/**
* Creates the `netlify graph:edit` command
* @param {import('commander').OptionValues} options
* @param {import('../base-command').BaseCommand} program
* @param {import('../base-command').BaseCommand} command
* @returns
*/
const graphEdit = async (options, command) => {
Expand Down Expand Up @@ -46,7 +47,7 @@ const graphEdit = async (options, command) => {
let oneGraphSessionId = loadCLISession(state)
if (!oneGraphSessionId) {
const sessionName = generateSessionName()
const oneGraphSession = await createCLISession(netlifyToken, site.id, sessionName)
const oneGraphSession = await createCLISession(netlifyToken, site.id, sessionName, null)
state.set('oneGraphSessionId', oneGraphSession.id)
oneGraphSessionId = state.get('oneGraphSessionId')
}
Expand Down
53 changes: 53 additions & 0 deletions src/commands/graph/graph-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// @ts-check
const {
buildSchema,
generateHandlerByOperationName,
getNetlifyGraphConfig,
readGraphQLSchemaFile,
} = require('../../lib/one-graph/cli-netlify-graph')
const { error } = require('../../utils')

/**
* Creates the `netlify graph:handler` command
* @param {string} operationName
* @param {import('commander').OptionValues} options
* @param {import('../base-command').BaseCommand} command
* @returns
*/
const graphHandler = async (operationName, options, command) => {
const netlifyGraphConfig = await getNetlifyGraphConfig({ command, options })

const schemaString = readGraphQLSchemaFile(netlifyGraphConfig)

let schema

try {
schema = buildSchema(schemaString)
} catch (buildSchemaError) {
error(`Error parsing schema: ${buildSchemaError}`)
}

if (!schema) {
error(`Failed to parse Netlify GraphQL schema`)
}

generateHandlerByOperationName(netlifyGraphConfig, schema, operationName, {})
}

/**
* Creates the `netlify graph:handler` command
* @param {import('../base-command').BaseCommand} program
* @returns
*/
const createGraphHandlerCommand = (program) =>
program
.command('graph:handler')
.argument('<name>', 'Operation name')
.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)
})

module.exports = { createGraphHandlerCommand }
61 changes: 61 additions & 0 deletions src/commands/graph/graph-library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// @ts-check
const {
buildSchema,
defaultExampleOperationsDoc,
extractFunctionsFromOperationDoc,
generateFunctionsFile,
getNetlifyGraphConfig,
parse,
readGraphQLOperationsSourceFile,
readGraphQLSchemaFile,
} = require('../../lib/one-graph/cli-netlify-graph')
const { error } = require('../../utils')

/**
* Creates the `netlify graph:library` command
* @param {import('commander').OptionValues} options
* @param {import('../base-command').BaseCommand} command
* @returns
*/
const graphLibrary = async (options, command) => {
const netlifyGraphConfig = await getNetlifyGraphConfig({ command, options })

const schemaString = readGraphQLSchemaFile(netlifyGraphConfig)

let schema

try {
schema = buildSchema(schemaString)
} catch (buildSchemaError) {
error(`Error parsing schema: ${buildSchemaError}`)
}

if (!schema) {
error(`Failed to parse Netlify GraphQL schema`)
}

let currentOperationsDoc = readGraphQLOperationsSourceFile(netlifyGraphConfig)
if (currentOperationsDoc.trim().length === 0) {
currentOperationsDoc = defaultExampleOperationsDoc
}

const parsedDoc = parse(currentOperationsDoc)
const { fragments, functions } = extractFunctionsFromOperationDoc(parsedDoc)

generateFunctionsFile({ netlifyGraphConfig, schema, operationsDoc: currentOperationsDoc, functions, fragments })
}

/**
* Creates the `netlify graph:library` command
* @param {import('../base-command').BaseCommand} program
* @returns
*/
const createGraphLibraryCommand = (program) =>
program
.command('graph:library')
.description('Generate the Graph function library')
.action(async (options, command) => {
await graphLibrary(options, command)
})

module.exports = { createGraphLibraryCommand }
Loading