GraphQL:
Enough rope to hang yourself with.
October 24th, 2018 - Greg Goforth
That one guy you work with that dabbled with GraphQL and is like...
Everyone that has to sit around that guy that dabbled with GraphQL...
30,000 Foot View
GraphQL is...
A query language for APIs and a runtime for fulfilling those queries with your existing (and disperate) data.
An understandable description of the data in your API.
Gives clients the power to ask for exactly what they need and nothing more.
Makes it easier to evolve APIs over time.
Enables powerful developer tools.
30,000 Foot View
GraphQL enables...
Multiple data sources in one request.
Fetching many resources in one query.
A types system to ensure Apps only ask for what’s possible and provide clear and helpful errors.
Quick and easy way to test queries in the browser via GraphiQL.
Simpler Versioning.
GraphQL Queries can be compared to GET requests. They are asking for some data, in a specific "shape".
GraphQL Mutations can be compared to POST, PUT, PATCH and DELETE requests. Mutations are any request to the api that causes side effects.
Queries and Mutations look almost identical.
query breweries {
breweries {
id
name
phone
}
}
mutation updateBrewery($id: ID!, $data: BreweryInput!) {
updateBrewery(id: $id, data: $data) {
status
}
}
Notice the mutation accepts some arguments, typically the payload of data to update a brewery in this case.
query breweries ($id: ID!, $prefix: String) {
breweries(id: $id) {
id
name
phone(prefix: $prefix)
}
}
mutation updateBrewery($id: ID!, $data: BreweryInput!) {
updateBrewery(id: $id, data: $data) {
status
}
}
Queries can also take arguments, either directly or at the field level.
const resolvers = {
Brewery: {
async phone(brewery, {prefix}, req) {
return `${prefix || 'TEL'}: ${brewery.phone}`;
},
},
Query: {
async ping(parentValue, {}, req, info) {
return 'pong';
},
async breweries(parentValue, {}, req, info) {
return JSON.parse(await rp('https://api.openbrewerydb.org/breweries?by_state=CA'));
},
}
};
Resolvers are responsible for actually fetching your data.
$ express graphql-app
$ cd graphql-app && npm install
$ npm install --save graphql graphql-tools express-graphql request request-promise
Basic Setup
or
$ git clone git@github.com/ggoforth/graphql-primer