GraphQL with Ruby On Rails: Queries in Multiple Files

GraphQL with Ruby On Rails: Queries in Multiple Files

<strong>In the last episode, we added RSpecs tests for GraphQL mutations. If you need some more insight about the topic you can check the article here. Today we are going to add specs (again, we will focus only on the happy paths) for GraphQL queries. But there is one thing about the current implementation of the queries that I don’t like. We have everything defined in one file: app/graphql/types/query_type.rb</strong>

In the last episode, we added RSpecs tests for GraphQL mutations. If you need some more insight about the topic you can check the article here. Today we are going to add specs (again, we will focus only on the happy paths) for GraphQL queries. But there is one thing about the current implementation of the queries that I don’t like. We have everything defined in one file: app/graphql/types/query_type.rb

module Types
  class QueryType < Types::BaseObject
    field :authors, [Types::AuthorType], null: false do
      description 'Find all authors'
    end
field :author, Types::AuthorType, null: false do
  description 'Find an author by ID'
  argument :id, ID, required: true
end

field :books, [Types::BookType], null: false do
  description 'Find all books'
end

field :book, Types::BookType, null: false do
  description 'Find a book by ID'
  argument :id, ID, required: true
end

def authors
  Author.all
end

def author(id:)
  Author.find(id)
end

def books
  Book.all
end

def book(id:)
  Book.find(id)
end

end
end

You can imagine that this file will grow over time. I want to show you how to divide the logic into many smaller pieces.

The first step of every refactoring

It is hard to do refactoring without proper specs. So we are going to add specs first. But to make the article a little shorter and easier to comprehend, we will embrace the approach we put a great emphasis on in our company. We will do a test-driven development. That means that we will write the final specs. So instead of adding specs to our previous implementation, we will add specs for the future implementation where every query is defined in a separate file.

We would like to have a structure where all queries are inside app/graphql/queries/ folder. Therefore our specs will end up in specs/graphql/queries/ directory.

specs/graphql/queries/author_spec.rb

require 'rails_helper'

module Queries
RSpec.describe Author, type: :request do
describe '.resolve' do
it 'returns author for provided id' do
author = create(:author, first_name: 'Lee', last_name: 'Child', date_of_birth: Date.parse('1954-10-29'))
book = create(:book, author: author)

    post '/graphql', params: { query: query(id: author.id) }

    json = JSON.parse(response.body)
    data = json['data']['author']

    expect(data).to include(
      'id'          =&gt; be_present,
      'firstName'   =&gt; 'Lee',
      'lastName'    =&gt; 'Child',
      'dateOfBirth' =&gt; '1954-10-29',
      'books'       =&gt; [{ 'id' =&gt; book.id.to_s }]
    )
  end
end

def query(id:)
  &lt;&lt;~GQL
    query {
      author(id: #{id}) {
        id
        firstName
        lastName
        dateOfBirth
        books {
          id
        }
      }
    }
  GQL
end

end
end

specs/graphql/queries/book_spec.rb

require 'rails_helper'

module Queries
RSpec.describe Authors, type: :request do
describe '.resolve' do
it 'returns all authors' do
author = create(:author, first_name: 'Lee', last_name: 'Child', date_of_birth: Date.parse('1954-10-29'))
create(:author, first_name: 'Stephen', last_name: 'King', date_of_birth: Date.parse('1947-09-21'))
book = create(:book, author: author)

    post '/graphql', params: { query: query }

    json = JSON.parse(response.body)
    data = json['data']['authors']

    expect(data).to match_array [
      hash_including(
        'id'          =&gt; be_present,
        'firstName'   =&gt; 'Lee',
        'lastName'    =&gt; 'Child',
        'dateOfBirth' =&gt; '1954-10-29',
        'books'       =&gt; [{ 'id' =&gt; book.id.to_s }]
      ),
      hash_including(
        'id'          =&gt; be_present,
        'firstName'   =&gt; 'Stephen',
        'lastName'    =&gt; 'King',
        'dateOfBirth' =&gt; '1947-09-21',
        'books'       =&gt; []
      )
    ]
  end
end

def query
  &lt;&lt;~GQL
    query {
      authors {
        id
        firstName
        lastName
        dateOfBirth
        books {
          id
        }
      }
    }
  GQL
end

end
end

specs/graphql/queries/book_spec.rb and

specs/graphql/queries/books_spec.rb are almost the same so I’am not going to paste the code in here. Instead, you can check the files on gist if you like: book_spec.rb and books_spec.rb

As you can see our new specs look really similar to those we wrote for mutations. We have a query method that we use for building GraphQL query and we check if the result of the query meets our expectations.

Dividing queries into separate files

Now when all needed specs are in place we can finally do the refactoring. I am not going to show a proper TDD in this article because it will require those actions over and over again: run spec, see the error, fix the error, run spec again to see another error, fix that error, etc. It will be hard to follow so I’m going to present the final solution and explain the idea behind it.

We want to have a similar structure to what we have in mutations but it is not supported and we need to look for alternatives. The one we usually use for splitting queries is Resolvers.

Disclaimer: it is not always recommended. Please check the documentation for more information.

As I mentioned many times, we want our query_type.rb to work similar to mutation_type.rb it means that a new version of app/graphql/types/query_type.rb should look like this:

module Types
class QueryType < Types::BaseObject
field :author, resolver: Queries::Author
field :authors, resolver: Queries::Authors
field :book, resolver: Queries::Book
field :books, resolver: Queries::Books
end
end

We removed all the logic we had. Instead, we define what query is available and the resolver for that query (a place when we can find it).

If we are going to follow the mutations’ way of doing things we need an additional file: app/graphql/queries/base_query.rb

module Queries
class BaseQuery < GraphQL::Schema::Resolver
end
end

It is empty for now, but in the future episodes, we will use it for some cool things.

We can finally see the implementation of our new queries:

app/graphql/queries/author.rb

module Queries
class Author < Queries::BaseQuery
description 'Find an author by ID'

argument :id, ID, required: true

type Types::AuthorType, null: false

def resolve(id:)
  ::Author.find(id)
end

end
end

app/graphql/queries/authors.rb

module Queries
class Authors < Queries::BaseQuery
description 'Find all authors'

type [Types::AuthorType], null: false

def resolve
  ::Author.all
end

end
end

app/graphql/queries/book.rb

module Queries
class Book < Queries::BaseQuery
description 'Find a book by ID'

argument :id, ID, required: true

type Types::BookType, null: false

def resolve(id:)
  ::Book.find(id)
end

end
end

app/graphql/queries/books.rb

module Queries
class Books < Queries::BaseQuery
description 'Find all books'

type [Types::BookType], null: false

def resolve
  ::Book.all
end

end
end

The code of each query is moved from the query_type.rb without any changes. The magic that makes it possible is the inheritance from Queries::BaseQuery which inherits from GraphQL::Schema::Resolver. That is it.

Summary

At this point we have all the endpoints tested out and we (almost) have a structure that is easy to build on. In the future episodes, we will discuss things like pagination, authentication, authorization and many more.

Originally published by Ireneusz Skrobiś at https://itnext.io

Learn More

☞ The Complete Ruby on Rails Developer Course

☞ Learn to Code with Ruby

☞ Comprehensive Ruby Programming

☞ Dissecting Ruby on Rails 5 - Become a Professional Developer

How to set up GraphQL with Golang: a deep dive from basics to advanced

How to set up GraphQL with Golang: a deep dive from basics to advanced

GraphQL has become a buzzword over the last few years after Facebook made it open-source. I have tried GraphQL with the Node.js, and I agree with all the buzz about the advantages and simplicity of GraphQL.

GraphQL has become a buzzword over the last few years after Facebook made it open-source. I have tried GraphQL with the Node.js, and I agree with all the buzz about the advantages and simplicity of GraphQL.

So what is GraphQL? This is what the official GraphQL definition says:

GraphQL is a query language for APIs and runtime for fulfilling those queries with your existing data. GraphQL provides a complete and 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, and enables powerful developer tools.
I recently switched to Golang for a new project I’m working on (from Node.js) and I decided to try GraphQL with it. There are not many library options with Golang but I have tried it with Thunder, graphql, graphql-go, and gqlgen. And I have to say that gqlgen is winning among all the libraries I have tried.

gqlgen is still in beta with latest version 0.7.2 at the time of writing this article, and it’s rapidly evolving. You can find their road-map here. And now 99designs is officially sponsoring them, so we will see even better development speed for this awesome open source project. vektah and neelance are major contributors, and neelance also wrote graphql-go.

So let’s dive into the library semantics assuming you have basic GraphQL knowledge.

Highlights

As their headline states,

GraphQL is a query language for APIs and runtime for fulfilling those queries with your existing data. GraphQL provides a complete and 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, and enables powerful developer tools.
I think this is the most promising thing about the library: you will never see map[string]interface{} here, as it uses a strictly typed approach.

Apart from that, it uses a Schema first Approach: so you define your API using the graphql Schema Definition Language. This has its own powerful code generation tools which will auto-generate all of your GraphQL code and you will just need to implement the core logic of that interface method.

I have divided this article into two phases:

  • The basics: Configuration, Mutations, Queries, and Subscription
  • The advanced: Authentication, Dataloaders, and Query Complexity

Phase 1: The Basics - Configuration, Mutations, Queries, and Subscriptions

We will use a video publishing site as an example in which a user can publish a video, add screenshots, add a review, and get videos and related videos.

mkdir -p $GOPATH/src/github.com/ridhamtarpara/go-graphql-demo/

Create the following schema in the project root:

type User {
    id: ID!
    name: String!
    email: String!
}

type Video {
    id: ID!
    name: String!
    description: String!
    user: User!
    url: String!
    createdAt: Timestamp!
    screenshots: [Screenshot]
    related(limit: Int = 25, offset: Int = 0): [Video!]!
}

type Screenshot {
    id: ID!
    videoId: ID!
    url: String!
}

input NewVideo {
    name: String!
    description: String!
    userId: ID!
    url: String!
}

type Mutation {
    createVideo(input: NewVideo!): Video!
}

type Query {
    Videos(limit: Int = 25, offset: Int = 0): [Video!]!
}

scalar Timestamp

Here we have defined our basic models and one mutation to publish new videos, and one query to get all videos. You can read more about the graphql schema here. We have also defined one custom type (scalar), as by default graphql has only 5 scalar types that include Int, Float, String, Boolean and ID.

So if you want to use custom type, then you can define a custom scalar in schema.graphql (like we have defined Timestamp) and provide its definition in code. In gqlgen, you need to provide marshal and unmarshal methods for all custom scalars and map them to gqlgen.yml.

Another major change in gqlgen in the last version is that they have removed the dependency on compiled binaries. So add the following file to your project under scripts/gqlgen.go.

// +build ignore

package main

import "github.com/99designs/gqlgen/cmd"

func main() {
	cmd.Execute()
}

and initialize dep with:

dep init

Now it’s time to take advantage of the library’s codegen feature which generates all the boring (but interesting for a few) skeleton code.

go run scripts/gqlgen.go init

which will create the following files:

gqlgen.yml — Config file to control code generation.

**generated.go **— The generated code which you might not want to see.

models_gen.go — All the models for input and type of your provided schema.

resolver.go — You need to write your implementations.

server/server.go — entry point with an http.Handler to start the GraphQL server.

Let’s have a look at one of the generated models of the Video type:

type Video struct {
	ID          string        `json:"id"`
	Name        string        `json:"name"`
	User        User          `json:"user"`
	URL         string        `json:"url"`
	CreatedAt   string        `json:"createdAt"`
	Screenshots []*Screenshot `json:"screenshots"`
	Related     []Video       `json:"related"`
}

Here, as you can see, ID is defined as a string and CreatedAt is also a string. Other related models are mapped accordingly, but in the real world you don’t want this — if you are using any SQL data type you want your ID field as int or int64, depending on your database.

For example I am using PostgreSQL for demo so of course I want ID as an int and CreatedAt as a time.Time. So we need to define our own model and instruct gqlgen to use our model instead of generating a new one.

type Video struct {
	ID          int       `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	User        User      `json:"user"`
	URL         string    `json:"url"`
	CreatedAt   time.Time `json:"createdAt"`
	Related     []Video
}

// Lets redefine the base ID type to use an id as int
func MarshalID(id int) graphql.Marshaler {
	return graphql.WriterFunc(func(w io.Writer) {
		io.WriteString(w, strconv.Quote(fmt.Sprintf("%d", id)))
	})
}

// And the same for the unmarshaler
func UnmarshalID(v interface{}) (int, error) {
	id, ok := v.(string)
	if !ok {
		return 0, fmt.Errorf("ids must be strings")
	}
	i, e := strconv.Atoi(id)
	return int(i), e
}

func MarshalTimestamp(t time.Time) graphql.Marshaler {
	timestamp := t.Unix() * 1000

	return graphql.WriterFunc(func(w io.Writer) {
		io.WriteString(w, strconv.FormatInt(timestamp, 10))
	})
}

func UnmarshalTimestamp(v interface{}) (time.Time, error) {
	if tmpStr, ok := v.(int); ok {
		return time.Unix(int64(tmpStr), 0), nil
	}
	return time.Time{}, errors.TimeStampError
}

and update gqlgen to use these models like this:

schema:
- schema.graphql
exec:
  filename: generated.go
model:
  filename: models_gen.go
resolver:
  filename: resolver.go
  type: Resolver
models:
  Video:
    model: github.com/ridhamtarpara/go-graphql-demo/api.Video
  ID:
    model: github.com/ridhamtarpara/go-graphql-demo/api.ID
  Timestamp:
model: github.com/ridhamtarpara/go-graphql-demo/api.Timestamp

So, the focal point is the custom definitions for ID and Timestamp with the marshal and unmarshal methods and their mapping in a gqlgen.yml file. Now when the user provides a string as ID, UnmarshalID will convert a string into an int. While sending the response, MarshalID will convert int to string. The same goes for Timestamp or any other custom scalar you define.

Now it’s time to implement real logic. Open resolver.go and provide the definition to mutation and queries. The stubs are already auto-generated with a not implemented panic statement so let’s override that.

func (r *mutationResolver) CreateVideo(ctx context.Context, input NewVideo) (api.Video, error) {
	newVideo := api.Video{
		URL:         input.URL,
		Name:        input.Name,
		CreatedAt:   time.Now().UTC(),
	}

	rows, err := dal.LogAndQuery(r.db, "INSERT INTO videos (name, url, user_id, created_at) VALUES($1, $2, $3, $4) RETURNING id",
		input.Name, input.URL, input.UserID, newVideo.CreatedAt)
	defer rows.Close()

	if err != nil || !rows.Next() {
		return api.Video{}, err
	}
	if err := rows.Scan(&newVideo.ID); err != nil {
		errors.DebugPrintf(err)
		if errors.IsForeignKeyError(err) {
			return api.Video{}, errors.UserNotExist
		}
		return api.Video{}, errors.InternalServerError
	}
  
	return newVideo, nil
}

func (r *queryResolver) Videos(ctx context.Context, limit *int, offset *int) ([]api.Video, error) {
	var video api.Video
	var videos []api.Video

	rows, err := dal.LogAndQuery(r.db, "SELECT id, name, url, created_at, user_id FROM videos ORDER BY created_at desc limit $1 offset $2", limit, offset)
	defer rows.Close();
	
    if err != nil {
		errors.DebugPrintf(err)
		return nil, errors.InternalServerError
	}
	for rows.Next() {
		if err := rows.Scan(&video.ID, &video.Name, &video.URL, &video.CreatedAt, &video.UserID); err != nil {
			errors.DebugPrintf(err)
			return nil, errors.InternalServerError
		}
		videos = append(videos, video)
	}

	return videos, nil
}

and hit the mutation:

Ohh it worked…… but wait, why is my user empty 😦? So here there is a similar concept like lazy and eager loading. As graphQL is extensible, you need to define which fields you want to populate eagerly and which ones lazily.

I have created this golden rule for my organization team working with gqlgen:

GraphQL is a query language for APIs and runtime for fulfilling those queries with your existing data. GraphQL provides a complete and 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, and enables powerful developer tools.
For our use-case, I want to load Related Videos (and even users) only if a client asks for those fields. But as we have included those fields in the models, gqlgen will assume that you will provide those values while resolving video — so currently we are getting an empty struct.

Sometimes you need a certain type of data every time, so you don’t want to load it with another query. Rather you can use something like SQL joins to improve performance. For one use-case (not included in the article), I needed video metadata every time with the video which is stored in a different place. So if I loaded it when requested, I would need another query. But as I knew my requirements (that I need it everywhere on the client side), I preferred it to load eagerly to improve the performance.

So let’s rewrite the model and regenerate the gqlgen code. For the sake of simplicity, we will only define methods for the user.

type Video struct {
	ID          int       `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	UserID      int       `json:"-"`
	URL         string    `json:"url"`
	CreatedAt   time.Time `json:"createdAt"`
}

So we have added UserID and removed User struct and regenerated the code:

go run scripts/gqlgen.go -v

This will generate the following interface methods to resolve the undefined structs and you need to define those in your resolver:

type VideoResolver interface {
	User(ctx context.Context, obj *api.Video) (api.User, error)
	Screenshots(ctx context.Context, obj *api.Video) ([]*api.Screenshot, error)
	Related(ctx context.Context, obj *api.Video, limit *int, offset *int) ([]api.Video, error)
}

And here is our definition:

func (r *videoResolver) User(ctx context.Context, obj *api.Video) (api.User, error) {
	rows, _ := dal.LogAndQuery(r.db,"SELECT id, name, email FROM users where id = $1", obj.UserID)
	defer rows.Close()
	
	if !rows.Next() {
		return api.User{}, nil
	}
	var user api.User
	if err := rows.Scan(&user.ID, &user.Name, &user.Email); err != nil {
		errors.DebugPrintf(err)
		return api.User{}, errors.InternalServerError
	}
	
	return user, nil
}

Now the result should look something like this:

So this covers the very basics of graphql and should get you started. Try a few things with graphql and the power of Golang! But before that, let’s have a look at subscription which should be included in the scope of this article.

Subscriptions

Graphql provides subscription as an operation type which allows you to subscribe to real tile data in GraphQL. gqlgen provides web socket-based real-time subscription events.

You need to define your subscription in the schema.graphql file. Here we are subscribing to the video publishing event.

type Subscription {
    videoPublished: Video!
}

Regenerate the code by running: go run scripts/gqlgen.go -v.

As explained earlier, it will make one interface in generated.go which you need to implement in your resolver. In our case, it looks like this:

var videoPublishedChannel map[string]chan api.Video

func init() {
	videoPublishedChannel = map[string]chan api.Video{}
}

type subscriptionResolver struct{ *Resolver }

func (r *subscriptionResolver) VideoPublished(ctx context.Context) (<-chan api.Video, error) {
	id := randx.String(8)

	videoEvent := make(chan api.Video, 1)
	go func() {
		<-ctx.Done()
	}()
	videoPublishedChannel[id] = videoEvent
	return videoEvent, nil
}

func (r *mutationResolver) CreateVideo(ctx context.Context, input NewVideo) (api.Video, error) {
	// your logic ...

	for _, observer := range videoPublishedChannel {
		observer <- newVideo
	}

	return newVideo, nil
}

Now, you need to emit events when a new video is created. As you can see on line 23 we have done that.

And it’s time to test the subscription:

GraphQL comes with certain advantages, but everything that glitters is not gold. You need to take care of a few things like authorizations, query complexity, caching, N+1 query problem, rate limiting, and a few more issues — otherwise it will put you in performance jeopardy.

Phase 2: The advanced - Authentication, Dataloaders, and Query Complexity

Every time I read a tutorial like this, I feel like I know everything I need to know and can get my all problems solved.

But when I start working on things on my own, I usually end up getting an internal server error or never-ending requests or dead ends and I have to dig deep into that to carve my way out. Hopefully we can help prevent that here.

Let’s take a look at a few advanced concepts starting with basic authentication.

Authentication

In a REST API, you have a sort of authentication system and some out of the box authorizations on particular endpoints. But in GraphQL, only one endpoint is exposed so you can achieve this with schema directives.

You need to edit your schema.graphql as follows:

type Mutation {
    createVideo(input: NewVideo!): Video! @isAuthenticated
}

directive @isAuthenticated on FIELD_DEFINITION

We have created an isAuthenticated directive and now we have applied that directive to createVideo subscription. After you regenerate code you need to give a definition of the directive. Currently, directives are implemented as struct methods instead of the interface so we have to give a definition.

I have updated the generated code of server.go and created a method to return graphql config for server.go as follows:

func NewRootResolvers(db *sql.DB) Config {
	c := Config{
		Resolvers: &Resolver{
			db: db,
		},
	}

	// Schema Directive
	c.Directives.IsAuthenticated = func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
		ctxUserID := ctx.Value(UserIDCtxKey)
		if ctxUserID != nil {
			return next(ctx)
		} else {
			return nil, errors.UnauthorisedError
		}
	}
	return c
}
rootHandler:= dataloaders.DataloaderMiddleware(
		db,
		handler.GraphQL(
			go_graphql_demo.NewExecutableSchema(go_graphql_demo.NewRootResolvers(db)
		)
	)
http.Handle("/query", auth.AuthMiddleware(rootHandler))

We have read the userId from the context. Looks strange right? How was userId inserted in the context and why in context? Ok, so gqlgen only provides you the request contexts at the implementation level, so you can not read any of the HTTP request data like headers or cookies in graphql resolvers or directives. Therefore, you need to add your middleware and fetch those data and put the data in your context.

So we need to define auth middleware to fetch auth data from the request and validate.

I haven’t defined any logic there, but instead I passed the userId as authorization for demo purposes. Then chain this middleware in server.go along with the new config loading method.

Now, the directive definition makes sense. Don’t handle unauthorized users in your middleware as it will be handled by your directive.

Demo time:

You can even pass arguments in the schema directives like this:

directive @hasRole(role: Role!) on FIELD_DEFINITION
enum Role { ADMIN USER }

Dataloaders

This all looks fancy, doesn’t it? You are loading data when needed. Clients have control of the data, there is no under-fetching and no over-fetching. But everything comes with a cost.

So what’s the cost here? Let’s take a look at the logs while fetching all the videos. We have 8 video entries and there are 5 users.

query{
  Videos(limit: 10){
    name
    user{
      name
    }
  }
}

Query: Videos : SELECT id, name, description, url, created_at, user_id FROM videos ORDER BY created_at desc limit $1 offset $2
Resolver: User : SELECT id, name, email FROM users where id = $1
Resolver: User : SELECT id, name, email FROM users where id = $1
Resolver: User : SELECT id, name, email FROM users where id = $1
Resolver: User : SELECT id, name, email FROM users where id = $1
Resolver: User : SELECT id, name, email FROM users where id = $1
Resolver: User : SELECT id, name, email FROM users where id = $1
Resolver: User : SELECT id, name, email FROM users where id = $1
Resolver: User : SELECT id, name, email FROM users where id = $1

Why 9 queries (1 videos table and 8 users table)? It looks horrible. I was just about to have a heart attack when I thought about replacing our current REST API servers with this…but dataloaders came as a complete cure for it!

This is known as the N+1 problem, There will be one query to get all the data and for each data (N) there will be another database query.

This is a very serious issue in terms of performance and resources: although these queries are parallel, they will use your resources up.

We will use the dataloaden library from the author of gqlgen. It is a Go- generated library. We will generate the dataloader for the user first.

go get github.com/vektah/dataloaden
dataloaden github.com/ridhamtarpara/go-graphql-demo/api.User

This will generate a file userloader_gen.go which has methods like Fetch, LoadAll, and Prime.

Now, we need to define the Fetch method to get the result in bulk.

func DataloaderMiddleware(db *sql.DB, next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		userloader := UserLoader{
			wait : 1 * time.Millisecond,
			maxBatch: 100,
			fetch: func(ids []int) ([]*api.User, []error) {
				var sqlQuery string
				if len(ids) == 1 {
					sqlQuery = "SELECT id, name, email from users WHERE id = ?"
				} else {
					sqlQuery = "SELECT id, name, email from users WHERE id IN (?)"
				}
				sqlQuery, arguments, err := sqlx.In(sqlQuery, ids)
				if err != nil {
					log.Println(err)
				}
				sqlQuery = sqlx.Rebind(sqlx.DOLLAR, sqlQuery)
				rows, err := dal.LogAndQuery(db, sqlQuery, arguments...)
				defer rows.Close();
				if err != nil {
					log.Println(err)
				}
				userById := map[int]*api.User{}

				for rows.Next() {
					user:= api.User{}
					if err := rows.Scan(&user.ID, &user.Name, &user.Email); err != nil {
						errors.DebugPrintf(err)
						return nil, []error{errors.InternalServerError}
					}
					userById[user.ID] = &user
				}

				users := make([]*api.User, len(ids))
				for i, id := range ids {
					users[i] = userById[id]
					i++
				}

				return users, nil
			},
		}
		ctx := context.WithValue(r.Context(), CtxKey, &userloader)
		r = r.WithContext(ctx)
		next.ServeHTTP(w, r)
	})
}

Here, we are waiting for 1ms for a user to load queries and we have kept a maximum batch of 100 queries. So now, instead of firing a query for each user, dataloader will wait for either 1 millisecond for 100 users before hitting the database. We need to change our user resolver logic to use dataloader instead of the previous query logic.

func (r *videoResolver) User(ctx context.Context, obj *api.Video) (api.User, error) {
	user, err := ctx.Value(dataloaders.CtxKey).(*dataloaders.UserLoader).Load(obj.UserID)
	return *user, err
}

After this, my logs look like this for similar data:

Query: Videos : SELECT id, name, description, url, created_at, user_id FROM videos ORDER BY created_at desc limit $1 offset $2
Dataloader: User : SELECT id, name, email from users WHERE id IN ($1, $2, $3, $4, $5)

Now only two queries are fired, so everyone is happy. The interesting thing is that only five user keys are given to query even though 8 videos are there. So dataloader removed duplicate entries.

Query Complexity

In GraphQL you are giving a powerful way for the client to fetch whatever they need, but this exposes you to the risk of denial of service attacks.

Let’s understand this through an example which we’ve been referring to for this whole article.

Now we have a related field in video type which returns related videos. And each related video is of the graphql video type so they all have related videos too…and this goes on.

Consider the following query to understand the severity of the situation:

{
  Videos(limit: 10, offset: 0){
    name
    url
    related(limit: 10, offset: 0){
      name
      url
      related(limit: 10, offset: 0){
        name
        url
        related(limit: 100, offset: 0){
          name
          url
        }
      }
    }
  }
}

If I add one more subobject or increase the limit to 100, then it will be millions of videos loading in one call. Perhaps (or rather definitely) this will make your database and service unresponsive.

gqlgen provides a way to define the maximum query complexity allowed in one call. You just need to add one line (Line 5 in the following snippet) in your graphql handler and define the maximum complexity (300 in our case).

rootHandler:= dataloaders.DataloaderMiddleware(
	db,
	handler.GraphQL(
		go_graphql_demo.NewExecutableSchema(go_graphql_demo.NewRootResolvers(db)),
		handler.ComplexityLimit(300)
  ),
)

gqlgen assigns fix complexity weight for each field so it will consider struct, array, and string all as equals. So for this query, complexity will be 12. But we know that nested fields weigh too much, so we need to tell gqlgen to calculate accordingly (in simple terms, use multiplication instead of just sum).

func NewRootResolvers(db *sql.DB) Config {
	c := Config{
		Resolvers: &Resolver{
			db: db,
		},
	}

	// Complexity
	countComplexity := func(childComplexity int, limit *int, offset *int) int {
		return *limit * childComplexity
	}
	c.Complexity.Query.Videos = countComplexity
	c.Complexity.Video.Related = countComplexity

	// Schema Directive
	c.Directives.IsAuthenticated = func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
		ctxUserID := ctx.Value(UserIDCtxKey)
		if ctxUserID != nil {
			return next(ctx)
		} else {
			return nil, errors.UnauthorisedError
		}
	}
	return c
}

Just like directives, complexity is also defined as struct, so we have changed our config method accordingly.

I haven’t defined the related method logic and just returned the empty array. So related is empty in the output, but this should give you a clear idea about how to use the query complexity.

Final Notes

This code is on Github. You can play around with it, and if you have any questions or concerns let me know in the comment section.

Learn More

Build a Basic App with Spring Boot and JPA using PostgreSQL

How to Install PostgreSQL on Ubuntu 18.04

An Introduction to Queries in PostgreSQL

GraphQL Tutorial: Understanding Spring Data JPA/SpringBoot

How To Manage an SQL Database

GraphQL with React: The Complete Developers Guide

GraphQL with Angular & Apollo - The Full-stack Guide

GraphQL: Learning GraphQL with Node.Js

Complete guide to building a GraphQL API

GraphQL: Introduction to GraphQL for beginners

File Upload with Vue, Apollo Client and GraphQL

File Upload with Vue, Apollo Client and GraphQL

In this tutorial, I'll be showing you how to hand file uploads in GraphQL by building a fullstack app.

The tutorial will be divided into two main sections: building the GraphQL API and the frontend app. The GraphQL API will be built using Apollo Server and the frontend app will be built with Vue.js and Vue Apollo.

Table of Contents

  • Prerequisites
  • What we'll be building
  • Getting your Cloudinary keys
  • Building the GraphQL server
  • Building the frontend app
  • Fetching all photos
  • Uploading photo
  • Conclusion
Prerequisites

This tutorial assumes you conformatable with GraphQL and using it in a Vue app.

What we'll be building

For the purpose of this tutorial, we'll be building a simple photo album app, where users will be able to upload as well as view their photos. All the photos will be uploaded directly to Cloudinary. Below is a quick demo of the final app:

File Upload with Vue, Apollo Client and GraphQL

Getting your Cloudinary keys

Before we dive into code, let’s make sure we have our Cloudinary key in place. If you don’t already have an account with them, you can signup for free. Otherwise, login to your dashboard where you would see your account details along with your keys.

File Upload with Vue, Apollo Client and GraphQL

Building the GraphQL server

Now, let's start building the GraphQL server. First, let's create our project directory:

$ mkdir graphql-vue-photo-upload && cd graphql-vue-photo-upload
$ mkdir server && cd server
$ npm init -y

All the GraphQL API related code will be inside the server directory. Next, let’s install the necessary dependencies for our GraphQL server:

$ npm install graphql apollo-server cloudinary dotenv

Addition to installing Apollo Server and it dependence, we also install the Cloudinary Node.js library and a package to read environment variables.

Once that’s done installing, we can start building the GraphQL server. Create a new src directory and create a new index.js file inside it, then add the following code in it:

// server/src/index.js

const { ApolloServer } = require('apollo-server')
require('dotenv').config()
const typeDefs = require('./schema')
const resolvers = require('./resolvers')

const server = new ApolloServer({
  typeDefs,
  resolvers
})

server.listen().then(({ url }) => console.log(`Server ready at ${url}`))

Next, we need to create the schema and resolvers which our GraphQL server references. We’ll start with creating the schema. Create a schema.js inside src directory and paste the following code into it:

// server/src/schema.js

const { gql } = require('apollo-server')

const typeDefs = gql`type Photo {
    filename: String!
    path: String!
  }

  type Query {
    allPhotos: [Photo]
  }

  type Mutation {
    uploadPhoto(photo: Upload!): Photo!
  }`

module.exports = typeDefs

Here, we define a Photo type that comprises of two fields: the filename of the photo and the path to the actual photo. Then we define a single query allPhotos for fetching all uploaded photos. Lastly, we have a mutation for uploading a photo. The uploadPhoto mutation accepts a single argument, which is the photo that is to be uploaded. The argument is of the scalar type Upload, which is made available to us my Apollo Server, since it has built-in support of file upload. The mutation will return the uploaded photo.

The next, thing to do is create the resolvers. Still inside src directory, create a resolvers.js and add the code below in it:

// server/src/resolvers.js

const cloudinary = require('cloudinary').v2

cloudinary.config({
  cloud_name: process.env.CLOUD_NAME,
  api_key: process.env.API_KEY,
  api_secret: process.env.API_SECRET
})

const photos = []

const resolvers = {
  Query: {
    allPhotos () {
      return photos
    }
  },
  Mutation: {
    async uploadPhoto (parent, { photo }) {
      const { filename, createReadStream } = await photo

      try {
        const result = await new Promise((resolve, reject) => {
          createReadStream().pipe(
            cloudinary.uploader.upload_stream((error, result) => {
              if (error) {
                reject(error)
              }

              resolve(result)
            })
          )
        })

        const newPhoto = { filename, path: result.secure_url }

        photos.push(newPhoto)

        return newPhoto
      } catch (err) {
        console.log(err)
      }
    }
  }
}

module.exports = resolvers

First, we pull in the Cloudinary library and configured it will our credentials, which are getting from the environment variables. Then we create an empty array, which will hold our photos. Next, we define the resolver for the allPhotos query, which simply returns the photos array.

For the uploadPhoto mutation, Apollo Server would return the selected file as Promise, which contains a bunch of details about the file, such as: createReadStream, filename, mimetype and encoding. In this tutorial, we’ll only be making use of the first two, so we extract them from the object. Using createReadStream, we stream the file directly to Cloudinary. Since it is an asynchronous operation we wrap it in a Promise and awaits it. If the Promise was resolved, that is, the file was uploaded successfully to Cloudinary, we create a new object containing the uploaded file name and the Cloudinary path to the file. Then we push the new object to the photos array and finally return the new object.

Lastly, if there was an error uploading the file to Cloudinary, we simply console log the erorr.

Before we wrap up the GraphQL API, let’s quickly add our environment variables. Create a .env file directly in the server directory and add the code below in it:

// server/.env

CLOUD_NAME=YOUR_CLOUD_NAME
API_KEY=YOUR_API_KEY
API_SECRET=YOUR_API_SECRET

Remember to replace the placeholders with your actual account details.

Finally, let’s start the server:

$ node src/index.js

The server should be running on [[http://localhost:4000](http://localhost:4000)](http://localhost:4000](http://localhost:4000))

Building the frontend app

As already mentioned, the frontend app will be built with Vue.js, so let’s create a new Vue.js app using the Vue CLI:

$ vue create client

When prompted, press enter to select the default preset. Start the app and leave it running while we build on it:

$ cd client
$ yarn serve

The app should be running on http://localhost:8080

Once the Vue app is created, let’s install the necessary dependencies:

$ npm install vue-apollo graphql-tag graphql apollo-cache-inmemory apollo-client apollo-upload-client

Out of these dependencies, the one that is new and that I’d like to point out is [apollo-upload-client]([https://github.com/jaydenseric/apollo-upload-client)](https://github.com/jaydenseric/apollo-upload-client)). It’s a package for Apollo Client that allows us to send GraphQL multipart requests. It will be used in place of apollo-link.

Next, let’s configure Vue Apollo and these dependencies. Update main.js as below:

// client/src/main.js

import { InMemoryCache } from 'apollo-cache-inmemory'
import { ApolloClient } from 'apollo-client'
import { createUploadLink } from 'apollo-upload-client'
import Vue from 'vue'
import VueApollo from 'vue-apollo'
import App from './App.vue'

Vue.config.productionTip = false

Vue.use(VueApollo)

const apolloClient = new ApolloClient({
  link: createUploadLink({ uri: 'http://localhost:4000' }),
  cache: new InMemoryCache()
})

const apolloProvider = new VueApollo({
  defaultClient: apolloClient
})

new Vue({
  apolloProvider,
  render: h => h(App)
}).$mount('#app')

You’ll notice here we are using createUploadLink from apollo-upload-client to create the ApolloClient link, passing to it our GraphQL API endpoint.

To give our app a bit of styling, we’ll be pulling in UIKit. Add the line below to head section of index.html:





Fetching all photos

We’ll start with the GraphQL query for fetching all our photo. Create a graphql directory inside the client/src directory and with in, create a AllPhotos.js file and paste the code below into it:

// client/src/graphql/AllPhotos.js

import gql from 'graphql-tag'

export default gql`query allPhotos {
    allPhotos {
      filename
      path
    }
  }`

For the sake of simplicity, we’ll be making use of just the App.vue component. So let’s update it as below:

// client/src/App.vue


  
    
      ## Photo Album


      
        
          
            
              ![](photo.path)
            
            {{ photo.filename }}

          
        
      
    
  



Within the apollo object, we add the ALL_PHOTOS query to fetch all photos and save it in a allPhotos. Once allPhotos is populated with data from our GraphQL API, we display the photos by looping through them.

If we view our app, we should get something similar to below:
File Upload with Vue, Apollo Client and GraphQL

Uploading photo

Of course, we need to have uploaded some photos before we can see them. Let’s implement that now. Still inside the graphql directory, create a UploadPhoto.js and paste the code below into it:

// client/src/graphql/UploadPhoto.js

import gql from 'graphql-tag'

export default gql`mutation uploadPhoto($photo: Upload!) {
    uploadPhoto(photo: $photo) {
      filename
      path
    }
  }`

Next, add the snippet below to the template section of App.vue, just below the Photo Album heading:

// client/src/App.vue


  

Here, we have a file input field that accepts only images. On change of the input field a uploadPhoto method is triggered.

In the script section, add:

// client/src/App.vue

import UPLOAD_PHOTO from "./graphql/UploadPhoto";

methods: {
  async uploadPhoto({ target }) {
    await this.$apollo.mutate({
      mutation: UPLOAD_PHOTO,
      variables: {
        photo: target.files[0]
      },
      update: (store, { data: { uploadPhoto } }) => {
        const data = store.readQuery({ query: ALL_PHOTOS });

        data.allPhotos.push(uploadPhoto);

        store.writeQuery({ query: ALL_PHOTOS, data });
      }
    });
  }
}

We get extract the target from the input event, then call the mutate method, passing to it the UPLOAD_PHOTO mutation as well as the required argument (through the variables object). We get the selected file from the files on the target object. Once the mutation is executed, we update the cache by adding the newly uploaded photo to the allPhotos array.
File Upload with Vue, Apollo Client and GraphQL

File Upload with Vue, Apollo Client and GraphQL

Conclusion

So in this tutorial, we have seen how to handle file uploads in GraphQL using Apollo Server on the server side and Vue and Vue Apollo on the client side. Though we used Cloudinary to store our photos, you can easily wrap it for any other cloud storage service or you can even save directly to your own local filesystem.

How to build a simple CRUD with GraphQL and Flutter

How to build a simple CRUD with GraphQL and Flutter

In this tutorial, I am going to show you how to build a simple CRUD with Flutter (client-side) and GraphQL (server-side)

Originally published by Daniel Berrío Barrera at https://medium.com

Flutter is an SDK (Software development kit) to build applications for Android and IOs with a single codebase. On the other hand, GraphQL is a query language that allows us to create queries that will be executing on the server. In this tutorial, I am going to show you how to build a simple CRUD with Flutter (client-side) and GraphQL (server-side). Let’s start

Once we have created our Flutter project we need to import some dependencies in our pubspec.yaml file in order to use GraphQL and HTTP with our API. It’s essential to notice that the API was created on NodeJS, so for this tutorial I’m not going to show how to build it.

dependencies:
  flutter:
    sdk: flutter
  graphql_flutter: ^2.0.1
  http: ^0.12.0+2

Then, we are going to create a file called graphQLConf.dart, this file will contain the basic configuration to use GraphQL like our link to the API and the cache configuration. So, we create a class called GraphQLConfiguration, inside this, there are three main parts. The first one is the link to the API, the second one is the ValueNotifier which will notify its listener when any kind of value changed, and finally, the method clientToQuery() will let us make queries and mutation in our application.

import "package:flutter/material.dart";
import "package:graphql_flutter/graphql_flutter.dart";

class GraphQLConfiguration {
static HttpLink httpLink = HttpLink(
uri: "https://examplegraphql.herokuapp.com/graphql",
);

ValueNotifier<GraphQLClient> client = ValueNotifier(
GraphQLClient(
link: httpLink,
cache: OptimisticCache(dataIdFromObject: typenameDataIdFromObject),
),
);

GraphQLClient clientToQuery() {
return GraphQLClient(
cache: OptimisticCache(dataIdFromObject: typenameDataIdFromObject),
link: httpLink,
);
}
}

In our main.dart file we will wrap the MaterialApp Widget with the previous configuration as follow:

import 'package:flutter/material.dart';
import "package:graphql_flutter/graphql_flutter.dart";
import "services/graphqlConf.dart";
import "package:example/components/principal.dart";

GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration();

void main() => runApp(
GraphQLProvider(
client: graphQLConfiguration.client,
child: CacheProvider(child: MyApp()),
),
);

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Principal(),
);
}
}

As you can saw we called the GraphQLObject from the graphQLConf.dart file and then we wrap it into the main method. But we don’t know how the Principal Widget use there is made. Basically, during this example we are going to work with a class Person, this class will contain basic information such as the id, the name, the last name, and the age. The application lets us add a person, edit a person, see all the persons added and finally delete a person (a basic CRUD). So let’s create a class person.

class Person {
Person(this.id, this.name, this.lastName, this.age);

final String id;
final String name;
final String lastName;
final int age;

getId() => this.id;

getName() => this.name;

getLastName() => this.lastName;

getAge() => this.age;
}

As you can see is a simple class Person with the id, name, lastName and age attributes, also we had a constructor and the get methods to access its values. Then we need to do our queries to the API. GraphQL provides two forms:

  • Query: It allows us to access data and read it.
  • Mutation: It allows to modify our data. Add, edit or delete something.

For this, let’s create a class that will contain all the queries and mutation that we are going to use. (Note: Dart uses a notation with three quotes to make the queries).

class QueryMutation {
String addPerson(String id, String name, String lastName, int age) {
return """
mutation{
addPerson(id: "$id", name: "$name", lastName: "$lastName", age: $age){
id
name
lastName
age
}
}
""";
}

String getAll(){
return """
{
persons{
id
name
lastName
age
}
}
""";
}

String deletePerson(id){
return """
mutation{
deletePerson(id: "$id"){
id
}
}
""";
}

String editPerson(String id, String name, String lastName, int age){
return """
mutation{
editPerson(id: "$id", name: "$name", lastName: "$lastName", age: $age){
name
lastName
}
}
""";
}
}

Then, our Principal widget will use an AlertDialog to do the insert, read and delete operations, and then it will be called from our Widget. So let’s create the AlertDialog widget.

import "package:flutter/material.dart";
import "../services/graphqlConf.dart";
import "../services/queryMutation.dart";
import "package:graphql_flutter/graphql_flutter.dart";
import "package:example/components/person.dart";

class AlertDialogWindow extends StatefulWidget {
final Person person;
final bool isAdd;

AlertDialogWindow({Key key, this.person, this.isAdd}) : super(key: key);

@override
State<StatefulWidget> createState() =>
_AlertDialogWindow(this.person, this.isAdd);
}

class _AlertDialogWindow extends State<AlertDialogWindow> {
TextEditingController txtId = TextEditingController();
TextEditingController txtName = TextEditingController();
TextEditingController txtLastName = TextEditingController();
TextEditingController txtAge = TextEditingController();
GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration();
QueryMutation addMutation = QueryMutation();

final Person person;
final bool isAdd;

_AlertDialogWindow(this.person, this.isAdd);

@override
void initState() {
super.initState();
if (!this.isAdd) {
txtId.text = person.getId();
txtName.text = person.getName();
txtLastName.text = person.getLastName();
txtAge.text = person.getAge().toString();
}
}

@override
Widget build(BuildContext context) {
// TODO: implement build
return AlertDialog(
title: Text(this.isAdd ? "Add" : "Edit or Delete"),
content: Container(
child: SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width,
),
child: Stack(
children: <Widget>[
Container(
child: TextField(
maxLength: 5,
controller: txtId,
enabled: this.isAdd,
decoration: InputDecoration(
icon: Icon(Icons.perm_identity),
labelText: "ID",
),
),
),
Container(
padding: EdgeInsets.only(top: 80.0),
child: TextField(
maxLength: 40,
controller: txtName,
decoration: InputDecoration(
icon: Icon(Icons.text_format),
labelText: "Name",
),
),
),
Container(
padding: EdgeInsets.only(top: 160.0),
child: TextField(
maxLength: 40,
controller: txtLastName,
decoration: InputDecoration(
icon: Icon(Icons.text_rotate_vertical),
labelText: "Last name",
),
),
),
Container(
padding: EdgeInsets.only(top: 240.0),
child: TextField(
maxLength: 2,
controller: txtAge,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: "Age", icon: Icon(Icons.calendar_today)),
),
),
],
),
),
),
),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () => Navigator.of(context).pop(),
),
!this.isAdd
? FlatButton(
child: Text("Delete"),
onPressed: () async {
GraphQLClient _client = graphQLConfiguration.clientToQuery();
QueryResult result = await _client.mutate(
MutationOptions(
document: addMutation.deletePerson(txtId.text),
),
);
if (!result.hasErrors) Navigator.of(context).pop();
},
)
: null,
FlatButton(
child: Text(this.isAdd ? "Add" : "Edit"),
onPressed: () async {
if (txtId.text.isNotEmpty &&
txtName.text.isNotEmpty &&
txtLastName.text.isNotEmpty &&
txtAge.text.isNotEmpty) {
if (this.isAdd) {
GraphQLClient _client = graphQLConfiguration.clientToQuery();
QueryResult result = await _client.mutate(
MutationOptions(
document: addMutation.addPerson(
txtId.text,
txtName.text,
txtLastName.text,
int.parse(txtAge.text),
),
),
);
if (!result.hasErrors) {
txtId.clear();
txtName.clear();
txtLastName.clear();
txtAge.clear();
Navigator.of(context).pop();
}
} else {
GraphQLClient _client = graphQLConfiguration.clientToQuery();
QueryResult result = await _client.mutate(
MutationOptions(
document: addMutation.editPerson(
txtId.text,
txtName.text,
txtLastName.text,
int.parse(txtAge.text),
),
),
);
if (!result.hasErrors) {
txtId.clear();
txtName.clear();
txtLastName.clear();
txtAge.clear();
Navigator.of(context).pop();
}
}
}
},
)
],
);
}
}

In the _AlertDialog we used all the mutations created to add, edit and delete a person. The process is as follows:

  • We called our GraphQl configuration from GraphQLConfiguration: graphQLConfiguration = GraphQLConfiguration();
  • We get the mutations from the class we have created: QueryMutation addMutation = QueryMutation();
  • We create our GraphQL client to connect to our backend: GraphQLClient _client = graphQLConfiguration.clientToQuery();
  • Finally we do our mutation with the required parameters.

Once we have done the previous steps we check is our result is correct, and if that is true, just return to the main page and clear all the TextFields. Basically, all the queries and mutations done during the example are based under the same principle: declare your variables, do your query or mutation and finally check if that has errors.

Finally, we have our whole Principal widget, and there is something more we need to remark. Inside our initState() we are going to call all the data saved in our backend, and to do that we use our function fillList(), and basically it will fill a listPerson with all the data stored, to access to it, we do result.data[“name our query or mutation”][index][“name of the variable”] and that is it.

import "package:flutter/material.dart";
import "package:example/components/alertDialogs.dart";
import "package:graphql_flutter/graphql_flutter.dart";
import "package:example/components/person.dart";
import "../services/graphqlConf.dart";
import "../services/queryMutation.dart";

class Principal extends StatefulWidget {
@override
State<StatefulWidget> createState() => _Principal();
}

class _Principal extends State<Principal> {
List<Person> listPerson = List<Person>();
GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration();

void fillList() async {
QueryMutation queryMutation = QueryMutation();
GraphQLClient _client = graphQLConfiguration.clientToQuery();
QueryResult result = await _client.query(
QueryOptions(
document: queryMutation.getAll(),
),
);
if (!result.hasErrors) {
for (var i = 0; i < result.data["persons"].length; i++) {
setState(() {
listPerson.add(
Person(
result.data["persons"][i]["id"],
result.data["persons"][i]["name"],
result.data["persons"][i]["lastName"],
result.data["persons"][i]["age"],
),
);
});
}
}
}

@override
void initState() {
super.initState();
fillList();
}

void _addPerson(context) {
showDialog(
context: context,
builder: (BuildContext context) {
AlertDialogWindow alertDialogWindow =
new AlertDialogWindow(isAdd: true);
return alertDialogWindow;
},
).whenComplete(() {
listPerson.clear();
fillList();
});
}

void _editDeletePerson(context, Person person) {
showDialog(
context: context,
builder: (BuildContext context) {
AlertDialogWindow alertDialogWindow =
new AlertDialogWindow(isAdd: false, person: person);
return alertDialogWindow;
},
).whenComplete(() {
listPerson.clear();
fillList();
});
}

@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Example"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.add_circle_outline),
onPressed: () => _addPerson(context),
tooltip: "Insert new person",
),
],
),
body: Stack(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
child: Text(
"Person",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 30.0),
),
),
Container(
padding: EdgeInsets.only(top: 50.0),
child: ListView.builder(
itemCount: listPerson.length,
itemBuilder: (context, index) {
return ListTile(
selected: listPerson == null ? false : true,
title: Text(
"${listPerson[index].getName()}",
),
onTap: () {
_editDeletePerson(context, listPerson[index]);
},
);
},
),
),
],
),
);
}
}

Our result!


Build CRUD Mobile App using React Native, Elements, Navigation, Apollo Client and GraphQL

Build CRUD Mobile App using React Native, Elements, Navigation, Apollo Client and GraphQL

A comprehensive step by step tutorial on learn to build CRUD (Create, Read, Update, Delete) using React Native, Elements, Navigation, Apollo Client and GraphQL

A comprehensive step by step tutorial on learn to build CRUD (Create, Read, Update, Delete) using React Native, Elements, Navigation, Apollo Client and GraphQL

We will use existing Node.js, Express.js, MongoDB, and GraphQL that already created in React.js tutorial. So, we will focus on accessing GraphQL from the React Native Mobile Apps. Same exactly as React.js tutorial, we will use Apollo Client GraphQL to integrating with React Native.

The flow of CRUD Mobile Apps is very basic. It just the main page that shows the list of data, the details page, the edit, and the create data page. In this tutorial, we will use React Native and React Native Elements components including ScrollView, TextInput, Text, Button, View, etc. The list page contains a list of the book with the details button and a button to add new data on the right corner of the header. The details page contains data details text, the back button on the left corner of the header, an edit button and a delete button. The add-data page contains a React Native form, back button, and save button. The edit-data page contains the React Native form, back data, and update button. Simply as that, we will try to build this React Native Mobile Apps to Android and iOS devices.

The Example of React Native Navigation and Elements wrap together with Apollo Client GraphQL

The following tools, frameworks, and modules are required for this tutorial:

Before start to the main steps, make sure that you have installed Node.js and can run npm in the terminal or command line. To check the existing or installed Node.js environment open the terminal/command line then type this command.

node -v
v10.15.1
npm -v
6.9.0
yarn -v
1.10.1

React Native and Apollo GraphQL Tutorial: Install create-react-native-app and Create App

The create-react-native-app is a tool for creating a React Native App. To install it, type this command in your App projects folder.

sudo npm install -g create-react-native-app

Then create a React Native App using this create-react-native-app command.

create-react-native-app RnGraphql

If there question to install Expo, just type Y and choose a blank template because we will add the navigation later. That command will create a React Native app then install all required modules. The app or project folder will contain these folders and files.

|-- App.js
|-- app.json
|-- assets
| |-- icon.png
| `-- splash.png
|-- node_modules
`-- package.json

Next, go to the newly created React App folder.

cd RnGraphql

This React Native App is running via Expo app, before running on your Android or iOS device, make sure you have to install the Expo App to Android or Expo Client to iOS. Of course, that app is available in the App Store. So, we assume that you have installed the Expo App in your device then type this command to run the app.

npm start
or
yarn start

You will see this barcode and instruction in the terminal or command line.

To open the app in the Android device, open the Expo App first then tap on Scan QR Code button. Scan the barcode in the terminal or command line then you will see the React Native Android App like this after waiting for minutes the build process.

For iOS Device, press s from the keyboard to send React Native App URL to your Email or SMS. Enter your phone number or Email address (We use an email address) then press Enter. You will get this email to your mailbox.

Choose open in Expo URL then open in your browser, that will be redirected to Expo App. In Expo App welcome screen, shake your phone to open the React Native App. Now, you will see this screen in your iOS device.

Make sure you have the same network between your device and your development computer.

React Native and Apollo GraphQL Tutorial: Add Navigation Header and required Screen

As you see, the React Native app that you can only show plain text which unlike the standard Android or iOS Mobile Apps. To make a standard Android or iOS Mobile Apps, we need to add React Native header and navigation button with an icon to the React Native view. Type this command to install React Native Navigation (react-navigation).

yarn add react-navigation

Also, add the React Native Gesture Handler module by type this command.

yarn add react-native-gesture-handler

We have to link react-native to react-native-gesture-handler.

react-native link react-native-gesture-handler

Next, create a folder for components and components files in the root of the app folder by type these commands.

mkdir components
touch components/BooksScreen.js
touch components/BookDetailScreen.js
touch components/AddBookScreen.js
touch components/EditBookScreen.js

We will fill that files with simple React Native Button, Text, View, props, and navigation. Open and edit components/BooksScreen.js then add this React codes.

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';

class BooksScreen extends Component {
 static navigationOptions = {
  title: 'Books List',
 };
 render() {
  return (
   <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text>Books List</Text>
    <Button
     title="Go to Details"
     onPress={() => this.props.navigation.navigate('BookDetails')}
    />
    <Button
     title="Go to Add Book"
     onPress={() => this.props.navigation.navigate('AddBook')}
    />
    <Button
     title="Go to Edit Book"
     onPress={() => this.props.navigation.navigate('EditBook')}
    />
   </View>
  );
 }
}

export default BooksScreen;

Open and edit components/BookDetailScreen.js then add this React codes.

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';

class BookDetailScreen extends Component {
 static navigationOptions = {
  title: 'Book Details',
 };
 render() {
  return (
   <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text>Book Details</Text>
    <Button
     title="Go to Details... again"
     onPress={() => this.props.navigation.push('BookDetails')}
    />
    <Button
     title="Go to Home"
     onPress={() => this.props.navigation.navigate('Book')}
    />
    <Button
     title="Go back"
     onPress={() => this.props.navigation.goBack()}
    />
   </View>
  );
 }
}

export default BookDetailScreen;

Open and edit components/AddBookScreen.js then add this React codes.

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';

class AddBookScreen extends Component {
 static navigationOptions = {
  title: 'Add Book',
 };
 render() {
  return (
   <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text>Add Book</Text>
    <Button
     title="Go to Add Book... again"
     onPress={() => this.props.navigation.push('AddBook')}
    />
    <Button
     title="Go to Home"
     onPress={() => this.props.navigation.navigate('Book')}
    />
    <Button
     title="Go back"
     onPress={() => this.props.navigation.goBack()}
    />
   </View>
  );
 }
}

export default AddBookScreen;

Open and edit components/EditBookScreen.js then add this React codes.

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';

class EditBookScreen extends Component {
 static navigationOptions = {
  title: 'Edit Book',
 };
 render() {
  return (
   <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text>Add Book</Text>
    <Button
     title="Go to Edit Book... again"
     onPress={() => this.props.navigation.push('EditBook')}
    />
    <Button
     title="Go to Home"
     onPress={() => this.props.navigation.navigate('Book')}
    />
    <Button
     title="Go back"
     onPress={() => this.props.navigation.goBack()}
    />
   </View>
  );
 }
}

export default EditBookScreen;

Next, open and edit App.js then add replace all codes with this.

import { createStackNavigator, createAppContainer } from 'react-navigation';
import BooksScreen from './components/BooksScreen';
import BookDetailScreen from './components/BookDetailScreen';
import AddBookScreen from './components/AddBookScreen';
import EditBookScreen from './components/EditBookScreen';

const MainNavigator = createStackNavigator({
 Book: { screen: BooksScreen },
 BookDetails: { screen: BookDetailScreen },
 AddBook: { screen: AddBookScreen },
 EditBook: { screen: EditBookScreen },
});

const App = createAppContainer(MainNavigator);

export default App;

Next, run again the React Native app then refresh your Expo app. You will see the standard Android or iOS Mobile Apps in your device screen.

React Native and Apollo GraphQL Tutorial: Install and Configure Apollo Client GraphQL

Now, we have to install and configure all of the required modules and dependencies to the **React Apollo **and GraphQL. Type this command to install the modules at once.

yarn add react-apollo apollo-client apollo-cache-inmemory apollo-link-http graphql-tag graphql

Next, open and edit App.js then replace all imports with these imports.

import React from 'react';
import { AppRegistry } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import BooksScreen from './components/BooksScreen';
import BookDetailScreen from './components/BookDetailScreen';
import AddBookScreen from './components/AddBookScreen';
import EditBookScreen from './components/EditBookScreen';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';
import { ApolloProvider } from 'react-apollo';

Change the constant name of createAppContainer constant variable.

const MyRootComponent = createAppContainer(MainNavigator);

Add these lines of Apollo Client constant variables.

const cache = new InMemoryCache();
const client = new ApolloClient({
 cache,
 link: new HttpLink({
    uri: 'http://localhost:3000/graphql',
 }),
});

const App = () => (
 <ApolloProvider client={client}>
  <MyRootComponent />
 </ApolloProvider>
);

Register the Apollo Provider by adding this line before the export line.

AppRegistry.registerComponent('MyApp', () => App);

React Native and Apollo GraphQL Tutorial: Show List of Books

To show the list of the books in the Books components, open and edit components/BooksScreen.js then add/replace these imports.

import React, { Component } from 'react';
import { StyleSheet, FlatList, ActivityIndicator, View, Text } from 'react-native';
import { ListItem, Button } from 'react-native-elements';
import gql from 'graphql-tag';
import { Query } from 'react-apollo';

As you can see, there are view elements that use react-native-elements module. For that, install the react-native-elements module first by type this command.

yarn add react-native-elements

Declare a constant before the class name for the query.

const GET_BOOKS = gql`
 {
  books {
   _id
   title
   author
  }
 }
`;

Replace navigationOptions with this.

static navigationOptions = ({ navigation }) => {
  return {
    title: 'LIST OF BOOKS',
    headerRight: (
    <Button
      buttonStyle={{ padding: 0, backgroundColor: 'transparent' }}
      icon={{ name: 'add-circle', style: { marginRight: 0, fontSize: 28 } }}
      onPress={() => { navigation.push('AddBook') }}
    />
    ),
  };
};

Add the function to extract and mapping result from GraphQL and render list item from the FlatList that will be added to the render section.

keyExtractor = (item, index) => index.toString()

renderItem = ({ item }) => (
  <ListItem
    title={item.title}
    onPress={() => {
      this.props.navigation.navigate('BookDetails', {
        id: `${item._id}`,
      });
    }}
    chevron
    bottomDivider
  />
)

Replace all React Native view render with Apollo Client GraphQL Query with pollInterval to make the page always request GraphQL data.

render() {
  return (
    <Query pollInterval={500} query={GET_BOOKS}>
      {({ loading, error, data }) => {
        if (loading) return(
          <View style={styles.activity}>
            <ActivityIndicator size="large" color="#0000ff"/>
          </View>
        );
        if (error) return(
          <View style={styles.activity}>
            <Text>`Error! ${error.message}`</Text>
          </View>
        );
        return (
          <FlatList
            keyExtractor={this.keyExtractor}
            data={data.books}
            renderItem={this.renderItem}
          />
        );
      }}
    </Query>
  );
}

Add React Native styles constant variables before the export code.

const styles = StyleSheet.create({
 container: {
   flex: 1,
   paddingBottom: 22
 },
 item: {
   padding: 10,
   fontSize: 18,
   height: 44,
 },
 activity: {
   position: 'absolute',
   left: 0,
   right: 0,
   top: 0,
   bottom: 0,
   alignItems: 'center',
   justifyContent: 'center'
 }
})

React Native and Apollo GraphQL Tutorial: Show a Book Details

To show book details, we have to modify components/BookDetailScreen.js then replace all imports with these imports.

import React, { Component } from 'react';
import { ScrollView, StyleSheet, ActivityIndicator, View, Text } from 'react-native';
import { Card, Button } from 'react-native-elements';
import gql from 'graphql-tag';
import { Query, Mutation } from 'react-apollo';

Declare the constant variables of the GraphQL gql or Queries.

const GET_BOOK = gql`
  query book($bookId: String) {
    book(id: $bookId) {
      _id
      isbn
      title
      author
      description
      published_year
      publisher
      updated_date
    }
  }
`;

const DELETE_BOOK = gql`
 mutation removeBook($id: String!) {
  removeBook(id:$id) {
   _id
  }
 }
`;

Modify the render section of the class to display a book details by implementing Apollo GraphQL Query and React Native ScrollView, Card, View, Button, Text, ActivityIndicator, etc.

class BookDetailScreen extends Component {
 static navigationOptions = {
  title: 'Book Details',
 };
 render() {
  const { navigation } = this.props;
  return (
    <Query pollInterval={500} query={GET_BOOK} variables={{ bookId: navigation.getParam('id') }}>
      {({ loading, error, data }) => {
        if (loading) return(<View style={styles.activity}>
          <ActivityIndicator size="large" color="#0000ff" />
         </View>);
        if (error) return(<Text>`Error! ${error.message}`</Text>);
        return (
          <ScrollView>
            <Card style={styles.container}>
              <View style={styles.subContainer}>
                <View>
                  <Text style={{fontSize: 16, fontWeight: 'bold'}}>ISBN:</Text>
                  <Text style={{fontSize: 18, marginBottom: 10}}>{data.book.isbn}</Text>
                </View>
                <View>
                  <Text style={{fontSize: 16, fontWeight: 'bold'}}>Title: </Text>
                  <Text style={{fontSize: 18, marginBottom: 10}}>{data.book.title}</Text>
                </View>
                <View>
                  <Text style={{fontSize: 16, fontWeight: 'bold'}}>Author: </Text>
                  <Text style={{fontSize: 18, marginBottom: 10}}>{data.book.author}</Text>
                </View>
                <View>
                  <Text style={{fontSize: 16, fontWeight: 'bold'}}>Description: </Text>
                  <Text style={{fontSize: 18, marginBottom: 10}}>{data.book.description}</Text>
                </View>
                <View>
                  <Text style={{fontSize: 16, fontWeight: 'bold'}}>Published Year: </Text>
                  <Text style={{fontSize: 18, marginBottom: 10}}>{data.book.published_year}</Text>
                </View>
                <View>
                  <Text style={{fontSize: 16, fontWeight: 'bold'}}>Publisher: </Text>
                  <Text style={{fontSize: 18, marginBottom: 10}}>{data.book.publisher}</Text>
                </View>
                <View>
                  <Text style={{fontSize: 16, fontWeight: 'bold'}}>Updated Date: </Text>
                  <Text style={{fontSize: 18}}>{data.book.updated_date}</Text>
                </View>
              </View>
              <Mutation mutation={DELETE_BOOK} key={data.book._id} onCompleted={() => navigation.goBack()}>
                {(removeBook, { loading2, error2 }) => (
                  <View style={styles.subContainer}>
                    <Button
                    style={styles.detailButton}
                    large
                    backgroundColor={'#CCCCCC'}
                    leftIcon={{name: 'edit'}}
                    title='Edit'
                    onPress={() => {
                      navigation.navigate('EditBook', { id: `${data.book._id}`, });
                    }} />
                    <Button
                    style={styles.detailButton}
                    large
                    backgroundColor={'#999999'}
                    color={'#FFFFFF'}
                    leftIcon={{name: 'delete'}}
                    title='Delete'
                    onPress={() => {
                      removeBook({ variables: { id: data.book._id } })
                      .then(res => res)
                      .catch(err => <Text>{err}</Text>);
                    }} />
                    {loading2 && <View style={styles.activity}>
                      <ActivityIndicator size="large" color="#0000ff" />
                     </View>}
                    {error2 && <Text>`Error! ${error2.message}`</Text>}
                  </View>
                )}
              </Mutation>
            </Card>
          </ScrollView>
        );
      }}
    </Query>
  );
 }
}

Modify or add the React Native style to match the modified details page.

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20
  },
  subContainer: {
    flex: 1,
    paddingBottom: 20,
    borderBottomWidth: 2,
    borderBottomColor: '#CCCCCC',
  },
  activity: {
    position: 'absolute',
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    alignItems: 'center',
    justifyContent: 'center'
  },
  detailButton: {
    marginTop: 10
  }
})

React Native and Apollo GraphQL Tutorial: Add a New Book

To add a new book, we have an add button in the List of Book page that will push the Add Page. This Add Page will use Apollo GraphQL Mutation instead of React Form. The React Native TextInput change will use a function that triggers the OnChange event. Open and edit components/AddBookScreen.js then replace all imports with these imports.

import React, { Component } from 'react';
import { StyleSheet, ScrollView, ActivityIndicator, View, TextInput, Text } from 'react-native';
import { Button } from 'react-native-elements';
import gql from "graphql-tag";
import { Mutation } from "react-apollo";

Declare a constant variable of the Apollo GraphQL gql or Query before the class name.

const ADD_BOOK = gql`
  mutation AddBook(
    $isbn: String!,
    $title: String!,
    $author: String!,
    $description: String!,
    $publisher: String!,
    $published_year: Int!) {
    addBook(
      isbn: $isbn,
      title: $title,
      author: $author,
      description: $description,
      publisher: $publisher,
      published_year: $published_year) {
      _id
    }
  }
`;

Add an object of the state after the navigationOptions that consists of required fields that match the GraphQL data.

state = {
 isbn: '',
 title: '',
 author: '',
 description: '',
 published_year: '',
 publisher: '',
}

Add a function to update the React Native TextInput value and state object values.

updateTextInput = (text, field) => {
 const state = this.state
 state[field] = text;
 this.setState(state);
}

Modify the React Native render section to add Apollo GraphQL Mutation, React Native TextInput, ScrollView, View, ActivityIndicator, etc.

render() {
  const { isbn, title, author, description, published_year, publisher } = this.state;
  return (
   <Mutation mutation={ADD_BOOK} onCompleted={() => this.props.navigation.goBack()}>
     {(addBook, { loading, error }) => (
      <ScrollView style={styles.container}>
       <View style={styles.subContainer}>
        <TextInput
          style={styles.textInput}
          placeholder={'ISBN'}
          value={this.state.isbn}
          onChangeText={(text) => this.updateTextInput(text, 'isbn')}
        />
       </View>
       <View style={styles.subContainer}>
        <TextInput
          style={styles.textInput}
          placeholder={'Title'}
          value={this.state.title}
          onChangeText={(text) => this.updateTextInput(text, 'title')}
        />
       </View>
       <View style={styles.subContainer}>
        <TextInput
          style={styles.textInput}
          placeholder={'Author'}
          value={this.state.author}
          onChangeText={(text) => this.updateTextInput(text, 'author')}
        />
       </View>
       <View style={styles.subContainer}>
        <TextInput
          style={styles.textInput}
          placeholder={'Description'}
          multiline={true}
          numberOfLines={4}
          value={this.state.description}
          onChangeText={(text) => this.updateTextInput(text, 'description')}
        />
       </View>
       <View style={styles.subContainer}>
        <TextInput
          style={styles.textInput}
          placeholder={'Published Year'}
          value={this.state.published_year}
          keyboardType='numeric'
          onChangeText={(text) => this.updateTextInput(text, 'published_year')}
        />
       </View>
       <View style={styles.subContainer}>
        <TextInput
          style={styles.textInput}
          placeholder={'Publisher'}
          value={this.state.publisher}
          onChangeText={(text) => this.updateTextInput(text, 'publisher')}
        />
       </View>
       <View>
        <Button
         large
         leftIcon={{name: 'save'}}
         title='Save'
         onPress={() => {
          addBook({
           variables: {
            isbn: this.state.isbn,
            title: this.state.title,
            author: this.state.author,
            description: this.state.description,
            publisher: this.state.publisher,
            published_year: parseInt(this.state.published_year),
           }
          })
           .then(res => this.setState({ isbn: '', title: '', author: '', description: '', published_year: '', publisher }))
           .catch(err => <Text>{err}</Text>);
         }} />
       </View>
       {loading && <View style={styles.activity}>
         <ActivityIndicator size="large" color="#0000ff" />
        </View>}
       {error && <Text>`Error! ${error.message}`</Text>}
      </ScrollView>
     )}
    </Mutation>
  );
 }
}

Also, modify the React Native style to match the required views.

const styles = StyleSheet.create({
 container: {
  flex: 1,
  padding: 20
 },
 subContainer: {
  flex: 1,
  marginBottom: 20,
  padding: 5,
  borderBottomWidth: 2,
  borderBottomColor: '#CCCCCC',
 },
 activity: {
  position: 'absolute',
  left: 0,
  right: 0,
  top: 0,
  bottom: 0,
  alignItems: 'center',
  justifyContent: 'center'
 },
 textInput: {
  fontSize: 18,
  margin: 5,
 },
})

React Native and Apollo GraphQL Tutorial: Edit a Book

The Edit Book Screen pushed from the Book Details Screen. Same as the previous Add Book Page, this page will contain the Apollo GraphQL Mutation and React Native TextInput with additional Apollo GraphQL Query. All React Native TextInput will fill with the data from GraphQL Query. Open and edit components/EditBookScreen.js then replace all imports with these imports.

import React, { Component } from 'react';
import { StyleSheet, ScrollView, ActivityIndicator, View, TextInput, Text } from 'react-native';
import { Button } from 'react-native-elements';
import gql from "graphql-tag";
import { Query, Mutation } from "react-apollo";

Declare the constant variables of the Apollo GraphQL Query or gql after the imports.

const GET_BOOK = gql`
  query book($bookId: String) {
    book(id: $bookId) {
      _id
      isbn
      title
      author
      description
      published_year
      publisher
      updated_date
    }
  }
`;

const UPDATE_BOOK = gql`
  mutation updateBook(
    $id: String!,
    $isbn: String!,
    $title: String!,
    $author: String!,
    $description: String!,
    $publisher: String!,
    $published_year: Int!) {
    updateBook(
    id: $id,
    isbn: $isbn,
    title: $title,
    author: $author,
    description: $description,
    publisher: $publisher,
    published_year: $published_year) {
      updated_date
    }
  }
`;

The above constant variables are Apollo GraphQL Query of the GET and the UPDATE Book data. Next, inside the main class, add an object of the state that contains required fields.

state = {
 isbn: '',
 title: '',
 author: '',
 description: '',
 published_year: '',
 publisher: '',
}

Add a function to update only the state instead of the state and the fields because the fields will fill with the data from GraphQL. In this case, you can't call setState twice to fill the state, so, we fill the state and the fields separately.

updateTextInput = (text, field) => {
 const state = this.state
 state[field] = text;
 this.setState(state);
}

Modify the React Native render section to Apollo GraphQL Query, Mutation, React Native ScrollView, View, TextInput, Button, etc.

render() {
  const { navigation } = this.props;
  const { isbn, title, author, description, published_year, publisher } = this.state;
  return (
   <Query query={GET_BOOK} variables={{ bookId: navigation.getParam('id') }}>
    {({ loading, error, data }) => {
     if (loading) return(<View style={styles.activity}>
       <ActivityIndicator size="large" color="#0000ff" />
      </View>);
     if (error) return(<Text>`Error! ${error.message}`</Text>);
     return (
      <Mutation mutation={UPDATE_BOOK} key={data.book._id} onCompleted={() => navigation.goBack()}>
       {(updateBook, { loading2, error2 }) => (
        <ScrollView style={styles.container}>
         <View style={styles.subContainer}>
          <TextInput
            style={styles.textInput}
            placeholder={'ISBN'}
            defaultValue={data.book.isbn}
            onChangeText={(text) => this.updateTextInput(text, 'isbn')}
          />
         </View>
         <View style={styles.subContainer}>
          <TextInput
            style={styles.textInput}
            placeholder={'Title'}
            defaultValue={data.book.title}
            onChangeText={(text) => this.updateTextInput(text, 'title')}
          />
         </View>
         <View style={styles.subContainer}>
          <TextInput
            style={styles.textInput}
            placeholder={'Author'}
            defaultValue={data.book.author}
            onChangeText={(text) => this.updateTextInput(text, 'author')}
          />
         </View>
         <View style={styles.subContainer}>
          <TextInput
            style={styles.textInput}
            placeholder={'Description'}
            multiline={true}
            numberOfLines={4}
            defaultValue={data.book.description}
            onChangeText={(text) => this.updateTextInput(text, 'description')}
          />
         </View>
         <View style={styles.subContainer}>
          <TextInput
            style={styles.textInput}
            placeholder={'Published Year'}
            defaultValue={data.book.published_year.toString()}
            keyboardType='numeric'
            onChangeText={(text) => this.updateTextInput(text, 'published_year')}
          />
         </View>
         <View style={styles.subContainer}>
          <TextInput
            style={styles.textInput}
            placeholder={'Publisher'}
            defaultValue={data.book.publisher}
            onChangeText={(text) => this.updateTextInput(text, 'publisher')}
          />
         </View>
         <View>
          <Button
           large
           leftIcon={{name: 'save'}}
           title='Save'
           onPress={() => {
            if (this.state.isbn === '')
             this.state.isbn = data.book.isbn;
            if (this.state.title === '')
             this.state.title = data.book.title;
            if (this.state.author === '')
             this.state.author = data.book.author;
            if (this.state.description === '')
             this.state.description = data.book.description;
            if (this.state.publisher === '')
             this.state.publisher = data.book.publisher;
            if (this.state.published_year === '')
             this.state.published_year = data.book.published_year;
            updateBook({
             variables: {
              id: data.book._id,
              isbn: this.state.isbn,
              title: this.state.title,
              author: this.state.author,
              description: this.state.description,
              publisher: this.state.publisher,
              published_year: parseInt(this.state.published_year),
             }
            })
             .then(res => res)
             .catch(err => <Text>{err}</Text>);
           }} />
         </View>
         {loading2 && <View style={styles.activity}>
           <ActivityIndicator size="large" color="#0000ff" />
          </View>}
         {error2 && <Text>`Error! ${error2.message}`</Text>}
        </ScrollView>
       )}
      </Mutation>
     );
    }}
   </Query>
  );
 }
}

Modify the React Native style to match the view requirements.

const styles = StyleSheet.create({
 container: {
  flex: 1,
  padding: 20
 },
 subContainer: {
  flex: 1,
  marginBottom: 20,
  padding: 5,
  borderBottomWidth: 2,
  borderBottomColor: '#CCCCCC',
 },
 activity: {
  position: 'absolute',
  left: 0,
  right: 0,
  top: 0,
  bottom: 0,
  alignItems: 'center',
  justifyContent: 'center'
 },
 textInput: {
  fontSize: 18,
  margin: 5,
 },
})

React Native and Apollo GraphQL Tutorial: Run and Test the Mobile Apps

Before running the React Native apps, we have to download and run the Node.js, Express.js, MongoDB GraphQL server. After download and install the required Node modules, run first the MongoDB server in the different terminal tab.

mongod

Run the Express.js GraphQL server in another terminal tab.

nodemon

In the current React Native app terminal tab, run the React Native app.

yarn start

Open again the Expo app on your iOS or Android device then refresh the current running React Native application. And here they are the full React Native Mobile Apps running with Apollo GraphQL data.

That it's, the React Native and Apollo GraphQL Tutorial: Build Mobile Apps with React Native Elements and Navigation. You can find the full source code from our GitHub.

Front-end Developer Handbook 2019

Front-end Developer Handbook 2019

Sponsored by Frontend Masters, advancing your skills with in-depth, modern front-end engineering courses

Sponsored by Frontend Masters, advancing your skills with in-depth, modern front-end engineering courses

Overview:

This is a guide that everyone can use to learn about the practice of front-end development. It broadly outlines and discusses the practice of front-end engineering: how to learn it and what tools are used when practicing it in 2019.

It is specifically written with the intention of being a professional resource for potential and currently practicing front-end developers to equip themselves with learning materials and development tools. Secondarily, it can be used by managers, CTOs, instructors, and head hunters to gain insights into the practice of front-end development.

The content of the handbook favors web technologies (HTML, CSS, DOM, and JavaScript) and those solutions that are directly built on top of these open technologies. The materials referenced and discussed in the book are either best in class or the current offering to a problem.

The book should not be considered a comprehensive outline of all resources available to a front-end developer. The value of the book is tied up in a terse, focused, and timely curation of just enough categorical information so as not to overwhelm anyone on any one particular subject matter.

The intention is to release an update to the content yearly. This is currently the fourth year an edition has been released.

What is in this Handbook:

Chapter 0 provides a lite recap of the year in front-end development and what may be to come. Chapter 1 & 2 aim to give a brief overview of the discipline and practice of front-end development. Chapters 3 & 4 organize and recommend learning paths and resources. Chapter 5 organizes and list the tools used by front-end developers and Chapter 6 highlights front-end information outlets.

Contribute content, suggestions, and fixes on github:

https://github.com/FrontendMasters/front-end-handbook-2019

Chapter 0. Recap of 2018 and Looking Forward

0.1 — Recap of Front-end Development in 2018

0.2 — In 2019, Expect...

Chapter 1. What Is a Front-end Developer?

This chapter provides a baseline explanation for front-end development and the front-end developer discipline.

Front-end web development, also known as client-side development is the practice of producing HTML, CSS and JavaScript for a website or Web Application so that a user can see and interact with them directly. The challenge associated with front end development is that the tools and techniques used to create the front end of a website change constantly and so the developer needs to constantly be aware of how the field is developing.> The objective of designing a site is to ensure that when the users open up the site they see the information in a format that is easy to read and relevant. This is further complicated by the fact that users now use a large variety of devices with varying screen sizes and resolutions thus forcing the designer to take into consideration these aspects when designing the site. They need to ensure that their site comes up correctly in different browsers (cross-browser), different operating systems (cross-platform) and different devices (cross-device), which requires careful planning on the side of the developer.> https://en.wikipedia.org/wiki/Front-end_web_development
Image source: https://www.upwork.com/hiring/development/front-end-developer/

A Front-end Developer...

A front-end developer architects and develops websites and web applications using web technologies (i.e., HTML, CSS, and JavaScript), which typically runs on the Open Web Platform or acts as compilation input for non-web platform environments (i.e., React Native).

A person enters into the field of front-end development by learning to build a website or web application which relies on HTML, CSS, and JavaScript and commonly runs in a web browser but can also run in a headless browser, WebView, or as compilation input for a native runtime environment. These four run times scenarios are explained below.

Web Browsers (most common)

A web browser is software used to retrieve, present, and traverse information on the WWW. Typically, browsers run on a desktop or laptop computer, tablet, or phone, but as of late a browser can be found on just about anything (i.e, on a fridge, in cars, etc.).

The most common web browsers are (shown in order of most used first):

Headless Browsers

Headless browsers are a web browser without a graphical user interface that can be controlled from a command line interface programmatically for the purpose of web page automation (e.g., functional testing, scraping, unit testing, etc.). Think of headless browsers as a browser that you can run programmatically from the command line that can retrieve and traverse web page code.

The most common headless browsers are:

Webviews

Webviews are used by a native OS, in a native application, to run web pages. Think of a webview like an iframe or a single tab from a web browser that is embedded in a native application running on a device (e.g., iOS, android, windows).

The most common solutions for webview development are:

  • Cordova (typically for native phone/tablet apps)
  • NW.js (typically used for desktop apps)
  • Electron (typically used for desktop apps)

Native from Web Tech

Eventually, what is learned from web browser development can be used by front-end developers to craft code for environments that are not fueled by a browser engine (i.e. web platform). As of late, development environments are being dreamed up that use web technologies (e.g., CSS and JavaScript), without web engines, to create native applications.

Some examples of these environments are:

Notes:

  1. Make sure you are clear what exactly is meant by the "web platform". Read the, ["Open Web Platform"](https://en.wikipedia.org/wiki/Open_Web_Platform ""Open Web Platform"") Wikipedia page. Explore the many technologies that make up the web platform.
Chapter 2. The Practice of Front-end Development: Overview

This chapter will break down and broadly describes the practice of front-end engineering starting with, "How Front-End Developers Are Made".

2.1 - How Front-End Developers Are Made

How exactly does one become a front-end developer? Well, it's complicated. Just consider this road map:

Image source: https://github.com/kamranahmedse/developer-roadmap

Today, in general, one can't go to college and expect to graduate with a degree in front-end engineering. And, I rarely hear of or meet front-end developers who suffered through what is likely a deprecated computer science degree or graphic design degree to end up writing HTML, CSS, and JavaScript professionally. From my perspective, most of the people working on the front-end today generally seem to be self-taught from the ground up or cross over into the front-end space from design or computer science fields.

If you were to set out today to become a front-end developer I would loosely strive to follow the process outlined below (Chapter 3 and Chapter 4 will dive into more details on learning resources).

  1. Learn, roughly, how the web platform works. Make sure you know the "what" and "where" of HTML, CSS, DOM, JavaScript, Domains, DNS, URLs, HTTP, browsers, and servers/hosting. Don't dive deep on anything just yet, just aim to understand the parts at play and how they loosely fit together. Start by building simple web pages.
  2. Learn HTML
  3. Learn CSS
  4. Learn JavaScript
  5. Learn DOM
  6. Learn the fundamentals of user interface design (i.e. UI patterns, interaction design, user experience design, and usability).
  7. Learn CLI/command line
  8. Learn the practice of software engineering (i.e., Application design/architecture, templates, Git, testing, monitoring, automating, code quality, development methodologies).
  9. Get opinionated and customize your tool box with whatever makes sense to your brain (e.g. Webpack, React, and Mobx).
  10. Learn Node.js

A short word of advice on learning. Learn the actual underlying technologies, before learning abstractions. Don't learn jQuery, learn the DOM. Don't learn SASS, learn CSS. Don't learn JSX, learn HTML. Don't learn TypeScript, learn JavaScript. Don't learn Handlebars, learn JavaScript ES6 templates. Don't just use Bootstrap, learn UI patterns.

Lately a lot of non-accredited, expensive, front-end code schools/bootcamps have emerged. These avenues of becoming a front-end developer are typically teacher directed courses, that follow a more traditional style of learning, from an official instructor (i.e., syllabus, test, quizzes, projects, team projects, grades, etc.).

Keep in mind, if you are considering an expensive training program, this is the web! Everything you need to learn is on the web for the taking, costing little to nothing. However, if you need someone to tell you how to take and learn what is low cost to free, and hold you accountable for learning it, you should consider a traditional instructor lead class room setting. Otherwise, I am not aware of any other profession that is practically free for the taking with an internet connection, a couple of dollars a month for screencasting memberships, and a burning desire for knowledge.

For example, if you want to get going today, consuming one or more of the following self-directed resources below can work:

When getting your start, you should fear most things that conceal complexity. Abstractions (e.g. jQuery) in the wrong hands can give the appearance of advanced skills, while all the time hiding the fact that a developer has an inferior understanding of the basics or underlying concepts.

It is assumed that on this journey you are not only learning, but also doing as you learn and investigate tools. Some suggest only doing to learn. While others suggest only learning about doing. I suggest you find a mix of both that matches how your brain works and do that. But, for sure, it is a mix! So, don't just read about it, do it. Learn, do. Learn, do. Repeat indefinitely because things change fast. This is why learning the fundamentals, and not abstractions, are so important.

2.2 - Front-End Job Titles

A great divide has been brewing in the front-end developer space for several years between two very different types of so-called front-end developers. On the one side, you have JavaScript-focused programmers who write JavaScript for front-end runtimes that likely have computer science skills with a software development history. They more than likely view HTML and CSS as an abstraction (i.e. JSX and CSS in JS). On the other side, you have, most likely, non-computer science educated developers who focus on HTML, CSS, and JavaScript as it specifically pertains to the UI. In 2019, when entering or trying to understand the front-end developer space you will absolutely feel this divide. The term front-end developer is on the verge of meaninglessness without clarifying words to address what type of front-end developer is being discussed.

Below is a list and description of various front-end job titles (Keep in mind titles are hard). The common, or most used (i.e., generic), title for a front-end developer is, "front-end developer" or "front-end engineer". Note that any job that contains the word "front-end", "client-side", "web UI", "HTML", "CSS", or "JavaScript" typically infers that a person has some degree of HTML, CSS, DOM, and JavaScript professional know how.

Front-End Developer: The generic job title that describes a developer who is skilled to some degree at HTML, CSS, DOM, and JavaScript and implementing these technologies on the web platform.

Front-End Engineer (aka JavaScript Developer or Full-stack JavaScript Developer): The job title given to a developer who comes from a computer science, engineering, background and is using these skills to work with front-end technologies. This role typically requires computer science knowledge and years of software development experience. When the word "JavaScript Application" is included in the job title, this will denote that the developer should be an advanced JavaScript developer possessing advanced programming, software development, and application development skills (i.e has years of experience building front-end software applications).

CSS/HTML Developer: The front-end job title that describes a developer who is skilled at HTML and CSS, excluding JavaScript and application, know how.

Front-End Web Designer: When the word "Designer" is included in the job title, this will denote that the designer will possess front-end skills (i.e., HTML & CSS) but also professional design (Visual Design and Interaction Design) skills.

UI (User Interface) Developer/Engineer: When the word "Interface" or "UI" is included in the job title, this will denote that the developer should posses interaction design skills in addition to front-end developer skills or front-end engineering skills.

Mobile/Tablet Front-End Developer: When the word "Mobile" or "Tablet" is included in the job title, this will denote that the developer has experience developing front-ends that run on mobile or tablet devices (either natively or on the web platform, i.e., in a browser).

Front-End SEO Expert: When the word "SEO" is included in the job title, this will denote that the developer has extensive experience crafting front-end technologies towards an SEO strategy.

Front-End Accessibility Expert: When the word "Accessibility" is included in the job title, this will denote that the developer has extensive experience crafting front-end technologies that support accessibility requirements and standards.

Front-End Dev. Ops: When the word "DevOps" is included in the job title, this will denote that the developer has extensive experience with software development practices pertaining to collaboration, integration, deployment, automation, and quality.

Front-End Testing/QA: When the word "Testing" or "QA" is included in the job title, this will denote that the developer has extensive experience testing and managing software that involves unit testing, functional testing, user testing, and A/B testing.

Notes:

  1. If you come across the "Full Stack" or the generic "Web Developer" terms in job titles these words may be used by an employer to describe a role that is responsible for all aspects of web/app development, i.e., both front-end (potentially including design) and back-end.

2.3 - Baseline Web Technologies Employed by Front-End Developers

The following core web technologies are employed by front-end developers (consider learning them in this order):

  1. Hyper Text Markup Language (aka HTML)
  2. Cascading Style Sheets (aka CSS)
  3. Uniform Resource Locators (aka URLs)
  4. Hypertext Transfer Protocol (aka HTTP)
  5. JavaScript Programming Language (aka ECMAScript 262)
  6. JavaScript Object Notation (aka JSON)
  7. Document Object Model (aka DOM)
  8. Web APIs (aka HTML5 and friends or Browser APIs)
  9. Web Content Accessibility Guidelines (aka WCAG) & Accessible Rich Internet Applications (aka ARIA)

For a comprehensive list of all web related specifications have a look at platform.html5.org or MDN Web APIs.

The nine technologies just mentioned are defined below along with a link to the relevant documentation and specification for each technology.

Hyper Text Markup Language (aka HTML)

HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages. Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.> — Wikipedia
Most relevant specifications / documentation:

Cascading Style Sheets (aka CSS)

Cascading Style Sheets (CSS) is a style sheet language used for describing the look and formatting of a document written in a markup language. Although most often used to change the style of web pages and user interfaces written in HTML and XHTML, the language can be applied to any kind of XML document, including plain XML, SVG and XUL. Along with HTML and JavaScript, CSS is a cornerstone technology used by most websites to create visually engaging webpages, user interfaces for web applications, and user interfaces for many mobile applications.> — Wikipedia
Most relevant specifications / documentation:

Hypertext Transfer Protocol (aka HTTP)

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web.> — Wikipedia
Most relevant specifications:

Uniform Resource Locators (aka URL)

A uniform resource locator (URL) (also called a web address) is a reference to a resource that specifies the location of the resource on a computer network and a mechanism for retrieving it. A URL is a specific type of uniform resource identifier (URI), although many people use the two terms interchangeably. A URL implies the means to access an indicated resource, which is not true of every URI. URLs occur most commonly to reference web pages (http), but are also used for file transfer (ftp), email (mailto), database access (JDBC), and many other applications.> — Wikipedia
Most relevant specifications:

Document Object Model (aka DOM)

The Document Object Model (DOM) is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML, and XML documents. The nodes of every document are organized in a tree structure, called the DOM tree. Objects in the DOM tree may be addressed and manipulated by using methods on the objects. The public interface of a DOM is specified in its application programming interface (API).> — Wikipedia
Most relevant specifications / documentation:

JavaScript Programming Language (aka ECMAScript 262)

JavaScript is a high level, dynamic, untyped, and interpreted programming language. It has been standardized in the ECMAScript language specification. Alongside HTML and CSS, it is one of the three essential technologies of World Wide Web content production; the majority of websites employ it and it is supported by all modern web browsers without plug-ins. JavaScript is prototype-based with first-class functions, making it a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles. It has an API for working with text, arrays, dates and regular expressions, but does not include any I/O, such as networking, storage or graphics facilities, relying for these upon the host environment in which it is embedded.> — Wikipedia
Most relevant specifications / documentation:

Web APIs (aka HTML5 and friends)

When writing code for the Web using JavaScript, there are a great many APIs available. Below is a list of all the interfaces (that is, types of objects) that you may be able to use while developing your Web app or site.> — Mozilla
Most relevant documentation:

JavaScript Object Notation (aka JSON)

It is the primary data format used for asynchronous browser/server communication (AJAJ), largely replacing XML (used by AJAX). Although originally derived from the JavaScript scripting language, JSON is a language-independent data format. Code for parsing and generating JSON data is readily available in many programming languages. The JSON format was originally specified by Douglas Crockford. It is currently described by two competing standards, RFC 7159 and ECMA-404. The ECMA standard is minimal, describing only the allowed grammar syntax, whereas the RFC also provides some semantic and security considerations. The official Internet media type for JSON is application/json. The JSON filename extension is .json.> — Wikipedia
Most relevant specifications:

Web Content Accessibility Guidelines (aka WCAG) & Accessible Rich Internet Applications (aka ARIA)

Accessibility refers to the design of products, devices, services, or environments for people with disabilities. The concept of accessible design ensures both “direct access” (i.e., unassisted) and "indirect access" meaning compatibility with a person's assistive technology (for example, computer screen readers).> — Wikipedia* Web Accessibility Initiative (WAI)

2.4 - Potential Front-end Developer Skills

Image source: http://blog.naustud.io/2015/06/baseline-for-modern-front-end-developers.html

A basic to advanced understanding of HTML, CSS, DOM, JavaScript, HTTP/URL, and web browsers is assumed for any type of professional front-end developer role.

Beyond the skills just mentioned, a front-end developer might also be specifically skilled in one or more of the following:

  • Content Management Systems (aka CMS)
  • Node.js
  • Cross-Browser Testing
  • Cross-Platform Testing
  • Unit Testing
  • Cross-Device Testing
  • Accessibility / WAI-ARIA
  • Search Engine Optimization (aka SEO)
  • Interaction or User Interface Design
  • User Experience
  • Usability
  • E-commerce Systems
  • Portal Systems
  • Wireframing
  • CSS Layout / Grids
  • DOM Manipulation (e.g., jQuery)
  • Mobile Web Performance
  • Load Testing
  • Performance Testing
  • Progressive Enhancement / Graceful Degradation
  • Version Control (e.g., GIT)
  • MVC / MVVM / MV*
  • Functional Programming
  • Data Formats (e.g., JSON, XML)
  • Data APIs (e.g Restful API)
  • Web Font Embedding
  • Scalable Vector Graphics (aka SVG)
  • Regular Expressions
  • Microdata / Microformats
  • Task Runners, Build Tools, Process Automation Tools
  • Responsive Web Design
  • Object-Oriented Programming
  • Application Architecture
  • Modules
  • Dependency Managers
  • Package Managers
  • JavaScript Animation
  • CSS Animation
  • Charts / Graphs
  • UI Widgets
  • Code Quality Testing
  • Code Coverage Testing
  • Code Complexity Analysis
  • Integration Testing
  • Command Line / CLI
  • Templating Strategies
  • Templating Engines
  • Single Page Applications
  • Web/Browser Security
  • Browser Developer Tools

2.5 - Front-End Developers Develop For...

A front-end developer crafts HTML, CSS, and JS that typically runs on the web platform (e.g. a web browser) delivered from one of the following operating systems (aka OSs):

These operating systems typically run on one or more of the following devices:

  • Desktop computer
  • Laptop / netbook computer
  • Mobile phone
  • Tablet
  • TV
  • Watch
  • Things (i.e., anything you can imagine, car, refrigerator, lights, thermostat, etc.)

Image source: https://www.enterpriseirregulars.com/104084/roundup-internet-things-forecasts-market-estimates-2015/

Generally speaking, front-end technologies can run on the aforementioned operating systems and devices using the following run time web platform scenarios:

2.6 - Front-End on a Team

A front-end developer is typically only one player on a team that designs and develops web sites, web applications, or native applications running from web technologies.

A bare-bones development team for building professional web sites or software for the web platform will typically, minimally, contain the following roles.

  • Visual Designer (i.e., fonts, colors, spacing, emotion, visuals concepts & themes)
  • UI/Interaction Designer/Information Architect (i.e., wireframes, specifying all user interactions and UI functionality, structuring information)
  • Front-End Developer (i.e., writes code that runs in client/on the device)
  • Back-End Developer (i.e., writes code that runs on the server)

The roles are ordered according to overlapping skills. A front-end developer will typically have a good handle on UI/Interaction design as well as back-end development. It is not uncommon for team members to fill more than one role by taking on the responsibilities of an over-lapping role.

It is assumed that the team mentioned above is being directed by a project lead or some kind of product owner (i.e., stakeholder, project manager, project lead, etc.)

A larger web team might include the following roles not shown above:

  • SEO Strategists
  • DevOps Engineers
  • Performance Engineers
  • API Developers
  • Database Administrators
  • QA Engineers / Testers

2.7 - Generalist/Full-Stack Myth

The term "Full-Stack" developer has come to take on several meanings. So many, that not one meaning is clear when the term is used. Just consider the results from the two surveys shown below. These results might lead one to believe that being a full-stack developer is commonplace. But, in my almost 20 years of experience, this is anything but the case in a professional context.

Image source: https://medium.freecodecamp.com/we-asked-15-000-people-who-they-are-and-how-theyre-learning-to-code-4104e29b2781#.ngcpn8nlz

Image source: https://insights.stackoverflow.com/survey/2017#developer-profile-specific-developer-types

The roles to design and develop a website or web application require a deep set of skills and vast experience in the area of visual design, UI/interaction design, front-end development, and back-end development. Any person who can fill one or more of these 4 roles at a professional level is an extremely rare commodity.

Pragmatically, you should seek to be, or seek to hire, an expert in one of these roles (i.e. Visual Design, Interaction Design/IA, Front-end Dev, Back-end Dev). Those who claim to operate at an expert level at one or more of these roles are exceptionally rare.

However, given that JavaScript has infiltrated all layers of a technology stack (i.e. Node.js) finding a full-stack JS developer who can code the front-end and back-end is becoming less mythical. Typically, these full-stack developers only deal with JavaScript. A developer who can code the front-end, back-end, API, and database isn't as absurd as it once was (excluding visual design, interaction design, and CSS). Still mythical in my opinion, but not as uncommon as it once was. Thus, I wouldn't recommend a developer set out to become a "full-stack" developer. In rare situations, it can work. But, as a general concept for building a career as a front-end developer, I'd focus on front-end technologies.

2.8 - Front-End Interviews

Preparing:

Quiz's:

Questions you may get asked:

Questions you ask:

2.9 - Front-End Job Boards

A plethora of technical job listing outlets exist. The narrowed list below are currently the most relevant resources for finding a specific front-end position/career.

Notes:

  1. Want to work remotely as a front-end developer checkout these remote-friendly companies.

2.10 - Front-End Salaries

The national average in the U.S for a mid-level front-end developer is somewhere between $65k and 100k.

Of course when you first start expect to enter the field at around 40k depending upon location and experience.

Notes:

  1. A lead/senior front-end developer/engineer can potentially live wherever they want (i.e., work remotely) and make over A lead/senior front-end developer/engineer can potentially live wherever they want (i.e., work remotely) and make over $150k a year (visit angel.co, sign-up, review front-end jobs over $150k or examine the salary ranges on Stack Overflow Jobs).50k a year (visit angel.co, sign-up, review front-end jobs over A lead/senior front-end developer/engineer can potentially live wherever they want (i.e., work remotely) and make over $150k a year (visit angel.co, sign-up, review front-end jobs over $150k or examine the salary ranges on Stack Overflow Jobs).50k or examine the salary ranges on Stack Overflow Jobs).
Chapter 3. Learning Front-end Dev: Self Directed Resources/Recommendations

This chapter highlights the many resources (video training, books, etc.) that an individual can use to direct their own learning process and career as a front-end developer.

The learning resources identified (articles, books, videos, screencasts etc..) will include both free and paid material. Paid material will be indicated with [$].

3.1. - Learn Internet/Web

The Internet is a global system of interconnected computer networks that use the Internet protocol suite (TCP/IP) to link several billion devices worldwide. It is a network of networks that consists of millions of private, public, academic, business, and government networks of local to global scope, linked by a broad array of electronic, wireless, and optical networking technologies. The Internet carries an extensive range of information resources and services, such as the inter-linked hypertext documents and applications of the World Wide Web (WWW), electronic mail, telephony, and peer-to-peer networks for file sharing.> — Wikipedia

Image source: https://www.helloitsliam.com/2014/12/20/how-the-internet-works-infographic/

Image source: http://www.bitrebels.com/technology/find-out-who-runs-the-internet-chart/

3.2. - Learn Web Browsers

A web browser (commonly referred to as a browser) is a software application for retrieving, presenting, and traversing information resources on the World Wide Web. An information resource is identified by a Uniform Resource Identifier (URI/URL) and may be a web page, image, video or other piece of content. Hyperlinks present in resources enable users easily to navigate their browsers to related resources. Although browsers are primarily intended to use the World Wide Web, they can also be used to access information provided by web servers in private networks or files in file systems.> — Wikipedia#### The most commonly used browsers (on desktop and mobile) are:

  1. Chrome (engine: Blink + V8)
  2. Firefox (engine: Gecko + SpiderMonkey)
  3. Internet Explorer (engine: Trident + Chakra)
  4. Safari (engine: Webkit + SquirrelFish)

Image source: http://gs.statcounter.com/browser-market-share

Evolution of Browsers & Web Technologies (i.e., APIs)

The Most Commonly Used Headless Browser Are:

How Browsers Work

Optimizing for Browsers:

Comparing Browsers

Browser Hacks

Developing for Browsers

In the past, front-end developers spent a lot of time making code work in several different browsers. This was once a bigger issue than it is today. Today, abstractions (e.g., React, Webpack, Post-CSS, Babel etc...) combined with modern browsers make browser development fairly easy. The new challenge is not which browser the user will use, but on which device they will run the browser.

Evergreen Browsers

The latest versions of most modern browsers are considered evergreen browsers. That is, in theory, they are supposed to automatically update themselves silently without prompting the user. This move towards self-updating browsers has been in reaction to the slow process of eliminating older browsers that do not auto-update.

Picking a Browser

As of today, most front-end developers use Chrome and "Chrome Dev Tools" to develop front-end code. However, the most used modern browsers all offer a flavor of developer tools. Picking one to use for development is a subjective choice. The more important issue is knowing which browsers, on which devices, you have to support and then testing appropriately.

3.3 - Learn Domain Name System (aka DNS)

The Domain Name System (DNS) is a hierarchical distributed naming system for computers, services, or any resource connected to the Internet or a private network. It associates various information with domain names assigned to each of the participating entities. Most prominently, it translates domain names, which can be easily memorized by humans, to the numerical IP addresses needed for the purpose of computer services and devices worldwide. The Domain Name System is an essential component of the functionality of most Internet services because it is the Internet's primary directory service.> — Wikipedia

Image source: http://www.digital-digest.com/blog/DVDGuy/wp-content/uploads/2011/11/how_dns_works.jpg

3.4 - Learn HTTP/Networks (Including CORS & WebSockets)

HTTP - The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web.> — Wikipedia#### HTTP Specifications

HTTP Docs

HTTP Videos/Articles/Tutorials

HTTP Status Codes

CORS - Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources (e.g., fonts) on a web page to be requested from another domain outside the domain from which the resource originated.> — Wikipedia#### CORS Specifications

CORS

WebSockets - WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C.> — Wikipedia#### WebSockets

3.5 - Learn Web Hosting

A web hosting service is a type of Internet hosting service that allows individuals and organizations to make their website accessible via the World Wide Web. Web hosts are companies that provide space on a server owned or leased for use by clients, as well as providing Internet connectivity, typically in a data center.> — Wikipedia#### General Learning:

Image source: https://firstsiteguide.com/wp-content/uploads/2016/06/what-is-web-hosting-infographic.jpg

3.6 - Learn General Front-End Development

3.7 - Learn User Interface/Interaction Design

User Interface Design - User interface design (UI) or user interface engineering is the design of user interfaces for machines and software, such as computers, home appliances, mobile devices, and other electronic devices, with the focus on maximizing the user experience. The goal of user interface design is to make the user's interaction as simple and efficient as possible, in terms of accomplishing user goals (user-centered design).> — Wikipedia> Interaction Design Pattern - A design pattern is a formal way of documenting a solution to a common design problem. The idea was introduced by the architect Christopher Alexander for use in urban planning and building architecture, and has been adapted for various other disciplines, including teaching and pedagogy, development organization and process, and software architecture and design.> — Wikipedia> User Experience Design - User Experience Design (UXD or UED or XD) is the process of enhancing user satisfaction by improving the usability, accessibility, and pleasure provided in the interaction between the user and the product. User experience design encompasses traditional human–computer interaction (HCI) design, and extends it by addressing all aspects of a product or service as perceived by users.> — Wikipedia> Human–Computer Interaction - Human–computer interaction (HCI) researches the design and use of computer technology, focusing particularly on the interfaces between people (users) and computers. Researchers in the field of HCI both observe the ways in which humans interact with computers and design technologies that lets humans interact with computers in novel ways.> — Wikipedia
Minimally I'd suggest reading the following canonical texts on the matter so one can support and potential build usable user interfaces.

3.8 - Learn HTML & CSS

HTML - HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages. Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.> — Wikipedia> CSS - Cascading Style Sheets (CSS) is a style sheet language used for describing the look and formatting of a document written in a markup language. Although most often used to change the style of web pages and user interfaces written in HTML and XHTML, the language can be applied to any kind of XML document, including plain XML, SVG and XUL. Along with HTML and JavaScript, CSS is a cornerstone technology used by most websites to create visually engaging webpages, user interfaces for web applications, and user interfaces for many mobile applications.> — Wikipedia
Liken to constructing a house, one might consider HTML the framing and CSS to be the painting & decorating.

General Learning:

Mastering CSS:

References/Docs:

Glossary/Vocabulary:

Standards/Specifications:

Architecting CSS:

Authoring/Architecting Conventions:

3.9 - Learn Search Engine Optimization

Search engine optimization (SEO) is the process of affecting the visibility of a website or a web page in a search engine's unpaid results — often referred to as "natural," "organic," or "earned" results. In general, the earlier (or higher ranked on the search results page), and more frequently a site appears in the search results list, the more visitors it will receive from the search engine's users. SEO may target different kinds of search, including image search, local search, video search, academic search, news search and industry-specific vertical search engines.> — Wikipedia

Image source: https://visual.ly/community/infographic/computers/how-does-seo-work

General Learning:

3.10 - Learn JavaScript

JavaScript is a high level, dynamic, untyped, and interpreted programming language. It has been standardized in the ECMAScript language specification. Alongside HTML and CSS, it is one of the three essential technologies of World Wide Web content production; the majority of websites employ it and it is supported by all modern web browsers without plug-ins. JavaScript is prototype-based with first-class functions, making it a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles. It has an API for working with text, arrays, dates and regular expressions, but does not include any I/O, such as networking, storage or graphics facilities, relying for these upon the host environment in which it is embedded.> — Wikipedia#### Getting Started:

General Learning:

Mastering:

Functional JavaScript:

References/Docs:

Glossary/Encyclopedia/Jargon:

Standards/Specifications:

Style:

Deprecated JS Learning Resources:

JS Explorers/Visualizers:

3.11 - Learn DOM, BOM, CSSOM & jQuery

DOM - The Document Object Model (DOM) is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML, and XML documents. The nodes of every document are organized in a tree structure, called the DOM tree. Objects in the DOM tree may be addressed and manipulated by using methods on the objects. The public interface of a DOM is specified in its application programming interface (API).> — Wikipedia> BOM - The Browser Object Model (BOM) is a browser-specific convention referring to all the objects exposed by the web browser. Unlike the Document Object Model, there is no standard for implementation and no strict definition, so browser vendors are free to implement the BOM in any way they wish.> — Wikipedia> jQuery - jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. jQuery is the most popular JavaScript library in use today, with installation on 65% of the top 10 million highest-trafficked sites on the Web. jQuery is free, open-source software licensed under the MIT License.> — Wikipedia
The ideal path, but certainly the most difficult, would be to first learn JavaScript, then the DOM, then jQuery. However, do what makes sense to your brain. Most front-end developers learn about JavaScript and then DOM by way of first learning jQuery. Whatever path you take, just make sure JavaScript, the DOM, and jQuery don't become a black box.

General Learning:

Mastering:

References/Docs:

Standards/Specifications:

3.12 - Learn Web Animation

General Learning:

Standards/Specifications:

3.13 - Learn Web Fonts, Icons, & Images

Web typography refers to the use of fonts on the World Wide Web. When HTML was first created, font faces and styles were controlled exclusively by the settings of each Web browser. There was no mechanism for individual Web pages to control font display until Netscape introduced the <font> tag in 1995, which was then standardized in the HTML 3.2 specification. However, the font specified by the tag had to be installed on the user's computer or a fallback font, such as a browser's default sans-serif or monospace font, would be used. The first Cascading Style Sheets specification was published in 1996 and provided the same capabilities.> The CSS2 specification was released in 1998 and attempted to improve the font selection process by adding font matching, synthesis and download. These techniques did not gain much use, and were removed in the CSS2.1 specification. However, Internet Explorer added support for the font downloading feature in version 4.0, released in 1997. Font downloading was later included in the CSS3 fonts module, and has since been implemented in Safari 3.1, Opera 10 and Mozilla Firefox 3.5. This has subsequently increased interest in Web typography, as well as the usage of font downloading.> — Wikipedia#### Fonts:

Icons:

Images:

3.14 - Learn Accessibility

Accessibility refers to the design of products, devices, services, or environments for people with disabilities. The concept of accessible design ensures both “direct access” (i.e., unassisted) and "indirect access" meaning compatibility with a person's assistive technology (for example, computer screen readers).> Accessibility can be viewed as the "ability to access" and benefit from some system or entity. The concept focuses on enabling access for people with disabilities, or special needs, or enabling access through the use of assistive technology; however, research and development in accessibility brings benefits to everyone.> Accessibility is not to be confused with usability, which is the extent to which a product (such as a device, service, or environment) can be used by specified users to achieve specified goals with effectiveness, efficiency and satisfaction in a specified context of use.> Accessibility is strongly related to universal design which is the process of creating products that are usable by people with the widest possible range of abilities, operating within the widest possible range of situations. This is about making things accessible to all people (whether they have a disability or not).> — Wikipedia#### General Learning:

Standards/Specifications:

3.15 - Learn Web/Browser APIs

Image source: http://www.evolutionoftheweb.com/

The BOM (Browser Object Model) and the DOM (Document Object Model) are not the only browser APIs that are made available on the web platform inside of browsers. Everything that is not specifically the DOM or BOM, but an interface for programming the browser could be considered a web or browser API (tragically in the past some of these APIs have been called HTML5 APIs which confuses their own specifics/standardize with the actual HTML5 specification specifying the HTML5 markup language). Note that web or browser APIs do include device APIs (e.g., <a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery" target="_blank">Navigator.getBattery()</a>) that are available through the browser on tablet and phones devices.

You should be aware of and learn, where appropriate, web/browser APIs. A good tool to use to familiarize oneself with all of these APIs would be to investigate the HTML5test.com results for the 5 most current browsers.

MDN has a great deal of information about web/browser APIs.

Keep in mind that not every API is specified by the W3C or WHATWG.

In addition to MDN, you might find the following resources helpful for learning about all the web/browser API's:

3.16 - Learn JSON (JavaScript Object Notation)

JSON, (canonically pronounced sometimes JavaScript Object Notation), is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is the primary data format used for asynchronous browser/server communication (AJAJ), largely replacing XML (used by AJAX).> Although originally derived from the JavaScript scripting language, JSON is a language-independent data format. Code for parsing and generating JSON data is readily available in many programming languages.> The JSON format was originally specified by Douglas Crockford. It is currently described by two competing standards, RFC 7159 and ECMA-404. The ECMA standard is minimal, describing only the allowed grammar syntax, whereas the RFC also provides some semantic and security considerations. The official Internet media type for JSON is application/json. The JSON filename extension is .json.> — Wikipedia#### General Learning:

References/Docs:

Standards/Specifications:

Architecting:

3.17 - Learn JS Templates

A JavaScript template is typically used, but not always with a MV* solution to separate parts of the view (i.e., the UI) from the logic and model (i.e., the data or JSON).

Note that JavaScript 2015 (aka ES6) added a native templating mechanism called ["Templates strings"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings ""Templates strings""). Additionally, templating as of late has been replaced by things like JSX, a template element, or HTML strings.

If I was not using React & JSX I'd first reach for JavaScript ["Templates strings"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings ""Templates strings"") and when that was lacking move to nunjucks.

3.18 - Learn Static Site Generators

Static site generators, typically written using server side code (i.e., ruby, php, python, nodeJS, etc.), produce static HTML files from static text/data + templates that are intended to be sent from a server to the client statically without a dynamic nature.

General Learning:

3.19 - Learn Computer Science via JS

3.20 - Learn Front-End Application Architecture

General Learning:

Deprecated Learning Materials:

Not a lot of general content is being created on this topic as of late. Most of the content offered for learning how to build front-end/SPA/JavaScript applications presupposes you've decided up a tool like Angular, Ember, React, or Aurelia.

My advice, in 2019 learn React and Mobx and Apollo/graphql.

3.21 - Learn Data (i.e. JSON) API Design

3.22 - Learn React

Learning React:

Mastering React:

Once you have a good handle on React move on to learning a more robust state management solution like MobX. If you are an experienced developer with Functional Programming knowledge look at Redux. If you need help understanding the role of state management beyond React's setState watch, "Advanced State Management in React (feat. Redux and MobX)".

3.23 - Learn Application State Management

3.24 - Learn Progressive Web App

Unlike traditional applications, progressive web apps are a hybrid of regular web pages (or websites) and a mobile application. This new application model attempts to combine features offered by most modern browsers with the benefits of mobile experience.> In 2015, designer Frances Berriman and Google Chrome engineer Alex Russell coined the term "Progressive Web Apps" to describe apps taking advantage of new features supported by modern browsers, including Service Workers and Web App Manifests, that let users upgrade web apps to be first-class applications in their native OS.> According to Google Developers, these characteristics are:* Progressive - Work for every user, regardless of browser choice because they’re built with progressive enhancement as a core tenet.

  • Responsive - Fit any form factor: desktop, mobile, tablet, or forms yet to emerge.
  • Connectivity independent - Service workers allow work offline, or on low quality networks.
  • App-like - Feel like an app to the user with app-style interactions and navigation.
  • Fresh - Always up-to-date thanks to the service worker update process.
  • Safe - Served via HTTPS to prevent snooping and ensure content hasn’t been tampered with.
  • Discoverable - Are identifiable as “applications” thanks to W3C manifests[6] and service worker registration scope allowing search engines to find them.
  • Re-engageable - Make re-engagement easy through features like push notifications.
  • Installable - Allow users to “keep” apps they find most useful on their home screen without the hassle of an app store.
  • Linkable - Easily shared via a URL and do not require complex installation.

Wikipedia* A Beginner’s Guide To Progressive Web Apps [read]

3.25 - Learn JS API Design

3.26 - Learn Browser Web Developer Tools

Web development tools allow web developers to test and debug their code. They are different from website builders and IDEs in that they do not assist in the direct creation of a webpage, rather they are tools used for testing the user facing interface of a website or web application.> Web development tools come as browser add-ons or built in features in web browsers. The most popular web browsers today like, Google Chrome, Firefox, Opera, Internet Explorer, and Safari have built in tools to help web developers, and many additional add-ons can be found in their respective plugin download centers.> Web development tools allow developers to work with a variety of web technologies, including HTML, CSS, the DOM, JavaScript, and other components that are handled by the web browser. Due to the increasing demand from web browsers to do more popular web browsers have included more features geared for developers.> — Wikipedia
While most browsers come equipped with web developer tools, the Chrome developer tools are currently the most talked about and widely used.

I'd suggest learning and using the Chrome web developer tools, simply because the best resources for learning web developer tools revolves around Chrome DevTools.

Learn Chrome Web Developer Tools:

Chrome Web Developer Tools Docs:

3.27 - Learn the Command Line (aka CLI)

A command-line interface or command language interpreter (CLI), also known as command-line user interface, console user interface, and character user interface (CUI), is a means of interacting with a computer program where the user (or client) issues commands to the program in the form of successive lines of text (command lines).> — Wikipedia#### General Learning:

Mastering:

3.28 - Learn Node.js

Node.js is an open-source, cross-platform runtime environment for developing server-side web applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows, Linux, FreeBSD, NonStop, IBM AIX, IBM System z and IBM i. Its work is hosted and supported by the Node.js Foundation, a collaborative project at Linux Foundation.> Node.js provides an event-driven architecture and a non-blocking I/O API designed to optimize an application's throughput and scalability for real-time web applications. It uses Google V8 JavaScript engine to execute code, and a large percentage of the basic modules are written in JavaScript. Node.js contains a built-in library to allow applications to act as a web server without software such as Apache HTTP Server, Nginx or IIS.> — Wikipedia#### General Learning:

3.29 - Learn Modules

General Learning:

References/Docs:

3.30 - Learn Module loaders/bundlers

Webpack:

Rollup:

3.31 - Learn Package Manager

A package manager or package management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing software packages for a computer's operating system in a consistent manner. It typically maintains a database of software dependencies and version information to prevent software mismatches and missing prerequisites.> — Wikipedia#### General Learning:

3.32 - Learn Version Control

A component of software configuration management, version control, also known as revision control or source control, is the management of changes to documents, computer programs, large web sites, and other collections of information. Changes are usually identified by a number or letter code, termed the "revision number," "revision level," or simply "revision." For example, an initial set of files is "revision 1." When the first change is made, the resulting set is "revision 2," and so on. Each revision is associated with a timestamp and the person making the change. Revisions can be compared, restored, and with some types of files, merged.> — Wikipedia
The most common solution used for version control today is Git. Learn it!

General Learning:

Mastering:

References/Docs:

3.33 - Learn Build and Task Automation

Build automation is the process of automating the creation of a software build and the associated processes including: compiling computer source code into binary code, packaging binary code, and running automated tests.> — Wikipedia#### General Learning:

References/Docs:

Gulp is great. However, you might only need npm run. Before turning to additional complexity in your application stack ask yourself if npm run can do the job. If you need more, use Gulp.

Read:

3.34 - Learn Site Performance Optimization

Web performance optimization, WPO, or website optimization is the field of knowledge about increasing the speed in which web pages are downloaded and displayed on the user's web browser. With the average internet speed increasing globally, it is fitting for website administrators and webmasters to consider the time it takes for websites to render for the visitor.> — Wikipedia#### General Learning:

3.35 - Learn Testing

Unit Testing - In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application.> — Wikipedia> Functional Testing - Functional testing is a quality assurance (QA) process and a type of black box testing that bases its test cases on the specifications of the software component under test. Functions are tested by feeding them input and examining the output, and internal program structure is rarely considered (not like in white-box testing). Functional testing usually describes what the system does.> — Wikipedia> Integration Testing - Integration testing (sometimes called integration and testing, abbreviated I&T) is the phase in software testing in which individual software modules are combined and tested as a group. It occurs after unit testing and before validation testing. Integration testing takes as its input modules that have been unit tested, groups them in larger aggregates, applies tests defined in an integration test plan to those aggregates, and delivers as its output the integrated system ready for system testing.> — Wikipedia#### General Learning:

3.36 - Learn Headless Browsers

A headless browser is a web browser without a graphical user interface.> Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but are executed via a command line interface or using network communication. They are particularly useful for testing web pages as they are able to render and understand HTML the same way a browser would, including styling elements such as page layout, color, font selection and execution of JavaScript and AJAX which are usually not available when using other testing methods. Google stated in 2009 that using a headless browser could help their search engine index content from websites that use AJAX.> — Wikipedia* Getting Started with Headless Chrome [readme]

PhantomJS is no longer maintained, Headless Chrome steps in.

3.37 - Learn Offline Development

Offline development (aka offline first) is an area of knowledge and discussion around development practices for devices that are not always connected to the Internet or a power source.

General Learning:

3.38 - Learn Web/Browser/App Security

3.39 - Learn Multi-Device Development

Image source: http://bradfrost.com/blog/post/this-is-the-web/

A website or web application can run on a wide range of computers, laptops, tablets and phones, as well as a handful of new devices (watches, thermostats, fridges, etc.). How you determine what devices you'll support and how you will develop to support those devices is called, "multi-device development strategy". Below, I list the most common multi-device development strategies.

  • Build a responsive (RWD) web site/app for all devices.
  • Build an adaptive/progressively enhanced web site/app for all devices.
  • Build a website, web app, native app, or hybrid-native app for each individual device or a grouping of devices.
  • Attempt to retrofit something you have already built using bits and parts from strategies 1, 2 or 3.

General Learning:

Chapter 4. Learning Front-end Dev: Instructor Directed Resources/Recommendations

This chapter highlights a few options for instructor directed learning via front-end development schools, courses, programs, and bootcamps.

The table below contains a small selection of instructor-led courses (i.e. programs, schools, and bootcamps). Use the table to get a general idea of what is available, the cost, duration, and locations of courses. (Be aware the information can change quickly)

company program price estimate on site remote duration Betamore Front-end Web Development 3,000 Baltimore, MD 10 weeks BLOC Become a Front-end Developer 4,999 yes 16 weeks @ 25hr/wk or 32 weeks @ 10hr/wk General Assembly Front-end Web Development 3,500 multiple locations 3 hrs/day 2 days/wk for 8 weeks Thinkful Front-end Web Development 300 per month yes 15 hrs/wk for 3 months Turing School of Software & Design Front-End Engineering 20,000 Denver, CO 7 months full time Notes:

  1. For a complete list of schools, courses, programs, and bootcamps to evaluate have a look at switchup.org or coursereport.com.

If you can't afford a directed education (can be very expensive), a self directed education using screencasts, books, and articles is a viable alternative to learn front-end development for the self-driven individual.

Chapter 5. Front-end Dev Tools

This chapter identifies the tools of the trade. Make sure you understanding the category that a set of tools falls within, before studying the tools themselves. Note that just because a tool is listed, or a category of tools is documented, this does not equate to an assertion on my part that a front-end developer should learn it and use it. Choose your own toolbox. I'm just providing the common toolbox options.

5.1 - Doc/API Browsing Tools

Tools to browse common developer documents and developer API references.

Cheatsheets:

5.2 - SEO Tools

General SEO Tools:

Tools for Finding SEO Tools:

5.3 - Prototyping & Wireframing Tools

Creating:

Collaboration / Presenting:

5.4 - Diagramming Tools

5.5 - HTTP/Network Tools

5.6 - Code Editing Tools

A source code editor is a text editor program designed specifically for editing source code of computer programs by programmers. It may be a standalone application or it may be built into an integrated development environment (IDE) or web browser. Source code editors are the most fundamental programming tool, as the fundamental job of programmers is to write and edit source code.> — Wikipedia
Front-end code can minimally be edited with a simple text editing application like Notepad or TextEdit. But, most front-end practitioners use a code editor specifically design for editing a programming language.

Code editors come in all sorts of types and size, so to speak. Selecting one is a rather subjective engagement. Choose one, learn it inside and out, then get on to learning HTML, CSS, DOM, and JavaScript.

However, I do strongly believe, minimally, a code editor should have the following qualities (by default or by way of plugins):

  1. Good documentation on how to use the editor
  2. Report (i.e., hinting/linting/errors) on the code quality of HTML, CSS, and JavaScript.
  3. Offer syntax highlighting for HTML, CSS, and JavaScript.
  4. Offer code completion for HTML, CSS, and JavaScript.
  5. Be customizable by way of a plug-in architecture
  6. Have available a large repository of third-party/community plug-ins that can be used to customize the editor to your liking
  7. Be small, simple, and not coupled to the code (i.e., not required to edit the code)

Code Editors:

Online Code Editors:

Shareable & Runnable Simple Code Editors:

Used to share limited amounts of immediately runnable code. Not a true code editor but a tool that can be used to share small amounts of immediately runnable code in a web browser.

I recommending using Visual Studio Code because of the quality of the tool and the continuous improvements made to the editor that likely won't stop or slow due to the fact that Microsoft is behind the tool. It is widely used:

Image source: https://2018.stateofjs.com/other-tools/text_editors

5.7 - Browser Tools

JS Utilities to fix Browsers:

General Reference Tools to Determine If X Browser Supports X:

Browser Development/Debug Tools:

Chrome Developer Tools (aka DevTools)Per-Panel DocumentationCommand Line API ReferenceKeyboard & UI Shortcuts ReferenceSettings* Firefox Developer Tools

JavaScript Utilities to Determine If X Browser Supports X:

Broad Browser Polyfills/Shims:

Hosted Testing/Automation for Browsers:

Headless Browsers:

Browser Automation:

Used for functional testing and monkey testing.

Browser Hacks:

Browser Syncing Tools:

Browser List:

Share target browsers between different front-end tools, like Autoprefixer, Stylelint and babel-preset-env.
Browserslisthttp://browserl.ist/### 5.8 - HTML Tools

HTML Templates/Boilerplates/Starter Kits:

HTML Polyfill:

Transpiling:

References:

Linting/Hinting:

Optimizer:

Online Creation/Generation/Experimentation Tools:

Authoring Conventions:

Workflow:

HTML Outliner:

Trending HTML Repositories on GitHub This Month:

https://github.com/trending?l=html&since=monthly

5.9 - CSS Tools

CSS Utilities:

CSS Frameworks (utilities + UI):

Mobile Only CSS Frameworks:

CSS Reset:

A CSS Reset (or “Reset CSS”) is a short, often compressed (minified) set of CSS rules that resets the styling of all HTML elements to a consistent baseline.> — cssreset.com* Eric Meyer's “Reset CSS” 2.0

Transpiling:

References:

Linting/Hinting:

Code Formatter/Beautifier:

Optimizer:

Online Creation/Generation/Experimentation Tools:

Architecting CSS:

Authoring/Architecting Conventions:

Style Guide Resources:

CSS in JS:

Trending CSS Repositories on GitHub This Month:

https://github.com/trending?l=css&since=monthly

5.10 - DOM Tools

DOM Libraries/Frameworks:

DOM Utilities:

DOM Event Tools:

DOM Performance Tools:

References:

DOM Polyfills/Shims:

Virtual DOM:

5.11 - JavaScript Tools

JS Utilities:

Transforming JavaScript Objects Tool:

Transpiling / Type Checking (ES to ES):

Type Checking (ES to ES):

Transpiling (ES to ES):

Code-analysis Engine:

Linting/Hinting & Style Linter:

Unit Testing:

Testing Assertions for Unit Testing:

Test Spies, Stubs, and Mocks for Unit Testing:

Code Formater/Beautifier:

Performance Testing:

Visualization, Static Analysis, Complexity, Coverage Tools:

Optimizer:

Obfuscate:

Sharable/Runnable Code Editors:

Online Regular Expression Editors/Visual Tools:

Authoring Convention Tools:

Trending JS Repositories on GitHub This Month:

https://github.com/trending?l=javascript&since=monthly

Most Depended upon Packages on NPM:

https://www.npmjs.com/browse/depended

5.12 - Headless CMS Tools

Site Generator Listings:

5.13 - Static Site Generators Tools

Site Generator Listings:

5.14 - Accessibility Tools

Guides

Site Scanners

Color Contrast Testers

Low-Vision Simulators

Screen Readers

Readability Testers

Articles

5.15 - App Frameworks (Desktop, Mobile, Tablet, etc.) Tools

Front-End App Frameworks:

Native Hybrid Mobile WebView (i.e., Browser Engine Driven) Frameworks:

These solutions typically use Cordova, crosswalk, or a custom WebView as a bridge to native APIs.

Native Hybrid Mobile Development Webview (i.e., Browser Engine Driven) Environments/Platforms/Tools:

These solutions typically use Cordova, crosswalk, or a custom WebView as a bridge to native APIs.

Native Desktop App Frameworks:

Native Mobile App Frameworks (Aka JavaScript Native Apps)

These solutions use a JS engine at runtime to interpret JS and bridge that to native APIs. No browser engine or WebView is used. The UI is constructed from native UI components.

References & demo apps:

Performance:

If you are new to front-end/JavaScript application development I'd start with Vue.js. Then I'd work my way to React. Then I'd look at Angular 2+, Ember, or Aurelia.

If you are building a simple website that has minimal interactions with data (i.e. mostly a static content web site), you should avoid a front-end framework. A lot of work can be done with a task runner like Gulp and jQuery, while avoiding the unnecessary complexity of learning and using an app framework tool.

Want something smaller than React, consider Preact. Preact is an attempt to recreate the core value proposition of React (or similar libraries like Mithril) using as little code as possible, with first-class support for ES2015. Currently the library is around 3kb (minified & gzipped).

5.16 - JavaScript App Manager

5.17 - State Tools

5.18 - Progressive Web App Tools:

5.19 - GUI Development/Build Tools

5.20 - Templating/Data Binding Tools

Just Templating:

Templating and Reactive Data Binding:

Templating to Virtual DOM:

5.21 - UI Widget & Component Toolkits

On Web Platform:

React Specific, On Web Platform:

Native Desktop/Laptop/Netbook Apps via Web Platform (i.e. used with NW.js and Electron):

If you need a basic set of UI Widgets/Components start with Semantic UI. If you are building something that needs a grid, spreadsheet, or pivot grid you'll have to look at Kendo UI or Webix. Keep in mind that most of these solutions still require jQuery.

5.22 - Data Visualization (e.g., Charts) Tools

JS Libraries:

Widgets & Components:

Services (i.e. hosted data visualization services for embedding and sharing):

5.23 - Graphics (e.g., SVG, canvas, webgl) Tools

General:

Canvas:

SVG:

WebGL:

5.24 - Animation Tools

CSS and JavaScript Utilities:

Polyfills/Shims:

Animation References:

5.25 - JSON Tools

Online Editors:

Formatter & Validator:

Query Tools:

Generating Mock JSON Tools:

Online JSON Mocking API Tools:

List of public JSON API's:

Local JSON Mocking API Tools:

JSON Specifications/Schemas:

5.26 - Placeholder Content Tools

Images:

Device Mockups:

Text:

User Data:

5.27 - Testing Tools

Software Testing Frameworks:

Testing Assertions for Unit Testing:

Test Spies, Stubs, and Mocks for Unit Testing:

Hosted Testing/Automation for Browsers:

Integration/Functional Testing:

Browser Automation:

UI Testing Tools:

Automated dead link and error detectors:

HTTP Stubbing

5.28 - Front-End Data Storage Tools (i.e. Data storage solution in the client)

5.29 - Module Loading/Bundling Tools

5.30 - Module/Package Repository Tools

5.31 - Hosting Tools

General

Static

5.32 - Project Management & Code Hosting Tools

5.33 - Collaboration & Communication Tools

Code/GitHub Collaboration & Communication:

5.34 - Content Management Hosted/API Tools

Headless CMS Tools:

Self-hosted Headless CMS Tools:

Hosted CMS:

Static CMS Tools:

5.35 - Back-end/API tools

Data/back-end as a service aka BAAS:

Data/back-end

GraphQLApolloRelay* Falcor

User Management as a Service:

Search

5.36 - Offline Tools

For more tools look here.

5.37 - Security Tools

Coding Tool:

Security Scanners/Evaluators/Testers:

References:

5.38 - Tasking (aka Build) Tools

Tasking/Build Tools:

Opinionated Tasking/Build pipeline tools:

Before reaching for Gulp make sure npm scripts or yarn script won't fit the bill. Read, ["Why I Left Gulp and Grunt for npm Scripts"](https://medium.freecodecamp.com/why-i-left-gulp-and-grunt-for-npm-scripts-3d6853dd22b8#.nw3huib54 ""Why I Left Gulp and Grunt for npm Scripts"").

5.39 - Deployment Tools

5.40 - Site/App Monitoring Tools

Uptime Monitoring:

General Monitoring Tools:

5.41 - JavaScript Error Reporting/Monitoring

5.42 - Performance Tools

Reporting:

JS Tools:

Budgeting:

References/Docs:

Checklist:

5.43 - Tools for Finding Tools

5.44 - Documentation Generation Tools

Chapter 6. Front-end Communities, Newsletters, News Sites, & Podcasts

General Front-End Newsletters, News, & Podcasts:

HTML/CSS Newsletters:

JavaScript Newsletters, News, & Podcasts:

Front-End Communities

Notes:

  1. Need more Newsletters, News Sites, & Podcasts look at Awesome Newsletter.
  2. Find local front-end development communities by searching https://www.meetup.com/