RESTful API Limitations
Endpoints are often designed to match a client. But clients get updated endpoints may not; this may lead to over-fetching or under-fetching.
Over-fetching: download more data than you need. e.g. need list of usernames, but get list of users and all available fields
Under-fetching: download less data than you need. e.g. want information about friends; first fetch list of friend IDs, then make request to get user data about each friend.
GraphQL
A specification for how you specify data (c.f. strong typing) and how you query it.
Example:
type TypeName {
nonNullString: String!,
nullArray: [String]
}
// String is a *scalar* type; a base type
All GraphQL queries return a 200 response code (unless there is a network error). Errors are returned in user-defined fields.
GraphQL:
- Returns 200 for all queries (unless there is a network error). Errors are returned in user-defined fields
- Uses
GETs andPOSTs; the former encodes the query in the URL query parameters while the latter encodes it in the body as JSON - Is a language specification for composing queries to a server
- Still requires some pre-defined data/queries on the server-side (what is allowable etc.)
Queries are in a JSON-like structure e.g.
{
assets(10) {
id,
url,
nestedObject: {
prop1,
prop2
}
}
}
Automated Testing
Mocha and Chai
- Runs test files in order given to it by the filesystem
- Within each test, tests are independent and run asynchronously
- Can setup pre- and post- conditions with
before(),aftereachetc. - Call
doneat the end of a test - Can test HTTP requests using Chai-HTTP
- Ensure erroneous queries are tested as well