-
Notifications
You must be signed in to change notification settings - Fork 211
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
Generate unions from oneOf return type #330
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Florian Tatzky <[email protected]>
@@ -406,6 +406,13 @@ export function getSchemaTargetGraphQLType<TSource, TContext, TArgs>( | |||
schema: SchemaObject, | |||
data: PreprocessingData<TSource, TContext, TArgs> | |||
): string | null { | |||
if (schema.oneOf) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I don't think we can do this as oneOf
and allOf
does not necessitate union or object.
For example, this is an example provided by json-schema
:
{
"type": "number",
"oneOf": [
{ "multipleOf": 5 },
{ "multipleOf": 3 }
]
}
Additionally, even if oneOf
contains a number of schemas, we still need to make a number of checks to see if we can still create a union as JSON schema oneOf
is a lot more flexible than GraphQL union type
.
For example, according to the specification, GraphQL unions cannot be composed of other unions
The member types of a Union type must all be Object base types; Scalar, Interface and Union types must not be member types of a Union. Similarly, wrapping types must not be member types of a Union.
That is why we have this check, to see if we have cases of nested oneOf
/allOf
.
This is for sure a bug that needs to be fixed. However, I think we need to approach it differently. I think the problem actually has to do with the fact that we do not properly identify the type of We're failing this if statement which ensures that all the member types ( The type data is coming from here and the definition for that is here. We identify the types of the schema here and unfortunately, I think what we need to do is to make In essence, I think we should move some of the logic from processing the Let me know if this all makes sense to you! |
@Alan-Cha yeah that makes total sense. As I said, I somehow knew that my changes were probably just to naive and a bandaid and has some major flaws I just couldn't see by myself. I talked to my team an my PO, and it's a story in our current sprint now and I can dedicate actual work time to dive deeper into this problem (which helps a lot). Thanks so far, and I will get back to you as soon as I have something. Btw is there a chat or some plattform you use for this community? Something where interaction is more immediate? If not that's ok, just asking. Best regards |
@ftatzky If you would like to take another look at this problem, that would be super cool! Otherwise, I will have to take a look at this in the upcoming weeks as I'm juggling some other tasks right now. I'm happy to answer whatever questions you have though! I think it should be easy to medium difficulty, leaning more towards easy. I think you are on the right track. We should have Yes! We have a Gitter chat room! Again, would be happy to answer any questions that you may have. |
There is one consideration we should make. Currently, the plan is to move the existing logic into
Actually on second thought, we should probably just utilize the switch statement
An example of how we can use {
"type": "number",
"oneOf": [
{ "multipleOf": 5 },
{ "multipleOf": 3 }
]
} We could create a new type that would actively check if it fits the
Edit: Actually, I think everything will be fine if we just utilize the switch statement. Don't worry about what I said. |
Let me know if this makes any sense! I might just be rambling 😅 |
According to Swagger: Inheritance and Polymorphism polymorphism can be described with the oneOf keyword in responses. Right now openapi-to-graphql doesn't support that.
This change enables the generation of unions for oneOf return types in responses.
SimpleTest.yaml
Generated output before the change
Generated output after the change