forked from magento/pwa-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update queries for latest 2.3 schema and add validator script (magent…
…o#318) * fix: update query for small_image schema change * feat(venia): Script to lint queries against schema
- Loading branch information
Showing
13 changed files
with
506 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require('dotenv').config(); | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; | ||
const magentoDomainVarName = 'MAGENTO_BACKEND_DOMAIN'; | ||
const magentoDomain = process.env[magentoDomainVarName]; | ||
if (!magentoDomain) { | ||
console.error( | ||
`No ${magentoDomainVarName} environment variable specified. Have you created a .env file?` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const { URL } = require('url'); | ||
let uri; | ||
try { | ||
uri = new URL('/graphql', magentoDomain); | ||
} catch (e) { | ||
console.error( | ||
`Could not build a GraphQL endpoint URL from env var ${magentoDomainVarName}: '${magentoDomain}'.` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const { gql, HttpLink, makePromise, execute } = require('apollo-boost'); | ||
const { getIntrospectionQuery, introspectionQuery } = require('graphql'); | ||
|
||
const query = gql( | ||
getIntrospectionQuery ? getIntrospectionQuery() : introspectionQuery | ||
); | ||
|
||
const link = new HttpLink({ uri, fetch: require('node-fetch') }); | ||
|
||
async function getSchema() { | ||
console.log(`Validating queries based on schema at ${uri.href}...`); | ||
return makePromise(execute(link, { query })); | ||
} | ||
|
||
async function getRuleConfig() { | ||
const schemaJson = await getSchema(); | ||
console.log(`Retrieved introspection query. Configuring validator...`); | ||
return [ | ||
'error', | ||
{ | ||
env: 'apollo', | ||
projectName: 'magento', | ||
schemaJson | ||
}, | ||
{ | ||
env: 'literal', | ||
projectName: 'magento', | ||
schemaJson | ||
} | ||
]; | ||
} | ||
|
||
async function getLinterConfig() { | ||
const ruleConfig = await getRuleConfig(); | ||
console.log(`Validator configured. Running validator...`); | ||
return { | ||
parser: 'babel-eslint', | ||
rules: { | ||
'graphql/template-strings': ruleConfig | ||
}, | ||
plugins: ['graphql'], | ||
useEslintrc: false | ||
}; | ||
} | ||
|
||
async function validateQueries() { | ||
const linterConfig = await getLinterConfig(); | ||
const CLIEngine = require('eslint').CLIEngine; | ||
const cli = new CLIEngine(linterConfig); | ||
const files = cli.resolveFileGlobPatterns(['src/**/*.{js,graphql,gql}']); | ||
const report = cli.executeOnFiles(files); | ||
if (report.errorCount > 0) { | ||
console.error(`Errors found!`); | ||
const formatter = cli.getFormatter(); | ||
process.stderr.write(formatter(report.results)); | ||
console.error( | ||
` | ||
These errors may indicate: | ||
- an out-of-date Magento 2.3 codebase running at "${magentoDomain}" | ||
- an out-of-date project codebase whose queries need updating | ||
Use GraphiQL or another schema exploration tool on the Magento store to learn more. | ||
` | ||
); | ||
process.exit(1); | ||
} else { | ||
console.log( | ||
`Validation passed! All queries found are valid for schema.` | ||
); | ||
process.exit(0); | ||
} | ||
} | ||
|
||
validateQueries(); |