I'm designing a stream API in GraphQL and having trouble choosing between the two implementations below. Which would you choose and why is it a better choice?
Created
January 18, 2017 15:33
-
-
Save donabrams/6cb5b4c124abaa40a45939ff1724bf0f to your computer and use it in GitHub Desktop.
GraphQL API Design Interview Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stream { | |
... on Text { | |
html | |
} | |
... on Photo { | |
url | |
} | |
... on CodeSnippet { | |
embedSrc | |
} | |
... on CodeExample { | |
embedSrc | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stream { | |
textSnippets { | |
createdAt | |
html | |
} | |
photos { | |
createdAt | |
url | |
} | |
codeSnippets { | |
createdAt | |
embedSrc | |
} | |
codeExamples { | |
createdAt | |
embedSrc | |
} | |
} |
Yeah, the first is definitely more "correct." Why? because the API is designed to return a stream, and a stream is a list of stream items in a certain order that happens to be one of those types. The stream determines what the order is. The second moves the responsibility for determining what a stream is to the receiver. I'd need to construct a "stream" by knowing what item types are in a stream and cobbling them together. I might as well have this and drop all pretext of the API determining what a stream is:
createdAt
html
}
photos {
createdAt
url
}
codeSnippets {
createdAt
embedSrc
}
codeExamples {
createdAt
embedSrc
}```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment