Summary

In short, GraphQL is a technology to select the fields of an object that return to server. ^11f1f4

For example, given the following resolver:

var rootValue = {
  users: () => {
    return database;
  }
}

with a fake database: ^8cf161

var database = [
  {
    username: "Test",
    password: "12345",
    friends: [
      {
        name: "Teddy"
      },
      {
        name: "Long"
      }
    ]
  },
  {
    username: "Test2",
    password: "1234",
    friends: [
      {
        name: "Vu"
      },
    ]
  },
];

with following schema:

var schema = buildSchema(`
	type Query {  
		users: [User]  
	}

    type User {
        username: String
        password: String
        friends: [Friend]
    }
    
    type Friend {
      name: String
    }
`);

And the query:

{ 
	users { 
		username,
		friends {
			name
		}
	}  
} 

It will automatically filter out the username for us in our database objects.

{
  "data": {
    "users": [
      {
        "username": "Test",
        "friends": [
          {
            "name": "Teddy"
          },
          {
            "name": "Long"
          }
        ]
      },
      {
        "username": "Test2",
        "friends": [
          {
            "name": "Vu"
          }
        ]
      }
    ]
  }
}

Note: This does not work with List, or Map. Only works with Objects. GraphQL needs us to write the resolver to handle the logic behind each query. There is no magic here.

So in order for graphql to work, we need:

  1. Source data (could be anything, normally a database):
    • Contains the source data that GraphQL will mainly use to filter out the objects field
  2. Schema:
    • For GraphQL to know the structure of our source data for the query to happen
  3. Resolver:
    • We handle the logic for our query and mutation to return the result object that we want
  4. Entry point:
    • How do we want to trigger this GraphQL. Could be via launching a server (commonly) or executing via function