strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Add SDL to strawberry conversion tool

Open patrick91 opened this issue 5 years ago • 26 comments

It would be nice to have a command to convert an SDL to strawberry types, something like:

type User {
  name: String!
}

type Query {
  user: User
}
$ strawberry import-schema path.grapqhl
import typing
import strawberry

@strawberry.type
class User:
    name: str

@strawberry.type
class Query:
    user: typing.Optional[User]

This should support description and directives if possible :)

patrick91 avatar Sep 25 '19 15:09 patrick91

@patrick91 I want to work on this. How to get started on this?

Jatinbalodhi96 avatar Sep 25 '19 15:09 Jatinbalodhi96

@Jatinbalodhi96 I'll assign you to this issue :)

We have a small contribution guide here: https://github.com/strawberry-graphql/strawberry/blob/master/CONTRIBUTING.md#contributing-to-code

We also have a cli setup (which should be refactored I reckon): https://github.com/strawberry-graphql/strawberry/blob/master/strawberry/cli/init.py

So maybe you can start from that, what do you think?

patrick91 avatar Sep 25 '19 16:09 patrick91

thanks, @patrick91 I followed the guide but while running I faced two errors:

  • [x] - poetry run pytest tests it gives me ModuleNotFoundError: No module named 'termios' error, after searching figure out that this library is for Linux only.
  • [x] While import strawberry I got no Module named 'graphql' even I already ran poetry install which ran.
  • [ ] Unable to install graphql by running poetry add graphql getting building 'graphql_ext' extension error: unknown file type '.pyx' (from 'graphql/graphql_ext.pyx') error

Jatinbalodhi96 avatar Sep 25 '19 19:09 Jatinbalodhi96

@Jatinbalodhi96 uh, never seen that error, what os and python version are using?

patrick91 avatar Sep 25 '19 20:09 patrick91

I'm using python 3.7 on windows 10, Now I'm able to run

  • I'm able run strawberry now but i installed graphql seperately using pipenv install --pre "graphql-core>=3a"

The error I sent earlier was because I was installing graphql using poetry add graphql, I'm not sure if it works same for all.

Jatinbalodhi96 avatar Sep 25 '19 20:09 Jatinbalodhi96

@Jatinbalodhi96 oh interest, I never tried strawberry or event GraphQL core on windows, I'll see if I can add tests for that, I don't have a windows machine at the moment, so it might take me a while to test this :)

patrick91 avatar Sep 25 '19 20:09 patrick91

@patrick91 I've never used poetry before but is this correct way to build the dev version. image

because I'm unable to run strawberry cli, but able to import strawberry.

Jatinbalodhi96 avatar Sep 25 '19 21:09 Jatinbalodhi96

@Jatinbalodhi96 yes :)

can you try with

poetry run strawberry ?

patrick91 avatar Sep 25 '19 21:09 patrick91

@patrick91 thanks this command run cli, but also I'm unable to import strawberry if I'm outside the directory, so like we do with pip for dependency installation pip install -e .

Jatinbalodhi96 avatar Sep 25 '19 21:09 Jatinbalodhi96

@Jatinbalodhi96 yes, poetry works a bit differently than pip, here's the docs on add: https://poetry.eustace.io/docs/cli/#add

do use it like pip install -e . you need another poetry project as far as I understand :)

patrick91 avatar Sep 25 '19 21:09 patrick91

Hey, I'm interested in working on this too, this would be my first contribution ever... Is it possible for two people to be working the same issue?

srbiotik avatar Feb 14 '20 17:02 srbiotik

@srbiotik no one is working on this at the moment, feel free to try! Ping me if you need some help 😉

patrick91 avatar Feb 14 '20 18:02 patrick91

@patrick91 Thanks a bunch, can't wait to sink my teeth into this!

srbiotik avatar Feb 14 '20 20:02 srbiotik

@patrick91 Hey, I'm implementing an optional recursive parameter for resolving import statements. This would allow for large schemes to be split up into files and still get parsed without calling the command multiple times. What do you think about this?

srbiotik avatar Feb 15 '20 18:02 srbiotik

@srbiotik large graphql schemas? that's interesting!

Personally I'd focus on being able to import a whole schema (even if it is one single file) and then add the support for import statements :)

patrick91 avatar Feb 15 '20 23:02 patrick91

@patrick91 I'm working on distinguishing required fields from optional. I approached the problem by comparing swapi graphql schema and split it into files which are located at tests/cli/helpers folder, since I can compare my output to the your swapi repository. There is one thing that kind of bugs me, why do some optional fields have = None and others not? When I look at the SDL there is no difference between them, although I see you made the swapi some 10 months ago so it's possible there has been some change in the SDL since then.

srbiotik avatar Feb 19 '20 22:02 srbiotik

The swapi repository I've worked on is quite outdated 😊

Anyway, the main reason there's = None in some places is because dataclasses don't set a default value for the field passed as optional:

image

It shouldn't make a difference for the SDL though, regarding SDL to Strawberry types, maybe we can put an option that adds = None to optional fields?

patrick91 avatar Feb 20 '20 09:02 patrick91

Sure no problem, already implemented that, I'm wondering though about the description of the optional field. If a field has a description doesn't it need :

= strawberry.field( name="originalFieldName", description="Some clear description.")

Further more, I think i should add a name argument if the snake_case version of the field name differs from the original name. One more question, in the swapi I never saw an optional field with a custom type object that has None assigned to it, is that a preference or a necessity? What about the strawberry.ID type is it legal to assign a description to it?

srbiotik avatar Feb 26 '20 23:02 srbiotik

One more question, in the swapi I never saw an optional field with a custom type object that has None assigned to it, is that a preference or a necessity?

I think it is not have = None :)

What about the strawberry.ID type is it legal to assign a description to it?

ID is usually self explicatory, but we should be able to do this:

x: strawberry.ID = strawberry.field(description='this is a legacy id')
``

patrick91 avatar Feb 28 '20 16:02 patrick91

Hey @patrick91 sorry for not being very active on this for a while, I've been buried at work an life :). So, I am up to a point where I could use somebody with more xp to look at what I've done so far, so what is the procedure how do present my work so far?

srbiotik avatar Apr 11 '20 12:04 srbiotik

@srbiotik can you do a PR? we can also chat on discord if you want :) https://discord.gg/ZkRTEJQ

patrick91 avatar Apr 14 '20 23:04 patrick91

Hello, I'd be interested in helping with this if you need some peer review

Ambro17 avatar May 25 '20 16:05 Ambro17

Hello, I'd be interested in helping with this if you need some peer review

Yeah I need some help with this :). Thanks!

srbiotik avatar May 26 '20 07:05 srbiotik

Do you have your code hosted in github so we can review it together?

Ambro17 avatar May 26 '20 11:05 Ambro17

@srbiotik any updates on this? I might need to implement it soon for something I'm working on, don't want to waste your time 😊

patrick91 avatar Jun 09 '20 14:06 patrick91

Hey, sorry for being passive about this I've been entangled by some work stuff. I'll push my version of code so far at this repository, you can run a test from the ./tests/cli/test_import_schema.py there are some test gql files in the ./tests/cli/helpers/type which you can change through the SPECIFIC_TYPE and SPECIFIC_SCHEMA constants in the test file. The resulting output should be located in the tests/cli/helpers/output. All the work done in this was some months ago so I need to browse it a bit to remember... I'll be joining the discord chanell tomorrow so we can chat about it there!

P.S. Be gentle, it's my first contribution :)

srbiotik avatar Jun 09 '20 19:06 srbiotik