This example illustrates the implementation of File API with GraphQL Gateway pattern. The best example for GraphQL Gateway basic usage can be found here: https://github.com/graphcool/graphql-server-example .
graphcool deploy # choose local cluster
# copy API endpoint into the `GRAPHCOOL_ENPOINT` env var in .env
To get GRAPHCOOL_SECRET
visit http://jwtbuilder.jamiekurtz.com and scroll to the bottom where you can hash your secret from graphcool.yml
and get the hashed output. (sssh
is used in the example.)
- Head over to the AWS console and navigate to the
S3
section. - Click create bucket and follow the instructions on screen.
- Once you have created a
bucket
, add bucket name that you've picked to .envS3_BUCKET
property. - Head back to the AWS Console and open
Identity and Access Management (IAM)
console. Navigate toUsers
and clickAdd user
. - Under Access type check Programmatic access and press
Next
. From options, select Attach existing policies directly and a table below will open. Search for AmazonS3FullAccess and check it. PressNext
to review everything and submit by pressingCreate user
. - Once done, copy the Access key ID to .env
S3_KEY
property and Secret access key to .envS3_SECRET
property. - You are all set to start the server!
yarn install
yarn start
# Open http://localhost:5000/
You can upload files to a project by doing a multipart/form-data
HTTP request to the File API http://localhost:5000/upload
.
It's important to use the form parameter data
as seen in the example below.
Everytime you upload a file to Graphcool, a new File
node is created that contains information about that file.
id
: the familiar system fieldsecret
: a unique, unguessable secret that allows access to the filename
: the file namesize
: the file sizeurl
: the url of the file where it can be accessed. The url contains of the project id and the filesecret
, so is unguessable as well.contentType
: the contentType of the file. It is determined based on the file name.
If you want to connect the File
node to another node in a relation, you can use the id
in the response.
Withcurl
you could execute:
curl -X POST 'http://localhost:5000/upload' -F "[email protected]"
This uploads the local file example.png
. The response could look something like this:
{
"id": "cjbqvp4ii00390181b1q0dq6h",
"name": "example.png",
"secret": "43de4b08-78b2-4b5c-a5b7-05ee350ee09a",
"contentType": "image/png",
"size": 36625,
"url": "https://__S3_BUCKET__.s3-eu-west-1.amazonaws.com/43de4b08-78b2-4b5c-a5b7-05ee350ee09a"
}
database
: GraphQL database service definitions (using Graphcool)src
: Source code of the gateway
.env
: Contains env vars (such asGRAPHCOOL_ENDPOINT
andGRAPHCOOL_APIKEY
).graphqlconfig.yml
: GraphQL config file used for IDE support andgraphql-cli
tsconfig.json
: Typescript compiler settings
MIT