I view it like a standardized RPC interface. As a standard, there a bunch of amazing tools and libraries and features you get for free that you'd otherwise have to provide yourself. (e.g. our entire clientside cache is a GraphQL cache. I haven't written any special caching code from scratch ... the clientside library just has it baked in.) I've found GraphQL to be the easiest API to build and use once you get over the learning curve.
REST's main strength is a flat learning curve, but once an API gets sufficiently complex, it's a total pain to maintain.
GraphQL's abstraction seems to scale constant or logarithmic in terms of mental overhead as new "endpoints" are added. While something like REST or even a bespoke RPC interface scales at least linearly in mental overhead. ie I add new GraphQL endpoints with relatively little trepidation.
How can you see what endpoints are supported? You linked here yesterday, but how to tell the things that you can ask for? For REST stuff you'd get a list of them; is it meant to be something I can infer somehow, based on the schema you provided?
reply
These are all the item endpoints or queries in graphql terms.
extend type Query { items(sub: String, sort: String, type: String, cursor: String, name: String, when: String, from: String, to: String, by: String, limit: Int): Items item(id: ID!): Item pageTitleAndUnshorted(url: String!): TitleUnshorted dupes(url: String!): [Item!] related(cursor: String, title: String, id: ID, minMatch: String, limit: Int): Items search(q: String, sub: String, cursor: String, what: String, sort: String, when: String, from: String, to: String): Items auctionPosition(sub: String, id: ID, bid: Int!): Int! itemRepetition(parentId: ID): Int! }
reply
If I wanted to get 100 of your most recent posts for instance, I'd query for them like this:
query items(name: 'elvismercury', limit: 100) { id createdAt title text }
reply
curl --request POST \ --header 'content-type: application/json' \ --url 'https://stacker.news/api/graphql' \ --data "{\"query\":\"query { items(name: \\\"elvismercury\\\", limit: 100) { id createdAt title text }}\"}"
{"errors":[{"message":"Cannot query field \"id\" on type \"Items\".","locations":[{"line":1,"column":51}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Cannot query field \"createdAt\" on type \"Items\".","locations":[{"line":1,"column":54}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Cannot query field \"title\" on type \"Items\".","locations":[{"line":1,"column":64}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Cannot query field \"text\" on type \"Items\".","locations":[{"line":1,"column":70}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}
Thoughts? (for the sake of PoW, I tried to troubleshoot already, but I don't know what I'm doing, so I'll omit the flailing.)
reply
Oh I oversimplified that example (accidentally).
This should do it:
query items(name: 'elvismercury', sort: 'user', limit: 100) { items { id createdAt title text } }
reply
One more on all this stuff.
I wanted to play around with some queries without obsessing over how many \\ I need to escape quotes from bash, so I went here and put in the server you mentioned yesterday as the endpoint:
https://stacker.news/api/graphql
It can't connect. It seems like it should be able to connect, but maybe I'm doing something really dumb. If so, is there some other tool that would let me run queries in a less frictiony way that I could use?
reply
It's CORS most likely.
I'll embed the sandbox on my next deployment.
reply
That did indeed do it. Exciting!
reply
Edited because I forgot we have to include sort: user for back compat.
reply