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

Validate Queries Plugin #1004

Merged
merged 7 commits into from
Mar 15, 2019
Merged

Validate Queries Plugin #1004

merged 7 commits into from
Mar 15, 2019

Conversation

supernova-at
Copy link
Contributor

@supernova-at supernova-at commented Mar 8, 2019

Description

graphql-cli-validate-magento-pwa-queries

This PR creates a new graphql-cli plugin called validate-magento-pwa-queries.

Venia depends on graphql-cli and this new plugin to do the work of validating the queries, replacing the need for validate-queries.js in packages/venia-concept/ itself.

Venia's validate-queries script runs the plugin.

Magento Compatibility Table

There is a new file called magento-compatibility.js at the root of PWA Studio. It currently looks like this:

/**
 * This file describes PWA Studio to Magento version compatabilities.
 */

// PWA Studio version -> Magento version.
module.exports = {
    '2.0.0': '2.3.0',
    '>2.0.0': '2.3.1'
};

This file is intended to be the source of truth for which versions of PWA are compatible with which versions of Magento.

The file is currently used by a new script (create-compatibility-table.js) in pwa-devdocs that creates a markdown table in its _includes directory for inclusion on the docs site.

It is also used by a new script (summarize-validate-queries.js) in venia-concept that looks up the current version of PWA (based on the version field in the root package.json) and informs developers which versions of Magento are compatible.

📝 This file should definitely be considered a "living document" and we should update it as appropriate during releases.

Related Issue

Closes #952 .

Motivation and Context

From the issue:

As a developer working with PWA Studio, I want to know when breaking changes occur with GraphQL in order to address any incompatibility or breaking changes. We should provide clear messaging/errors to developers regarding where and how to resolve.

Verification

First, yarn clean:all and yarn install to pick up the new dependencies.

Testing 2.3.0

  1. Change the value of MAGENTO_BACKEND_URL in env to https://release-dev-rxvv2iq-zddsyhrdimyra.us-4.magentosite.cloud/graphql.
  2. Run yarn validate-queries.
  3. Verify the following output:
aterrano pwa-studio (supernova/952_validate-queries) $ yarn validate-queries
yarn run v1.13.0
$ yarn workspace @magento/venia-concept run validate-queries
$ yarn run download-schema && graphql validate-magento-pwa-queries --project venia
$ graphql get-schema --project venia
project venia - No changes
Validating GraphQL queries in venia project...
✔ Locating schema...
✔ Finding queries in files...
✔ Validating project's queries against the schema...

All queries are valid.
✨  Done in 6.99s.

Testing 2.3.1

  1. Change the value of MAGENTO_BACKEND_URL in env to https://release-dev-231-npzdaky-zddsyhrdimyra.us-4.magentosite.cloud/.
  2. Run yarn validate-queries.
  3. Verify the following output:
aterrano pwa-studio (supernova/952_validate-queries) $ yarn validate-queries
yarn run v1.13.0
$ yarn workspace @magento/venia-concept run validate-queries
$ yarn run download-schema && graphql validate-magento-pwa-queries --project venia
$ graphql get-schema --project venia
project venia - No changes
Validating GraphQL queries in venia project...
✔ Locating schema...
✔ Finding queries in files...
✔ Validating project's queries against the schema...

Found some invalid queries:

/Users/aterrano/code/pwa-studio/packages/venia-concept/src/queries/getCategory.graphql
  11:21  error  Field "small_image" of type "ProductImage" must have a selection of subfields. Did you mean "small_image { ... }"?  graphql/template-strings

/Users/aterrano/code/pwa-studio/packages/venia-concept/src/queries/getCategoryList.graphql
  15:21  error  Field "small_image" of type "ProductImage" must have a selection of subfields. Did you mean "small_image { ... }"?  graphql/template-strings

/Users/aterrano/code/pwa-studio/packages/venia-concept/src/queries/getNavigationMenu.graphql
  21:11  error  Field "small_image" of type "ProductImage" must have a selection of subfields. Did you mean "small_image { ... }"?  graphql/template-strings

/Users/aterrano/code/pwa-studio/packages/venia-concept/src/queries/getProductDetail.graphql
  14:13  error  Field "description" of type "ComplexTextValue" must have a selection of subfields. Did you mean "description { ... }"?  graphql/template-strings

/Users/aterrano/code/pwa-studio/packages/venia-concept/src/queries/productSearch.graphql
  6:13  error  Field "small_image" of type "ProductImage" must have a selection of subfields. Did you mean "small_image { ... }"?  graphql/template-strings

✖ 5 problems (5 errors, 0 warnings)

Your versions of PWA and Magento may be incompatible.

This Venia store is from PWA Studio version 2.0.0.
Some components in this PWA send GraphQL queries that are only compatible with version 2.3.0 of Magento 2.

Please refer to the compatibility table in the PWA documentation for more details: https://magento-research.github.io/pwa-studio/technologies/magento-compatibility/.
    
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: /usr/local/Cellar/node@10/10.15.0/bin/node
Arguments: /usr/local/Cellar/yarn/1.13.0/libexec/lib/cli.js run validate-queries
Directory: /Users/aterrano/code/pwa-studio/packages/venia-concept
Output:

info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

How Has This Been Tested?

The verification steps above, and yarn test.

Screenshots (if appropriate):

Proposed Labels for Change Type/Package

FEATURE

venia-concept

Checklist:

  • I have read the CONTRIBUTING document.
  • I have linked an issue to this PR.
  • I have indicated the change type and relevant package(s).
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • All CI checks are green (linting, build/deploy, etc).
  • At least one core contributor has approved this PR.

TODOs

  • Incorporate the downloading of the schema into the plugin itself
  • Figure out why validation is coming back successful when the old script produces errors
  • Unit Tests for the plugin
  • Publish the graphql-cli-validate-queries plugin to NPM and install it from there instead of using the local filesystem
  • Determine how to tie in the compatibility table. The plugin itself shouldn't know anything about PWA or Magento. Can we use process.exit(1) effectively?

Followup

  • Open a PR to graphql-cli and get validate-queries listed as a plugin on their main README.

@vercel
Copy link

vercel bot commented Mar 8, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

@supernova-at supernova-at added hold On hold until another condition is fulfilled. in progress labels Mar 8, 2019
@supernova-at
Copy link
Contributor Author

Adding the hold and in progress labels because this has TODOs.

@supernova-at supernova-at added requires followup Some actions need to be taken after this code is merged. and removed requires followup Some actions need to be taken after this code is merged. labels Mar 8, 2019
@vercel vercel bot temporarily deployed to staging March 11, 2019 20:26 Inactive
@supernova-at
Copy link
Contributor Author

The graphql-cli-validate-queries plugin is ready for review.

Remaining steps for this PR:

  1. Publish graphql-cli-validate-queries to npm
  2. Re-add it as a dependency to venia-concept (instead of depending on the local file system).
  3. Try to add additional PWA / Magento specific logging (compatibility table) for when the plugin reports errors.

@coveralls
Copy link

coveralls commented Mar 11, 2019

Coverage Status

Coverage increased (+0.2%) to 76.995% when pulling f3db63e on supernova/952_validate-queries into 37cd2c1 on develop.

packages/graphql-cli-validate-queries/LICENSE.md Outdated Show resolved Hide resolved
packages/graphql-cli-validate-queries/index.js Outdated Show resolved Hide resolved
packages/graphql-cli-validate-queries/index.js Outdated Show resolved Hide resolved
@@ -28,7 +28,7 @@
"apollo-server": "~2.3.1",
"chalk": "~2.4.2",
"csv-parse": "~4.3.0",
"graphql": "~14.0.2",
"graphql": "~14.1.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to bump these versions due to an "incompatibility" bug where multiple instances of the graphql dependency were installed and the plugin didn't know which one to use.

Right now these are manually kept in sync. I would very much welcome a better way to do this, it seems to me like the version of graphql in upward-js and upward-spec should be completely independent.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should this just be a peer dep of the consuming project? IMO the more peer deps we delegate to the final consumer, the better, since that's a great way to prevent duplicate packages in a final bundle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do upward-js and upward-spec even depend on the graphql dependency? I see a single require('graphql-tag') here but other than that I don't see anything.

I also have next to no familiarity with these codebases though 😛

packages/venia-concept/package.json Outdated Show resolved Hide resolved
@supernova-at
Copy link
Contributor Author

supernova-at commented Mar 12, 2019

One last item that I'd appreciate feedback on:

Should pwa-buildpack or peregrine run the plugin too?

My gut is that peregrine will need it but that pwa-buildpack probably won't.

@zetlen
Copy link
Contributor

zetlen commented Mar 12, 2019

@supernova-at I don't think the queries in Peregrine should be in there. @jimbo could convince me otherwise, but I would rather Peregrine be pretty platform-agnostic. Its MagentoRouter uses a handbuilt query to retrieve urlResolver data, but I think it should be refactored so the query can be injected from the project.

Copy link
Contributor

@zetlen zetlen left a comment

Choose a reason for hiding this comment

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

This looks great. Since we eventually want to make a Buildpack CLI that does this Magento-specific stuff (see my comment below), we'll have to come revisit this no matter what. Because of that, I think it's safe to ignore some of the modularity ideas we discussed, and instead just make this plugin a Magento PWA Studio specific graphql-cli plugin, so you can combine the plugin code and the query report code.

"prepublishOnly": "yarn run build",
"start": "node server.js",
"start:debug": "node --inspect-brk ./node_modules/.bin/webpack-dev-server --progress --color --env.mode development",
"stats": "webpack-bundle-analyzer dist/build-stats.json",
"storybook": "start-storybook -p 9001 -c .storybook",
"storybook:build": "build-storybook -c .storybook -o storybook-dist",
"validate-queries": "node ./validate-queries.js",
"validate-queries": "yarn run download-schema && graphql validate-queries --project venia --filesGlob \"src/**/*.{js,graphql,gql}\" --clients apollo literal && node ./summarize-validate-queries.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

Eventually we'd want the same command to both:

  • Confirm that the cached local schema still matches the remote schema
  • Validate all the queries

I figure once we have a real @magento/pwa-buildpack cli, like pbuild, it would look a little more like:

"validate-queries": "pbuild verify-schema && graphql validate-queries | pbuild diagnose-queries"

Speaking of which, can you try making the CLI flags optional, and having them fall back to extension configuration in .graphlconfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up just removing them and always pulling them from .graphqlconfig.

packages/venia-concept/.graphqlconfig Show resolved Hide resolved
packages/venia-concept/summarize-validate-queries.js Outdated Show resolved Hide resolved
packages/venia-concept/summarize-validate-queries.js Outdated Show resolved Hide resolved
packages/venia-concept/summarize-validate-queries.js Outdated Show resolved Hide resolved
Creates a compatibility definition file and script to generate markdown from it

Creates a graphql-cli extension to validate a project's queries

Created a new project / repository called graphql-cli-validate-queries that - based on configuration - goes through all of a project's
GraphQL queries and runs them against an endpoint's schema.

Updates venia-concept to use this new extension and the graphql-cli tool in general.

Resolves #952.

Fixes the format of the graphql/template-strings eslint rule

Adds script to dev-docs to auto-generate the magento compatibility table

Adds a README to the graphql-cli-validate-queries  project

Adds unit tests to graphql-cli-validate-queries plugin.

Installs and wires up Jest for unit testing the validate-queries plugin.

Fixed typo mistakes. (#993)

* Fixed typo mistakes

* Fixed typo mistakes

chore: remove duplicate padding property (#941)

[BUGFIX] [ISSUE-958] Autocomplete renders loading component on clear (#961)

* [BUGFIX] [ISSUE-958] Autocomplete renders loading component on clear

Removes invalid TODO in graphql-cli-validate-queries/index.js

Adds a JSDoc comment block to the getValidator function in graphql-cli-validate-queries

Cleans up the devdoc draft for validate-queries plugin

Adds a PWA-specific summarization to the end of the validate-queries command.

Adds a new script summarize-validate-queries.js to venia-concept
Runs this script at the end of the validate-queries script
This script uses the compatibility table to clue developers to compatibility issues and how to resolve them

Runs prettier

Removes use of reserved word in summarize-validate-queries.js

Create compatibility table page (#1016)

* Fix auto-generated table markdown and re-order versions to list the latest version first

* Incorporate build-compatibility table script into build process

* Create a page to display the compatibility table

* Update pwa-devdocs/src/technologies/magento-compatibility/index.md

Co-Authored-By: jcalcaben <[email protected]>

Updates the path to the compatibility table on the docs site

Updates the wording around the reporting of version specifics and compatibility in the summarize-validate-queries script

Runs prettier and updates summarize-validate-queries.

Fixes dependencies and includes plugin in root jest test.

The graphql-cli-validate-magento-pwa-queries plugins tests are now run as part of the root test command.
This ensures that breaking tests in this project will fail the prepush check.

Cleans up yarn.lock changes by selectively installing dependencies via yarn workspace add commands.

Renames plugin to be more magento and pwa specific.

Runs prettier

Removes unneeded summary script file

Updates the dev docs for the graphql-cli plugin
@supernova-at
Copy link
Contributor Author

I had to rebase / force push because it was insanity to merge yarn.lock after #1006.

I ended up rebasing, deleting yarn.lock, and then yarn installing and checking in the new yarn.lock.

…a-queries from the registry instead of the local file system
@supernova-at
Copy link
Contributor Author

PR Updated:

  • Published graphql-cli-validate-magento-pwa-queries to the NPM registry
  • Updated Venia's dependencies to pull the plugin from the registry instead of the local file system

zetlen
zetlen previously approved these changes Mar 15, 2019
Copy link
Contributor

@zetlen zetlen left a comment

Choose a reason for hiding this comment

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

LGTM, this is a great addition. A good place to start generalizing these tools back into the graphql ecosystem.

() => ({
testEnvironment: 'node'
})
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for sticking with this convention we're doing.

* [graphql-cli](https://github.com/graphql-cli/graphql-cli)
* [eslint-plugin-graphql](https://github.com/apollographql/eslint-plugin-graphql)
* [graphql/no-deprecated-fields rule](https://github.com/apollographql/eslint-plugin-graphql#no-deprecated-fields-validation-rule)

Copy link
Contributor

Choose a reason for hiding this comment

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

I LOVE this documentation. Thank you for making it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just noticed a table formatting error, pushing commit now.

"endpoints": {
"default": "${env:MAGENTO_BACKEND_URL}graphql"
},
"validate-magento-pwa-queries": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Excellent

@@ -45,7 +46,8 @@
"apollo-link-context": "^1.0.14",
"apollo-link-http": "^1.5.11",
"braintree-web-drop-in": "^1.16.0",
"graphql": "^14.1.1",
"graphql-cli": "^3.0.11",
"graphql-cli-validate-magento-pwa-queries": "^1.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really a mouthful. 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol yeah, it was much better as validate-queries but then we made it magento / pwa specific 😝

@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Adobe Inc.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this an official thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Published to NPM so I was just covering all the bases 🤷‍♂️

@zetlen zetlen merged commit cb9b287 into develop Mar 15, 2019
@jimbo jimbo deleted the supernova/952_validate-queries branch March 15, 2019 19:34
"schemaPath": "lastCachedGraphQLSchema.json",
"extensions": {
"endpoints": {
"default": "${env:MAGENTO_BACKEND_URL}graphql"
Copy link
Contributor

Choose a reason for hiding this comment

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

Oops! This should have had a /.

Image from Gyazo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Operational/tooling work for GraphQL
6 participants