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

Support multiple schemas #51

Merged
merged 11 commits into from
Dec 4, 2019
Merged

Support multiple schemas #51

merged 11 commits into from
Dec 4, 2019

Conversation

arnarthor
Copy link
Contributor

@arnarthor arnarthor commented Dec 3, 2019

This adds an additional optional parameter to the ppx usage where you can locally override the desired schema to validate the query against.

This is fully backwards compatible with the old API and only extends it to allow for usage such as this

Reason

module MyQuery = [%graphql
  {|
    query pokemon($id: String, $name: String) {
      pokemon(name: $name, id: $id) {
        id
        name
      }
    }
  |};
  {schema: "pokedex_schema.json"}
];

OCaml

module MyQuery = [%graphql
  {|
    query pokemon($id: String, $name: String) {
      pokemon(name: $name, id: $id) {
        id
        name
      }
    }
  |};;
  {schema = "pokedex_schema.json"}
];

By adding this extra parameter, the PPX now supports having multiple different GraphQL APIs in a single project without the need for yarn workspaces or similar solutions.

@arnarthor
Copy link
Contributor Author

I wasn't able to run the bucklescript tests without the same changes as #50 had.

@baransu
Copy link
Collaborator

baransu commented Dec 3, 2019

Thank you! It's a really good addition. What do you think about adjusting second param to support more fine-tuning like generating serialize function? #20 (comment)

@arnarthor
Copy link
Contributor Author

arnarthor commented Dec 3, 2019

That sounds really good @baransu. So for this PR, instead of having this secondary argument as a string, having it as an object instead? Some thing like

module MyQuery = [%graphql
  {|
    query pokemon($id: String, $name: String) {
      pokemon(name: $name, id: $id) {
        id
        name
      }
    }
  |};
  {schema: "pokedex_schema.json"}
];

@baransu
Copy link
Collaborator

baransu commented Dec 3, 2019

Exactly like this. I'm only afraid of how it would work with optional arguments when you want to specify only part of additional params.

@arnarthor
Copy link
Contributor Author

Having it as an object instead of record should solve that right?

That way it doesn't really need to be a certain type/shape. The PPX can just look for different values of the object

@arnarthor
Copy link
Contributor Author

It means you can put in stuff that isn't doing anything, but at least we won't need to specify keys that we want default values to apply to?

@arnarthor
Copy link
Contributor Author

I'll experiment and ping you when I have an update. I think the suggestion above should just work as is 👍

@arnarthor
Copy link
Contributor Author

@baransu I've changed it to be a record. This works for any object since the PPX runs before the type checker.

To support other fields all you need to add is a separate function for the specific field you want similar to the extract_schema_from_config function

Copy link
Collaborator

@baransu baransu left a comment

Choose a reason for hiding this comment

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

Great PR. Thank you @arnarthor. I only have few comments about code quality 🙂


test("Allows you to omit nullable arguments", () =>
expect(MyQuery.make(~name="Pikachu", ())##variables)
== Js.Json.parseExn({| {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please change to |> based tests. I think it's more readable that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@@ -72,7 +72,7 @@ let drop_prefix = (prefix, str) => {
String.sub(str, len, rest);
};

let rewrite_query = (loc, delim, query) => {
let rewrite_query = (loc, delim, query, maybe_schema) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use labeled args here? It's getting pretty unreadable with more than 3 args.

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe_schema can then be ~schema=?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the parameter is explicitly an option in the downstream functions having it as ~schema=None instead made things easier to read.

If you explicitly want it as ~schema=? I can change it. But thought it didn't really matter myself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wait, I might have misunderstood the initial comment. Did you want all parameters to be named?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be all, can be just schema 🙂

@arnarthor
Copy link
Contributor Author

I think this is ready now @baransu 🎉

@baransu
Copy link
Collaborator

baransu commented Dec 3, 2019

@arnarthor Could you add it also to README?

@arnarthor arnarthor mentioned this pull request Dec 3, 2019
@baransu
Copy link
Collaborator

baransu commented Dec 4, 2019

Thank you! LGTM 🎉

@baransu baransu merged commit 467d557 into teamwalnut:master Dec 4, 2019
@baransu
Copy link
Collaborator

baransu commented Dec 7, 2019

Releases as 0.4.6

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.

2 participants