r/programming • u/oczekkk • Jun 27 '19
Postman announced support for GraphQL!
https://blog.graphqleditor.com/graphql-postman/7
Jun 27 '19
Idk if it's just me but the reason I don't use graphql for projects is cause I can never predict the runtime of the query I use.
1
u/tragicshark Jun 27 '19
It really comes down to how you define stuff...
suppose you have a schema:
type Query { hello: String foo: [Foo!]! } type Foo { ... }All this means is you have an endpoint that serves this schema which has functions on it
helloandfoowhich return aStringand an array ofFoo(and the array and items in it cannot be null).With a normal REST api, you would need 2 separate endpoints, all GraphQL does is define a particular type of endpoint which abstracts all the individual ones that you might need for a REST api and allows you to call them all with one GraphQL request.
Just how useful that winds up being is up to you.
6
u/justaphpguy Jun 28 '19
Text book examples are always easy, let's consider this:
items { avgGrade reviews { grade }
are you going to resolve
avgGradewith a custom aggregation SQL query and separate fetch all grades?or are you talking advantage that you loaded all grades anyway and can aggregate in-memory?
I'm not sure if it's "obvious" how to solve, especially if you just started out.
The latter has the hidden cost of implementation dependency. If you query does not request
reviews { grad }you don't the actual collection to aggregate in-memory, what now?1
u/tragicshark Jun 28 '19
I mean there wouldn't be any significant difference between 2 api endpoints, one that supplies the
avgGradevalue and another that suppliesreviewsvs 1 endpoint that lets you do both or either independently...GraphQL is a way to build an api that doesn't allow responses to interfere with each other, so you can service multiple traditional endpoints in the same network transit. It doesn't dictate what those members are (beyond the introspection layer) or how they are implemented (though some of the implementations may be more capable than others).
2
u/justaphpguy Jun 28 '19
But I think people would want such cases to be optimized, why wouldn't they.
In REST, with two calls, it's impossible (obviously).
In GraphQL you can do it; question is: should you.
6
u/mariosk89 Jun 27 '19
Super! I've only recently completed a project using GraphiQL for my queries. Hope this makes GraphQL more popular
6
Jun 27 '19
It's about time, https://insomnia.rest/ has had this for ages.
1
u/vosper1 Jun 27 '19
I'll throw in another recommendation for Insomnia - I switched from Postman and never looked back. It might be weird, but I love that it makes me name each request. And the vertical arrangement - so much better.
1
u/gbersac Jun 28 '19
does insomnia have built in test feature ? And cross team query sharing ?
2
Jun 28 '19
Team sharing yes. I dont think it has a test feature...never used that personally in postman so I guess it never really was a requirement for me to consider when switching.
9
u/Sairony Jun 27 '19
Never heard of GraphQL.
{
hero {
name
# Queries can have comments!
friends {
name
}
}
}Oh, already better than working with JSON in other words.
1
u/editor_of_the_beast Jun 27 '19
The value proposition of GraphQL is way more than it supporting comments in the query.
0
Jun 27 '19
[deleted]
19
Jun 27 '19 edited Jun 27 '19
It’s a beautiful feeling of getting back control that we used to have by running plain SQL queries over ADO.NET before the advent of REST.
Why is this a beautiful feeling? It sounds like a nightmare for public facing services.
In fact, with public facing databases, the first thing you should do is lock down access to the underlying objects to privileged accounts, and provide carefully curated views to public users based on specific use cases, to give you flexibility to change the underlying schemas as necessary as well as protecting the database against malicious or incompetent queries.
That's basically what you're doing with REST, providing carefully curated representations of resources to clients based on well articulated use cases.
The last thing you should do with public services is "give control back to the user". Unless you are providing a search API specifically, like Elasticsearch. More control == larger attack surface.
2
u/tragicshark Jun 27 '19
You can think of GraphQL as being basically an abstraction layer over a REST api with several endpoints.
GraphQL defines 2 entrypoint types: Query and Mutation. These roughly cover everything that a set of REST endpoints can do.
The big deal with GraphQL is that by defining the available members of Query (or Mutation, I'll stop meantioning that now), you enable a user to prune the response that must be generated for a given request.
For example if you have a schema like this:
enum Episode { NEWHOPE, EMPIRE, JEDI } interface Character { id: String! name: String friends: [Character] appearsIn: [Episode] } type Human implements Character { id: String! name: String friends: [Character] appearsIn: [Episode] homePlanet: String } type Droid implements Character { id: String! name: String friends: [Character] appearsIn: [Episode] primaryFunction: String } type Query { hero(episode: Episode): Character human(id: String!): Human droid(id: String!): Droid }Then by definition you have an endpoint which services this query type. If you then query this endpoint:
https://starwarsapi.query?query={droid(id:'R2-D2'){name primaryFunction}}you are effectively calling the endpoint and asking for only the "droid" member of that endpoint, passing the value 'R2-D2" in the id parameter and in that result set asking for the name and primaryFunction members.
A normal REST api would have instead 3 endpoints for what appears to be 3 different requests, but GraphQL takes the position that it is better to only have 1 endpoint which can handle up to all 3 endpoints worth of requests in the same network transit.
7
Jun 27 '19 edited Jun 27 '19
A normal REST api would have instead 3 endpoints for what appears to be 3 different requests, but GraphQL takes the position that it is better to only have 1 endpoint which can handle up to all 3 endpoints worth of requests in the same network transit.
That doesn't contradict REST either, because a resource can have many representations based on what you request.
Suppose you have the following endpoints:
/starwars /starwars/characters/{id}You can have variations like which allow you to control the data returned by the API:
/starwars/characters?species=droid /starwars/characters/123?includeFriends /starwars?thingsInEpisodeIVHow you make the request determines the data that is returned. You can define your request format in ad-hoc fashion, or you can use standard request formats like OData or JSON-API.
The difference is that GraphQL lets you define ad-hoc representations using a query DSL, while REST requires you to think about representations you will need ahead of time. REST is less flexible, but it doesn't devolve into a large number of individual requests as many GraphQL proponents like to claim. Less flexibility isn't necessarily a bad thing.
1
u/yawaramin Jun 29 '19
the first thing you should do is lock down access to the underlying objects to privileged accounts, and provide carefully curated views to public users based on specific use cases, to give you flexibility to change the underlying schemas as necessary
This is exactly what you do with GraphQL! Except not only do your payloads have a defined set of fields, but they can all have fewer than those. Have you ever implemented a
GET /cartand then aGET /cart?light=true? Well GraphQL makes this task totally unnecessary. The schema'sCarttype allows the consumer to pick out exactly the fields they want. No more back and forth between backend and frontend to agree on exactly what a 'light cart call payload' should look like.1
Jun 28 '19
You fail to see the forest for the tree, it’s understandable to a degree. GraphQL still has to be setup on the backend to return specific elements of whatever your serving, its simply not a free range proxy over data. The flexibility is that the properties can come from mixed sources independently.
The beauty of GraphQL is that it allows you too maintain a single endpoint instead of having multiple endpoints for the same data sets.
Say one group wants use data for a drop down list and only needs an id and a name, while another group needs and Id, name and an assortment of other fields. With rest you could have up too 8 endpoints to maintain for the same data set. With GraphQL its a single endpoint.
And since each property is setup with its own method, security can be linked applied to each individual property. Again within a single endpoint.
3
Jun 27 '19
[removed] — view removed comment
7
Jun 27 '19
I wager that the vast majority of implementations will expose GraphQL interfaces to applications within the same enterprise.
Between organizations or teams, I would say it's the same thing. Except for maybe the malicious part.
I would rather work out a contract with another team about specific representations, rather than increase flexibility in querying our data, unless we were specifically providing a search service.
Nor is it tantamount to exposing the structure of a data store.
It's not about exposing the structure of a data store. It's about giving end users more flexibility with the kinds of data available to them and the number of operations available to slice and dice that data. This kind of flexibility is not always desirable.
GraphQL simply supports filtering the data and returning well-defined graphs of it in one request-response pair, instead of chopped up into many.
REST does the same thing. That's what a representation is. However, the difference is that GraphQL lets end-users define their own representations, while REST bakes representations into the service. This requires end users and service providers to discuss requirements, so both sides know what to expect and can plan accordingly.
2
Jun 27 '19
[removed] — view removed comment
7
Jun 27 '19
The set-it-and-forget-it aspect is the direct benefit of GraphQL.
Is it really though? Because opening up your data to arbitrary queries instead of designing interfaces for your data sounds like trading one problem for another. There is no free lunch.
but in a parallel universe, another you is off working on other things instead of tending a growing forest of largely-unnecessary endpoints to access the same pool of underlying data.
If the representations are truly unnecessary, then they represent unnecessary use cases, and why would you keep them around? Otherwise, tending to the "unnecessary" endpoints is tending to the needs of your users. Just because you want to do other things, doesn't mean you can ignore your users. Hopefully your representations are robust enough to where you can pretty much forget about them. How many mainframes are out there, basically forgotten, still doing critical things?
5
u/sergioelixir Jun 27 '19
It's tricky because it's very easy to have N+1 problems. Very very easy.
2
u/justaphpguy Jun 28 '19
It's not such much different if someone uses ORMs and doesn't understand the concept of eager loading on how to prevent
n+1issues 🤷♀️
1
u/justaphpguy Jun 28 '19
I used Postman roughly for a few weeks when I got a API project started a few years ago.
Why? Because this was still in an early stage and unit tests were in its infancy.
Half a year later and the tides changed: I deployed code into production which I've never run through Postman nor explicitly tested in the application: I wrote tests/expectations for the REST/GraphQL endpoints and that's it.
Never looked back NOT to have to use such a tool.
The only thing sometimes is useful is to a playground like GraphiQL. The benefit for the team is: everyone has it already available with 0 other dependencies (read: yet another app needed to install / setup). It's just a projects URL and done.
14
u/badpotato Jun 27 '19
Well, time to switch REST support to GraphQL.