- Apollo Server is GraphQL server for Node.js project. Apollo server is community-based, Simplicity, Well-Performance.
- Apollo Client is a fully-featured GraphQL client.
This article shows some tips for Apollo Server, Apollo Client with TypeScript.
🐞 Autorization by request header
Apollo Server
Apollo Server as GraphQL API server receives req.headers.token
import * as express from "express"; |
Apollo Client
In client side, you should pass the token in request header like this:
export function getClient(): ApolloClient<any> { |
More Details
If you want to know more detail, please see:
- https://www.apollographql.com/docs/apollo-server/whats-new.html
- https://www.apollographql.com/docs/apollo-server/migration-two-dot.html
- https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html#Usage
- https://github.com/expressjs/cors
- https://medium.com/react-native-training/building-chatty-part-4-graphql-mutations-optimistic-ui-8dee7778a170
🏀 Use Apollo Client in Node.js
Installation
yarn add apollo-cache-inmemory apollo-client apollo-link-http isomorphic-fetch |
Example
When using Apollo Client in Node.js, you need to prepare fetch
method.
isomorphic-fetch provides fetch
method like major browser.
(Please do not use node-fetch
library. It does not match in this case.)
import fetch from "isomorphic-fetch"; |
Also, if you want to set timeout configuration, please use that:
private static fetchTimeout(url, options = {} as any): any { |
🎃 How to accepts a dinamic array in Mutation
Schema definition in server side:
const schema = ` |
In client side:
mutation { |
🍄 References
- https://github.com/graphql/graphql-js/issues/19
- https://github.com/matthew-andrews/isomorphic-fetch/issues/48
🖥 Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!