dillonkearns / elm-graphql

Autogenerate type-safe GraphQL queries in Elm.
https://package.elm-lang.org/packages/dillonkearns/elm-graphql/latest
BSD 3-Clause "New" or "Revised" License
778 stars 107 forks source link

Generate 'possibleTypes' from implemented 'interfaces' field #608

Closed th3coop closed 1 year ago

th3coop commented 2 years ago

This PR Fixes #607.

Instead of using the possbileTypes field on an ObjectType, this PR instead looks at the interfaces field for every InterfaceType and ObjectType definition to see what Interface it implements then add it to the Interfaces possibleTypes list.

Summary of changes:

th3coop commented 2 years ago

Ok, now that I understand the build checks I'm going to jump back into this. I plan to, at the very least, rename interfacePossibleTypesDict to something else so it doesn't seem like I'm still using that field.

I also acknowledge that it's likely a big nono that I've now exposed that function in order to make the tests work so I'm going to revisit that and see if there is a higher lvl I can put all the data together at or something.

dillonkearns commented 2 years ago

Sounds good! Could you also add an example that shows up in the end-to-end code?

This would be a good place to add it:

https://github.com/dillonkearns/elm-graphql/blob/master/ete_tests/edge-cases.gql

That way we'll have the new logic covered in the end-to-end output to make sure the output is looking good, and prevent it from accidentally changing in the future.

th3coop commented 2 years ago

Ok, found some issues with the generated code once I got the ete stuff added that I didn't notice when I was running things before (so good thing it's there!). I have an idea that I'm working through but it's going to take longer than I thought. Will be back.

Question: I see that for the ete test, it hits a server running on Heroku. Do I need to do anything to update that or does it update itself when I push the ete schema change?

th3coop commented 2 years ago

@dillonkearns K I think this is ready.

I'm not sure how you usually do these reviews but I'll point out the two major functional changes

th3coop commented 2 years ago

Morning @dillonkearns. Could I get approval for the workflows to run? I promise I didn't commit and Debug.logs

th3coop commented 2 years ago

And this time I ran npm run build before pushing the code 🤕 Sorry about that @dillonkearns

th3coop commented 1 year ago

Resolved: https://github.com/dillonkearns/elm-graphql/pull/610

@dillonkearns, to be able to run elm-review locally I had to updated it so there are some changes in this PR now that aren't directly related to the root issue it's addressing. I could easily do that in a separate branch if you like to keep things nice and tidy.

- This change is due to the new version of Elm-review and catching an unused module in the repo: https://github.com/dillonkearns/elm-graphql/pull/608/files#diff-d4b4331a1a542ce28ac90a7d59d1cf6f04eb34df3ae695d33c4f740c2c6c19c6L29 - suggested by jfmengels here , ran npx elm-json upgrade: https://github.com/dillonkearns/elm-graphql/pull/608/files#diff-9df456f94279a3c092f3f3011d527594b850731bc4e6f04191ab79e6ba3243b8

th3coop commented 1 year ago

OK, now I've run elm-review, npm run build and npm run test. It workflows should complete meow.,

th3coop commented 1 year ago

@dillonkearns ping for workflow run.

th3coop commented 1 year ago

Hey @dillonkearns, have you had time to take a look at this? Do you feel like this is a change you'll be interested in merging?

dillonkearns commented 1 year ago

Hey @th3coop, definitely interested in merging! I've been a bit busy with travel lately, sorry for the delayed response.

The only things I need before merging this are:

th3coop commented 1 year ago

Awwwww, but I was going to play Star Craft tonight.

Kidding! Absolutely I can address these points. I can get that diff of the before/after.

Follow up question:

dillonkearns commented 1 year ago

you are looking for an explicit statement as to why that change was necessary, why it's not possible to avoid it and what I've done my best to try to minimize it's effect as much as possible?

Yeah, exactly, basically that's something I need to understand before merging and releasing this, and anything you can do to help me understand that will speed things up.

th3coop commented 1 year ago

I can do this 👍

th3coop commented 1 year ago

API changes:

generator/src/Graphql/Parser/Type.elm

generator/src/Graphql/Generator/Group.elm

generator/src/Graphql/Generator/Context.elm

Hopefully that clarifies why I think these API changes are necessary/required. I acknowledge that the changes to Type.elm are not required but I really do think that that is the best place for those functions rather than one offs in let blocks where they are called. Having said that, I'll be happy to move them to a let block if you'd rather that.

th3coop commented 1 year ago

And here's a diff of the generated ete code that shows what the changes in this PR result in:

diff --git a/ete_tests/src/EdgeCases/Interface/Character.elm b/ete_tests/src/EdgeCases/Interface/Character.elm
index dafe3217..9b83f662 100644
--- a/ete_tests/src/EdgeCases/Interface/Character.elm
+++ b/ete_tests/src/EdgeCases/Interface/Character.elm
@@ -20,7 +20,8 @@ import Json.Decode as Decode

 type alias Fragments decodesTo =
-    { onHuman : SelectionSet decodesTo EdgeCases.Object.Human
+    { onSpecies : SelectionSet decodesTo EdgeCases.Interface.Species
+    , onHuman : SelectionSet decodesTo EdgeCases.Object.Human
     }

@@ -31,7 +32,8 @@ fragments :
     -> SelectionSet decodesTo EdgeCases.Interface.Character
 fragments selections____ =
     Object.exhaustiveFragmentSelection
-        [ Object.buildFragment "Human" selections____.onHuman
+        [ Object.buildFragment "Species" selections____.onSpecies
+        , Object.buildFragment "Human" selections____.onHuman
         ]

@@ -40,7 +42,8 @@ update syntax to add `SelectionSet`s for the types you want to handle.
 -}
 maybeFragments : Fragments (Maybe decodesTo)
 maybeFragments =
-    { onHuman = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
+    { onSpecies = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
+    , onHuman = Graphql.SelectionSet.empty |> Graphql.SelectionSet.map (\_ -> Nothing)
     }

I made a full PR for comparison and the specific changes is here: : https://github.com/th3coop/elm-graphql/pull/1/files#diff-0222bf4407ccb0114ee199ecfe42d21d7720f3a3422c5746096f26a4caf0d224L23

dillonkearns commented 1 year ago

Hey @th3coop thanks so much for giving me the diff and explanation of all these changes. I realized that these changes don't effect the published Elm package at all (neither the public interface or the code that runs within the Elm package) because all of the changes are within the generator/ folder, so that makes it even simpler! Thanks for outlining things, that made it a lot easier to navigate.

It's looking great, I went ahead and published this so it's now available with the latest NPM package, version 4.3.1. Thanks again for your work on this, I really appreciate it! 🙏

th3coop commented 1 year ago

Awesome! I've got a Jira ticket waiting for me to update our code to use this new version! It was my pleasure to help! Amazing setup in this package. So easy to add to it and test it for such a complicated paradigm.