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

Implement codegen for queries #1655

Merged
merged 48 commits into from
Mar 14, 2022
Merged

Implement codegen for queries #1655

merged 48 commits into from
Mar 14, 2022

Conversation

patrick91
Copy link
Member

@patrick91 patrick91 commented Feb 19, 2022

Description

This is WIP 😊 This PR implements a feature similar to GraphQL codegen that allows to transform a GraphQL query into code, at the moment I'll be targeting only Python[1] code, but this can be expanded to other languages too (maybe via installable plugins).

[1] The python codegen will be a plugin. We'll be providing a generic interface that will make use of plugins to generate code.

Note: the code is kinda recreating GraphQLType, GraphQLScalar and so on. They are meant as tiny datastructures that will be used to generate code :)

TODO:

There are some todo left in the code, on top of that we need:

  • Move codegen to a plugin (so we can have plugins for Python, TypeScript and so on)
  • Add a CLI interface (use rich?)
  • Proper errors
  • Improve internal interface for getting types (?)
  • Add plugin hooks to customise output
  • Variables and arguments support
  • Support for returning multiple files
  • Mark this as experimental in the CLI command

In other PRs:

  • pip install strawberry-graphql[cli]
  • Support for CLI options from the CLI plugins
  • Plugin for future python codestyle (using |, list) could be enabled based on the version

/cc @peniqliotuv

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

@codecov
Copy link

codecov bot commented Feb 19, 2022

Codecov Report

Merging #1655 (c6cf2a7) into main (731d041) will increase coverage by 0.14%.
The diff coverage is 99.51%.

@@            Coverage Diff             @@
##             main    #1655      +/-   ##
==========================================
+ Coverage   98.19%   98.34%   +0.14%     
==========================================
  Files         130      139       +9     
  Lines        4596     5185     +589     
  Branches      794      936     +142     
==========================================
+ Hits         4513     5099     +586     
  Misses         44       44              
- Partials       39       42       +3     

@patrick91 patrick91 force-pushed the feature/query-codegen branch 3 times, most recently from d90b059 to 7604141 Compare February 23, 2022 21:39
@patrick91
Copy link
Member Author

might be worth using this for tests: https://github.com/joseph-roitman/pytest-snapshot

@patrick91 patrick91 force-pushed the feature/query-codegen branch 2 times, most recently from 158d2fd to dc6626d Compare March 4, 2022 18:02
@patrick91 patrick91 marked this pull request as ready for review March 4, 2022 19:03
@botberry
Copy link
Member

botberry commented Mar 4, 2022

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release adds an experimental codegen feature for queries.
It allows to combine a graphql query and Strawberry schema to generate
Python types or TypeScript types.

You can use the following command:

strawberry codegen --schema schema --output-dir ./output -p python query.graphql

to generate python types that correspond to your GraphQL query.


Here's the preview release card for twitter:

Here's the tweet text:

🆕 Release (next) is out! Thanks to @patrick91 for the PR 👏

Get it here 👉 https://github.com/strawberry-graphql/strawberry/releases/tag/(next)

@github-actions
Copy link

github-actions bot commented Mar 4, 2022

Hi 👋 You can find a preview of the docs here:

https://strawberry.rocks/docs/pr/1655/README
https://strawberry.rocks/docs/pr/1655/codegen/codegen

@patrick91 patrick91 force-pushed the feature/query-codegen branch 4 times, most recently from f9c20f8 to 2c1deae Compare March 7, 2022 18:28
@patrick91
Copy link
Member Author

/pre-release

@botberry
Copy link
Member

botberry commented Mar 7, 2022

Pre-release

👋

Pre-release 0.103.0.dev.1647276413 [8ca8d37] has been released on PyPi! 🚀
You can try it by doing:

poetry add strawberry-graphql==0.103.0.dev.1647276413

@strawberry-graphql strawberry-graphql deleted a comment from botberry Mar 8, 2022
@patrick91
Copy link
Member Author

/pre-release

@patrick91 patrick91 force-pushed the feature/query-codegen branch 5 times, most recently from 2c5ec70 to cf4cbc1 Compare March 14, 2022 16:35
@patrick91
Copy link
Member Author

/pre-release

@patrick91 patrick91 merged commit 66bb1b4 into main Mar 14, 2022
@patrick91 patrick91 deleted the feature/query-codegen branch March 14, 2022 21:43
msobas pushed a commit to msobas/strawberry that referenced this pull request Apr 10, 2022
* Implement codegen for queries

* Add support for imports

* Move python codegen to a plugin

* Better print handling

* Wip

* WIP

* Implement initial typescript plugin

* Some type fixes

* Fix codegen

* Implement cli

* fix enum

* Fix tests

* Move tests to snapshot

* Update types

* Add some error handling

* wip

* Initial implementation of allowing to generate code from operation

* Directives

* More scalars

* Move plugin from test to core

* Add initial support for specifing plugins from CLI

* Initial support for variables

* Improve support for variables

* Collect nested input types

* Refactor fix types

* Initial support for returning multiple files

* Support multiple files per plugin

* Update command

* Test CLI

* Increase test coverage

* Add imports for scalars

* Write to files

* Add some output

* Add release notes

* Add docs and update cli interface

* Fix issue with mypy cache

* Fix type

* Add support for list values

* Add support for enum values

* Use pathlib for better writing

* wip cli plugin

* Improve CLI plugin

* Store types for operation and variables

* Update types

* Update docs

* Allow to override console plugin

* Fix type

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

Successfully merging this pull request may close these issues.

3 participants