Skip to content

Latest commit

 

History

History
 
 

graphcool-lib

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Sending queries and mutations with graphcool-lib

This example demonstrates how to send queries and mutations against your service's API using graphcool-lib from inside a Graphcool function.

Get started

1. Download the example

Clone the full framework repository and navigate to this directory or download only this example with the following command:

curl https://codeload.github.com/graphcool/framework/tar.gz/master | tar -xz --strip=2 framework-master/examples/graphcool-lib
cd graphcool-lib

Next, you need to create your GraphQL server using the Graphcool CLI.

2. Install the Graphcool CLI

If you haven't already, go ahead and install the CLI first:

npm install -g graphcool-framework

3. Create the GraphQL server

The next step will be to deploy the Graphcool service that's defined in this directory.

To deploy the service and actually create your GraphQL server, invoke the following command:

graphcool-framework deploy

When prompted which cluster you'd like to deploy, choose any of the Shared Clusters (shared-eu-west-1, shared-ap-northeast-1 or shared-us-west-2) rather than local.

Note: Whenever you make changes to files in this directory, you need to invoke graphcool-framework deploy again to make sure your changes get applied to the "remote" service.

Testing the service

Open a Playground

The easiest way to test the deployed service is by using a GraphQL Playground.

You can open a Playground with the following command:

graphcool-framework playground

Create User with three Posts

In the Playground, send the following mutation:

mutation {
  createUser(posts: [{
    imageURL: "https://dog.ceo/api/img/terrier-dandie/n02096437_806.jpg"
  }, {
    imageURL: "https://dog.ceo/api/img/mexicanhairless/n02113978_773.jpg"
  }, {
    imageURL: "https://dog.ceo/api/img/briard/n02105251_8951.jpg"
  }]) {
    id
  }
}

This creates a new User node along with three Post nodes that are connected via the UserPosts relation. Save the id of the newly created User node that's returned by the server.

Test the opeationBefore hook function

Due to the constraint implemented in checkPostCount.js, a User can at most be associated with 3 Post nodes. The new User has exactly three Post nodes, so any subsequent createPost-mutations for that User node are expected to fail.

Send the following mutation through the Playground. Don't forget to replace the placeholder __AUTHOR_ID__ with the id that ewas returned in the createUser mutation in the previous step:

mutation {
  createPost(authorId: "__AUTHOR_ID__", imageURL: "https://dog.ceo/api/img/hound-Ibizan/n02091244_569.jpg") {
    id
  }
}

Test the custom postRandomDogImage mutation

The resolver function defined and implemented in postRandomDogImage.graphql and postRandomDogImage.js fetches a random dog image from a public dog image API and stores it in the database.

You can invoke it as follows:

mutation {
  postRandomDogImage(authorId: "__AUTHOR_ID__") {
    url
  }
}