Skip to content

Instantly share code, notes, and snippets.

@donabrams
Created January 18, 2017 15:33
Show Gist options
  • Save donabrams/6cb5b4c124abaa40a45939ff1724bf0f to your computer and use it in GitHub Desktop.
Save donabrams/6cb5b4c124abaa40a45939ff1724bf0f to your computer and use it in GitHub Desktop.
GraphQL API Design Interview Question

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?

stream {
... on Text {
html
}
... on Photo {
url
}
... on CodeSnippet {
embedSrc
}
... on CodeExample {
embedSrc
}
}
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