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 GraphQLID #191

Merged
merged 1 commit into from
Aug 7, 2019
Merged

Support GraphQLID #191

merged 1 commit into from
Aug 7, 2019

Conversation

omarchehab98
Copy link
Contributor

Closes: #187

Summary:

Adds support to specifying a field being of type GraphQLID. Add format: id or format: uuid.

Test Plan:

  1. Change directory to packages/openapi-to-graphql
  2. Run npm run start_dev
  3. Browse to localhost:3000/graphql
  4. Click on "Docs"
  5. Click on "Query"
  6. Observe the type of properties whose names end with "id"

Before:

company(id: String!): Company
office(accept: Accept, id: Int!): Office
productWithId(productId: String!, productTag: String!): ProductWithId
productsReviews(id: String!, limit: Int, productTag: String): [ProductsReviewsListItem]

After:

company(id: ID!): Company
office(accept: Accept, id: ID!): Office
productWithId(productId: ID!, productTag: String!): ProductWithId
productsReviews(id: ID!, limit: Int, productTag: String): [ProductsReviewsListItem]
  1. Click on "Company"
  2. Observe the type of "id"

Before:

id: String
The identifier of a company

After:

id: ID
The identifier of a company
  1. Click "Back" twice
  2. Click on "Mutation"
  3. Click on "ProductWithIdInput"
  4. Observe the type of "productId"

Before:

productId: String
The id of the product

After:

productId: ID
The id of the product

@ErikWittern
Copy link
Collaborator

@omarchehab98 Thank you so much for this PR, very interesting proposal! We are not perfectly familiar with the way that id / uuid formatting works in OpenAPI / JSON schema. Maybe you can help us?

For one, you make a check for format being uuid, without also checking for the type. E.g., your implementation allows to combine a number type with a format of uuid. See for example here in the commit. Is it even possible that a number-typed value has a uuid format?

Furthermore, we couldn't find reference to the id format present in your implementation. What constraints does this format imply, and for what types can it be defined? Maybe you have a pointer to more information?

@omarchehab98
Copy link
Contributor Author

omarchehab98 commented Jul 2, 2019

@ErikWittern format id and uuid are undefined by the openapi spec. format uuid was given as an example of undefined formats that may be used.

Since we know uuid follows a strict format, I also wanted to a more generic format. Thus I chose another undefined format called id. It follows no format, we can let the user define it.

I agree with your concern about format uuid, I can amend that.

Finally, regarding format id, I don't plan on adding any constraints. It seems like the tests didn't like it being a number but I recommend we explore that further. Perhaps it's good that we enforce it being a string because of number precision variation in different languages and computers and given that it is generally not helpful to do arithmetic on an id with the exception of hashing.

@omarchehab98
Copy link
Contributor Author

Please feel free to take the lead from here, I don't mind if you jump in, make changes, and merge it

@Alan-Cha
Copy link
Collaborator

Alan-Cha commented Jul 2, 2019

@omarchehab98 Thanks for the quick response! And thanks again for filing the issue and the very detailed PR, all highly appreciated.

I think we need to enforce that the ID field should be a string. Your logic regarding number precision variation makes a lot of sense. Also, the ID scalar in GraphQL expects the JSON value to be a string.

However, I do not think we should add our own custom id format. Instead, I believe we should create a new option, that allows the user to describe format types that should be interpreted as ID types. Otherwise we would embrace a convention that may not be shared by all our users.


I can fix the tests and help amend the commit as soon as I am free.

@omarchehab98
Copy link
Contributor Author

Is this PR on your radar to merge?

@Alan-Cha
Copy link
Collaborator

Yes! I think we should! I made some changes and added test cases since the last time we spoke. We are still missing the option to declare other ID formats (and exposing it in the CLI) though.

Closes: #187

Summary:

Adds support to specifying a field being of type GraphQLID. Add `format: id` or `format: uuid`.

Test Plan:

1. Change directory to `packages/openapi-to-graphql`
2. Run `npm run start_dev`
3. Browse to localhost:3000/graphql
4. Click on "Docs"
5. Click on "Query"
6. Observe the type of properties whose names end with "id"

Before:

company(id: String!): Company
office(accept: Accept, id: Int!): Office
productWithId(productId: String!, productTag: String!): ProductWithId
productsReviews(id: String!, limit: Int, productTag: String): [ProductsReviewsListItem]

After:

company(id: ID!): Company
office(accept: Accept, id: ID!): Office
productWithId(productId: ID!, productTag: String!): ProductWithId
productsReviews(id: ID!, limit: Int, productTag: String): [ProductsReviewsListItem]

7. Click on "Company"
8. Observe the type of "id"

Before:

id: String
The identifier of a company

After:

id: ID
The identifier of a company

9. Click "Back" twice
10. Click on "Mutation"
11. Click on "ProductWithIdInput"
12. Observe the type of "productId"

Before:

productId: String
The id of the product

After:

productId: ID
The id of the product

Signed-off-by: Omar Chehab <[email protected]>
@ErikWittern ErikWittern merged commit e744a45 into IBM:master Aug 7, 2019
@ErikWittern
Copy link
Collaborator

@omarchehab98 @Alan-Cha Great work, thank you!

@omarchehab98 omarchehab98 deleted the graphql-id branch August 7, 2019 17:47
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.

Supporting GraphQLID
3 participants