GraphQL

Last updated 23 days ago

Cons of REST

  • composition of multiple resources requires multiple network requests

Fragment Container

https://facebook.github.io/relay/docs/fragment-container.html

Cons:

  • Can't change the params

Refetch Container

https://facebook.github.io/relay/docs/refetch-container.html

Cons:

  • First set of params/variables can't be dynamic/set

Pagination Container

https://facebook.github.io/relay/docs/pagination-container.html

Cons:

  • Can only change the count variable, nothing else

Best Practices

Ids

https://github.com/graphql/graphql-relay-js/blob/4fdadd3bbf3d5aaf66f1799be3e4eb010c115a4a/src/node/node.js

import {fromGlobalId, toGlobalId} from 'graphql-relay';
let {type, id} = fromGlobalId(globalId);
let globalID = toGlobalId(type, id);

Resolve

See GraphQLFieldResolveFn https://graphql.org/graphql-js/type/#graphqlobjecttype

resolve: async (root, args, context, info)
type GraphQLFieldResolveFn = (
source?: any,
args?: {[argName: string]: any},
context?: any,
info?: GraphQLResolveInfo
) => any

Fragments

Group fragments at top, for example:

fragment Example {
...Fragment1
...Fragment2
field1
field2
}

Examples

Relay

  • High initial cost of setting up

  • relay-compiler babel etc

Apollo

https://www.apollographql.com/

  • Supports TypeScript

Others

  • Apollo

  • Amazon Web Services (AWS) AppSync