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

Add support for printing dictionaries when using custom scalars #2048

Merged
merged 15 commits into from Jul 29, 2022

Conversation

patrick91
Copy link
Member

@patrick91 patrick91 commented Jul 28, 2022

This PR add support for printing dictionaries as default values, for example:

@strawberry.input
class MyInput:
    j: JSON = strawberry.field(default_factory=dict)
    j2: JSON = strawberry.field(default_factory=lambda: {"hello": "world"})

@strawberry.type
class Query:
    @strawberry.field
    def search(self, input: MyInput) -> JSON:
        return input.j

will print the following

input MyInput {
    j: JSON! = {}
    j2: JSON! = {hello: "world"}
}

type Query {
    search(input: MyInput!): JSON!
}

Here's an example of this working in another server: https://codesandbox.io/s/apollo-json-default-value-4mkreh?file=/index.js:0-899

TODO:

  • allow nested dictionaries
  • this, but also for arguments

Description

Types of Changes

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

@codecov
Copy link

codecov bot commented Jul 28, 2022

Codecov Report

Merging #2048 (047cbbb) into main (46f5f59) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #2048      +/-   ##
==========================================
+ Coverage   98.03%   98.05%   +0.02%     
==========================================
  Files         145      147       +2     
  Lines        5797     5874      +77     
  Branches     1080     1107      +27     
==========================================
+ Hits         5683     5760      +77     
  Misses         58       58              
  Partials       56       56              

@botberry
Copy link
Member

botberry commented Jul 28, 2022

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release adds support for printing default values for scalars like JSON.

For example the following:

import strawberry
from strawberry.scalars import JSON


@strawberry.input
class MyInput:
    j: JSON = strawberry.field(default_factory=dict)
    j2: JSON = strawberry.field(default_factory=lambda: {"hello": "world"})

will print the following schema:

input MyInput {
    j: JSON! = {}
    j2: JSON! = {hello: "world"}
}

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)

@patrick91 patrick91 force-pushed the feature/printer-scalar-dicts branch from abf6fc0 to 259bf0c Compare July 28, 2022 15:50
@patrick91 patrick91 force-pushed the feature/printer-scalar-dicts branch from cba78b5 to 3de9f1d Compare July 28, 2022 18:15
strawberry/printer/ast_from_value.py Show resolved Hide resolved
strawberry/printer/printer.py Show resolved Hide resolved
RELEASE.md Show resolved Hide resolved
@patrick91
Copy link
Member Author

@BryceBeagle I'll merge this since I need it for the federation 2.0 fixes (#2047)

I'll open an issue about rewriting the printer 😊

@patrick91 patrick91 merged commit ce4f702 into main Jul 29, 2022
@patrick91 patrick91 deleted the feature/printer-scalar-dicts branch July 29, 2022 10:16
nrbnlulu added a commit to nrbnlulu/strawberry that referenced this pull request Aug 1, 2022
* Bump ddtrace from 1.2.3 to 1.3.0 (strawberry-graphql#2050)

Bumps [ddtrace](https://github.com/DataDog/dd-trace-py) from 1.2.3 to 1.3.0.
- [Release notes](https://github.com/DataDog/dd-trace-py/releases)
- [Changelog](https://github.com/DataDog/dd-trace-py/blob/1.x/CHANGELOG.md)
- [Commits](DataDog/dd-trace-py@v1.2.3...v1.3.0)

---
updated-dependencies:
- dependency-name: ddtrace
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump types-setuptools from 63.2.1 to 63.2.2 (strawberry-graphql#2049)

Bumps [types-setuptools](https://github.com/python/typeshed) from 63.2.1 to 63.2.2.
- [Release notes](https://github.com/python/typeshed/releases)
- [Commits](https://github.com/python/typeshed/commits)

---
updated-dependencies:
- dependency-name: types-setuptools
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Add support for printing dictionaries when using custom scalars (strawberry-graphql#2048)

* Add support for printing dictionaries when using custom scalars

* Add support for nested dicts

* Add support for arguments

* Add test for bool and float

* Add test for id

* Test for list with values

* Add comment back

* Fix variable

* More tests

* More tests for lists

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add support for objects as default values

* Test more cases

* Ignore code paths

* Add one more test for floats

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Release 🍓 0.122.0

* Remove autoreload test (strawberry-graphql#2053)

* Fix aiohttp query operation selection (strawberry-graphql#2055)

* Release 🍓 0.122.1

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Patrick Arminio <patrick.arminio@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Botberry <bot@strawberry.rocks>
Co-authored-by: Jonathan Ehwald <github@ehwald.info>
bellini666 added a commit that referenced this pull request Aug 12, 2022
* Update dependencies

Add Channels optional dependency and an Extra package for channels

* Add documentation for Channels integration

* Add implementation for basic Channels integration

Complete implementation for websockets, partial implementation for HTTP (multipart/form-data still pending)

* Add unit tests for Channels integration

These unit tests are mostly copied over from FastAPI, and certainly aren't comprehensive or covering all the Channels-specific cases. However, they provide a reasonably good level of coverage for actual protocol behaviour.

The HTTP consumer does not yet have any unit tests.

* Add channels as dev dependency

* Fix code for python 3.7

* Update docs formatting

* Add release notes

* Fix typo

* Initial fixes for GET request

* Fix http handler

* fix: Handle disconnect should also reap completed tasks

* test: Fix channels websockets tests

* chore: Fix typing issues

* refactor: Pass the original error message along instead of a custom one

* fix: Fix urlpattern to handle `^graphql` instead of `^graph`

* feat: Finish HttpConsumer and also implement a sync version, with full testing coverage

* refactor: Inherit all handlers from the same base and better organize everything

* feat: Add channels layer typing protocol

* feat: Add a high level api for layers communication

* docs: Document how to use the high level layers event listener

* fix: Import Protocol from typing_extensions

* fix: Fix a typing issue with override

* fix: Fix the pyright test that just broke today

* test: Cover channels integration 100%

* fix: Pass response already encoded so that graphiql result doesn't get json encoded

* fix: Fix should_render_graphiql checking

* refactor: Handle invalid operation generically

* docs: Fix a small typo on documentation

* feat: Check for layers integration and give a better error message in case it is not found

* docs: Add a missing comma in documentation example

* check channels docs.

* check channels docs - fix .md references from camelCase to no case.

* check channels docs - added gql queries

* add tests docs

* add tests docs - added note about pytest.

* Fix .md code block annotation

Co-authored-by: Patrick Arminio <patrick.arminio@gmail.com>

* fixed grammar more clear explanation, formatting

* fix 404

* refactor: Only allow dotted access to channels context

* feat: Properly type channel_listen method

* channels docs - fit

* update fork (#1)

* Bump ddtrace from 1.2.3 to 1.3.0 (#2050)

Bumps [ddtrace](https://github.com/DataDog/dd-trace-py) from 1.2.3 to 1.3.0.
- [Release notes](https://github.com/DataDog/dd-trace-py/releases)
- [Changelog](https://github.com/DataDog/dd-trace-py/blob/1.x/CHANGELOG.md)
- [Commits](DataDog/dd-trace-py@v1.2.3...v1.3.0)

---
updated-dependencies:
- dependency-name: ddtrace
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump types-setuptools from 63.2.1 to 63.2.2 (#2049)

Bumps [types-setuptools](https://github.com/python/typeshed) from 63.2.1 to 63.2.2.
- [Release notes](https://github.com/python/typeshed/releases)
- [Commits](https://github.com/python/typeshed/commits)

---
updated-dependencies:
- dependency-name: types-setuptools
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Add support for printing dictionaries when using custom scalars (#2048)

* Add support for printing dictionaries when using custom scalars

* Add support for nested dicts

* Add support for arguments

* Add test for bool and float

* Add test for id

* Test for list with values

* Add comment back

* Fix variable

* More tests

* More tests for lists

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add support for objects as default values

* Test more cases

* Ignore code paths

* Add one more test for floats

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Release 🍓 0.122.0

* Remove autoreload test (#2053)

* Fix aiohttp query operation selection (#2055)

* Release 🍓 0.122.1

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Patrick Arminio <patrick.arminio@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Botberry <bot@strawberry.rocks>
Co-authored-by: Jonathan Ehwald <github@ehwald.info>

* Update docs/integrations/django.md

Co-authored-by: Jonathan Ehwald <github@ehwald.info>

* refactor: Remove not needed connection init timeout task cancellation

* refactor: s/import pathlib/from pathlib import Path/

* refactor: Default groups to an empty tuple

* chore: Add back channels dev dependency

* test: Fix tests that broke after last change to channel_listen

* docs: Add an explanation to why GraphqlWSHandler implements a handle_invalid_message method

* Update strawberry/channels/handlers/http_handler.py

Co-authored-by: Hans Huber <69171298+hans-brilliant@users.noreply.github.com>

* test: Fix tests that broke after last change

* Add log and missing marker

* Update release notes

* Minor docs update

* Add tweet file

* Docs update

Co-authored-by: Patrick Arminio <patrick.arminio@gmail.com>
Co-authored-by: Thiago Bellini Ribeiro <thiago@bellini.dev>
Co-authored-by: Nir <88795475+nrbnlulu@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Botberry <bot@strawberry.rocks>
Co-authored-by: Jonathan Ehwald <github@ehwald.info>
Co-authored-by: Thiago Bellini Ribeiro <hackedbellini@gmail.com>
Co-authored-by: Hans Huber <69171298+hans-brilliant@users.noreply.github.com>
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.

None yet

3 participants