Seleccionar página

Introduction to GraphQL

What is GraphQL?

GraphQL is one of the alternatives and main rivals of RESTful APIs nowadays. GraphQL is a query language for APIs and server-side runtime that allows asking and getting exactly what you need. It is not tied to any specific database or storage engine, so you have the freedom to choose whichever you want.

“Describe your data, ask for what you want, get predictable results.”- GraphQL

Why GraphQL?

One of the reasons of the popularity of GraphQL is that it provides solutions for developers’ problems. Some of the main features of GraphQL are:

  • Ask and get only what you need
  • Get many resource in one request: GraphQL unifies data that would otherwise require multiple endpoint
  • Use of types and fields instead of endpoints
  • API can be organized into a simple and understandable graph schema

Who is using GraphQL?

These are just some of the companies that are using GraphQL:

Basic GraphQL concepts

Field

”GraphQL is about asking for specific fields on objects” (Official documentation).

In other words, clients will get data objects described by a set of fields. The following query show how to ask for a field (“name” in this case) and how the result is as expected.

graphql query

Mutations

As opposed to queries (read-only operations), mutations are operations that make changes on the server. They can also return an object type, which is a really useful feature when you want to know the new state of the object modified. This is the structure of a mutation:

mutation {
mutationName(input: {MutationNameInput!}) {
MutationNamePayload
}

As you can see, you have to specified 3 this: mutation name, the input object you want to pass to the server and the payload (the data you want to return from the server).

Schema

All the queries are validated and executed against the schema. The Schema describes the possible data that a client can access.

Object types

The GraphQL official documentation defines object types like “The most basic components of a GraphQL schema, which just represent a kind of object you can fetch from your service, and what fields it has.”

type Starship {
id: ID!
name: String!
length(unit: LengthUnit = METER): Float
}

Notice that the length field of the Starship type, has an argument called LengthUnit. Arguments are key-value pairs attached to a specific field and are always optional.

Interfaces

Even it sounds strange, GraphQL supports interfaces. They behave like in other programming laguages, where the type or class that implements the interface must contain the specified fields, with the defined arguments and return types.

interface Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}
type Human implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
starships: [Starship]
totalCredits: Int
}

type Droid implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
primaryFunction: String
}

Getting started with GraphQL

Hello World

As we said before, many different programming languages support GraphQL. In this GraphQL tutorial we will build a Hello World example using the JavaScript implementation. You will need to install Node.js v6 in order to continue with the example.
Once you have Node installed, you can create a new project and install GraphQL.js from the Node Package Manager:

npm initnpm install graphql --save

Now that you have graphql installed on your project, you need to create a server.js file with the following code:

You can run this little application by typing:

node server.js

Well done! You will see the log printed when the code executes the graphql query.

What’s next?

In this introduction to GraphQL we have spoken about what GraphQL is,its main features, advantages and basic concepts. With the basic Hello World example you have also seen how to implement a GraphQL service using Javascript and Node.js. Now you can continue practicing with more complex examples and stay tuned for more articles like this.

Sources