Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:graphcool/graphcool
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Oct 13, 2017
2 parents a7cb539 + dc570c8 commit 79af32f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ This directory contains an example implementation for an **API gateway on top of

The API gateway uses dedicated tooling that allows to easily implement a mapping from the custom schema to the underlying CRUD API.

Try out the read-only [demo](https://graphqlbin.com/lx9I1).
Try out the read-only [demo](https://graphqlbin.com/Pj1Iv).

## Get started

### 1. Download the example

```sh
curl https://codeload.github.com/graphcool/graphcool/tar.gz/master | tar -xz --strip=2 graphcool-master/examples/proxy-custom-schema
cd proxy-custom-schema
curl https://codeload.github.com/graphcool/graphcool/tar.gz/master | tar -xz --strip=2 graphcool-master/examples/gateway-custom-schema
cd gateway-custom-schema
```

### 2. Install the Graphcool CLI
Expand All @@ -38,15 +38,15 @@ Copy the endpoint for the `Simple API` as you'll need it in the next step.

The service you just deployed provides a CRUD API for the `User` and `Post` model types that are defined in [./service/types.graphql](./service/types.graphql).

The goal of the proxy server is now to create a _custom_ GraphQL API that only exposes variants of the underlying CRUD API.
The goal of the gateway server is now to create a _custom_ GraphQL API that only exposes variants of the underlying CRUD API.

### 4. Configure and start the API gateway server

#### 4.1. Set the endpoint for the GraphQL CRUD API

You first need to connect the gateway to the CRUD API.

Paste the the HTTP endpoint for the `Simple API` from the previous step into [./proxy/index.ts](./proxy/index.ts) as the value for `endpoint`, replacing the current placeholder `__SIMPLE_API_ENDPOINT__`:
Paste the the HTTP endpoint for the `Simple API` from the previous step into [./gateway/index.ts](./gateway/index.ts) as the value for `endpoint`, replacing the current placeholder `__SIMPLE_API_ENDPOINT__`:

```js
const endpoint = '__SIMPLE_API_ENDPOINT__' // looks like: https://api.graph.cool/simple/v1/__SERVICE_ID__
Expand All @@ -57,17 +57,17 @@ const endpoint = '__SIMPLE_API_ENDPOINT__' // looks like: https://api.graph.cool

#### 4.2. Start the server

Navigate into the [`proxy`](./proxy) directory, install the node dependencies and start the server:
Navigate into the [`gateway`](./gateway) directory, install the node dependencies and start the server:

```sh
cd ../proxy
cd ../gateway
yarn install
yarn start
```

#### 4.3. Open GraphQL Playground

The API that's exposed by the proxy is now available inside a GraphQL Playground under the following URL:
The API that's exposed by the gateway is now available inside a GraphQL Playground under the following URL:

[`http://localhost:3000/playground`](http://localhost:3000/playground)

Expand Down Expand Up @@ -103,7 +103,7 @@ mutation {
}
```

> **Note**: It's important the `alias` of the `User` is set to `john`. Otherwise the API gateway won't return any data since the alias in this example is [hardcoded](./proxy/index.ts#L54).
> **Note**: It's important the `alias` of the `User` is set to `john`. Otherwise the API gateway won't return any data since the alias in this example is [hardcoded](./gateway/index.ts#L54).
### 2. Send queries to the API gateway

Expand Down Expand Up @@ -174,7 +174,7 @@ type Mutation {
}
```

The API gateway now creates another API that will be exposed to the clients. The server that exposes this API is executing its queries against the underlying CRUD API. The magic enabling this functionality is implemented in the [`run`](./proxy/index.ts#L11) function in [index.ts](./proxy/index.ts).
The API gateway now creates another API that will be exposed to the clients. The server that exposes this API is executing its queries against the underlying CRUD API. The magic enabling this functionality is implemented in the [`run`](./gateway/index.ts#L11) function in [index.ts](./gateway/index.ts).

Here's the schema that defines the new API:

Expand All @@ -191,10 +191,10 @@ type Viewer {

There are four major steps that are being performed to map the CRUD API to the new schema:

1. Create local version of the CRUD API using [`makeRemoteExecutableSchema`](http://dev.apollodata.com/tools/graphql-tools/remote-schemas.html#makeRemoteExecutableSchema). [See the code](./proxy/index.ts#L13)
2. Define schema for the new API (the one exposed by the API gateway). [See the code](./proxy/index.ts#L121)
3. Merge remote schema with new schema using [`mergeSchemas`](http://dev.apollodata.com/tools/graphql-tools/schema-stitching.html#mergeSchemas). [See the code](./proxy/index.ts#L47)
4. Limit exposed operations from merged schemas (hiding all root fields except `viewer`) using [`transformSchema`](https://github.com/graphcool/graphql-transform-schema). [See the code](./proxy/index.ts#L67).
1. Create local version of the CRUD API using [`makeRemoteExecutableSchema`](http://dev.apollodata.com/tools/graphql-tools/remote-schemas.html#makeRemoteExecutableSchema). [See the code](./gateway/index.ts#L13)
2. Define schema for the new API (the one exposed by the API gateway). [See the code](./gateway/index.ts#L121)
3. Merge remote schema with new schema using [`mergeSchemas`](http://dev.apollodata.com/tools/graphql-tools/schema-stitching.html#mergeSchemas). [See the code](./gateway/index.ts#L47)
4. Limit exposed operations from merged schemas (hiding all root fields except `viewer`) using [`transformSchema`](https://github.com/graphcool/graphql-transform-schema). [See the code](./gateway/index.ts#L67).



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as cors from 'cors'
import * as bodyParser from 'body-parser'
import { graphqlExpress } from 'apollo-server-express'
import { transformSchema } from 'graphql-transform-schema'
import { makeRemoteExecutableSchema, makeExecutableSchema, mergeSchemas, introspectSchema } from 'graphql-tools'
import { makeRemoteExecutableSchema, mergeSchemas, introspectSchema } from 'graphql-tools'
import { HttpLink } from 'apollo-link-http'
import fetch from 'node-fetch'
import { express as playground } from 'graphql-playground/middleware'
Expand All @@ -19,35 +19,24 @@ async function run() {
})

// Step 2: Define schema for the new API
// TODO https://github.com/apollographql/graphql-tools/issues/427
const tmpSchema = makeExecutableSchema({
typeDefs: `
type Query {
viewer: Viewer!
}
type Viewer {
_tmp: String
}
`,
resolvers: {
Query: {
viewer: () => ({}),
}
const extendTypeDefs = `
extend type Query {
viewer: Viewer!
}
})
const extendTypeDefs = `
extend type Viewer {
type Viewer {
me: User
topPosts(limit: Int): [Post!]!
}
`

// Step 3: Merge remote schema with new schema
const mergedSchemas = mergeSchemas({
schemas: [graphcoolSchema, tmpSchema, extendTypeDefs],
schemas: [graphcoolSchema, extendTypeDefs],
resolvers: mergeInfo => ({
Query: {
viewer: () => ({}),
},
Viewer: {
me: {
resolve(parent, args, context, info) {
Expand Down Expand Up @@ -79,4 +68,4 @@ async function run() {
app.listen(3000, () => console.log('Server running. Open http://localhost:3000/playground to run queries.'))
}

run().catch(console.error.bind(console))
run().catch(console.error.bind(console))
Original file line number Diff line number Diff line change
Expand Up @@ -1177,8 +1177,8 @@ graphql-tools@^1.1.0:
"@types/graphql" "^0.9.0"

graphql-tools@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/graphql-tools/-/graphql-tools-2.3.0.tgz#91109b4a1d5df378ecdeba6eb76b2d672e20f427"
version "2.5.0"
resolved "https://registry.yarnpkg.com/graphql-tools/-/graphql-tools-2.5.0.tgz#b072faf8e1d621550598a681c08504bae13b1904"
dependencies:
apollo-link "^0.7.0"
deprecated-decorator "^0.1.6"
Expand Down

0 comments on commit 79af32f

Please sign in to comment.