Update GraphQL readme section (#6056)

This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-09-17 10:49:52 -07:00
committed by Douglas Muraoka
parent fc57ff4fb6
commit d70474016a

135
README.md
View File

@@ -418,10 +418,10 @@ Take a look at [Live Query Guide](https://docs.parseplatform.org/parse-server/gu
The easiest way to run the Parse GraphQL API is through the CLI: The easiest way to run the Parse GraphQL API is through the CLI:
``` ```bash
$ npm install -g parse-server mongodb-runner $ npm install -g parse-server mongodb-runner
$ mongodb-runner start $ mongodb-runner start
$ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://localhost/test --mountGraphQL --mountPlayground $ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://localhost/test --publicServerURL http://localhost:1337/parse --mountGraphQL --mountPlayground
``` ```
After starting the server, you can visit http://localhost:1337/playground in your browser to start playing with your GraphQL API. After starting the server, you can visit http://localhost:1337/playground in your browser to start playing with your GraphQL API.
@@ -432,12 +432,12 @@ After starting the server, you can visit http://localhost:1337/playground in you
You can also run the Parse GraphQL API inside a Docker container: You can also run the Parse GraphQL API inside a Docker container:
``` ```bash
$ git clone https://github.com/parse-community/parse-server $ git clone https://github.com/parse-community/parse-server
$ cd parse-server $ cd parse-server
$ docker build --tag parse-server . $ docker build --tag parse-server .
$ docker run --name my-mongo -d mongo $ docker run --name my-mongo -d mongo
$ docker run --name my-parse-server --link my-mongo:mongo -d parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://mongo/test --mountGraphQL --mountPlayground $ docker run --name my-parse-server --link my-mongo:mongo -p 1337:1337 -d parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://mongo/test --publicServerURL http://localhost:1337/parse --mountGraphQL --mountPlayground
``` ```
After starting the server, you can visit http://localhost:1337/playground in your browser to start playing with your GraphQL API. After starting the server, you can visit http://localhost:1337/playground in your browser to start playing with your GraphQL API.
@@ -446,9 +446,17 @@ After starting the server, you can visit http://localhost:1337/playground in you
### Using Express.js ### Using Express.js
You can also mount the GraphQL API in an Express.js application together with the REST API or solo: You can also mount the GraphQL API in an Express.js application together with the REST API or solo. You first need to create a new project and install the required dependencies:
```bash
$ mkdir my-app
$ cd my-app
$ npm install parse-server express --save
``` ```
Then, create an `index.js` file with the following content:
```js
const express = require('express'); const express = require('express');
const { default: ParseServer, ParseGraphQLServer } = require('parse-server'); const { default: ParseServer, ParseGraphQLServer } = require('parse-server');
@@ -458,7 +466,8 @@ const parseServer = new ParseServer({
databaseURI: 'mongodb://localhost:27017/test', databaseURI: 'mongodb://localhost:27017/test',
appId: 'APPLICATION_ID', appId: 'APPLICATION_ID',
masterKey: 'MASTER_KEY', masterKey: 'MASTER_KEY',
serverURL: 'http://localhost:1337/parse' serverURL: 'http://localhost:1337/parse',
publicServerURL: 'http://localhost:1337/parse'
}); });
const parseGraphQLServer = new ParseGraphQLServer( const parseGraphQLServer = new ParseGraphQLServer(
@@ -480,7 +489,14 @@ app.listen(1337, function() {
}); });
``` ```
After starting the server, you can visit http://localhost:1337/playground in your browser to start playing with your GraphQL API. And finally start your app:
```bash
$ npx mongodb-runner start
$ node index.js
```
After starting the app, you can visit http://localhost:1337/playground in your browser to start playing with your GraphQL API.
***Note:*** Do ***NOT*** mount the GraphQL Playground in production. [Parse Dashboard](https://github.com/parse-community/parse-dashboard) has a built-in GraphQL Playground and it is the recommended option for production apps. ***Note:*** Do ***NOT*** mount the GraphQL Playground in production. [Parse Dashboard](https://github.com/parse-community/parse-dashboard) has a built-in GraphQL Playground and it is the recommended option for production apps.
@@ -504,27 +520,66 @@ You should receive the following response:
} }
``` ```
## Creating your first object ## Creating your first class
Since your application does not have a schema yet, you can use the generic `create` mutation to create your first object. Run the following: Since your application does not have any schema yet, you can use the `createClass` mutation to create your first class. Run the following:
```graphql ```graphql
mutation CreateObject { mutation CreateClass {
create(className: "GameScore" fields: { score: 1337 playerName: "Sean Plott" cheatMode: false }) { createClass(
objectId name: "GameScore"
createdAt schemaFields: {
addStrings: [{ name: "playerName" }]
addNumbers: [{ name: "score" }]
addBooleans: [{ name: "cheatMode" }]
}
) {
name
schemaFields {
name
__typename
}
} }
} }
``` ```
You should receive a response similar to this: You should receive the following response:
```json ```json
{ {
"data": { "data": {
"create": { "createClass": {
"objectId": "CVuh0o0ioY", "name": "GameScore",
"createdAt": "2019-08-27T06:35:15.641Z" "schemaFields": [
{
"name": "objectId",
"__typename": "SchemaStringField"
},
{
"name": "updatedAt",
"__typename": "SchemaDateField"
},
{
"name": "createdAt",
"__typename": "SchemaDateField"
},
{
"name": "playerName",
"__typename": "SchemaStringField"
},
{
"name": "score",
"__typename": "SchemaNumberField"
},
{
"name": "cheatMode",
"__typename": "SchemaBooleanField"
},
{
"name": "ACL",
"__typename": "SchemaACLField"
}
]
} }
} }
} }
@@ -532,15 +587,26 @@ You should receive a response similar to this:
## Using automatically generated operations ## Using automatically generated operations
Parse Server learned from the first object that you created and now you have the `GameScore` class in your schema. You can now start using the automatically generated operations! Parse Server learned from the first class that you created and now you have the `GameScore` class in your schema. You can now start using the automatically generated operations!
Run the following to create a second object: Run the following to create your first object:
```graphql ```graphql
mutation CreateGameScore { mutation CreateGameScore {
createGameScore(fields: { score: 2558 playerName: "Luke Skywalker" cheatMode: false }) { createGameScore(
objectId fields: {
playerName: "Sean Plott"
score: 1337
cheatMode: false
}
) {
id
updatedAt
createdAt createdAt
playerName
score
cheatMode
ACL
} }
} }
``` ```
@@ -551,8 +617,13 @@ You should receive a response similar to this:
{ {
"data": { "data": {
"createGameScore": { "createGameScore": {
"objectId": "XyvErLoJ2O", "id": "XN75D94OBD",
"createdAt": "2019-08-27T06:37:32.078Z" "updatedAt": "2019-09-17T06:50:26.357Z",
"createdAt": "2019-09-17T06:50:26.357Z",
"playerName": "Sean Plott",
"score": 1337,
"cheatMode": false,
"ACL": null
} }
} }
} }
@@ -564,8 +635,13 @@ You can also run a query to this new class:
query GameScores { query GameScores {
gameScores { gameScores {
results { results {
id
updatedAt
createdAt
playerName playerName
score score
cheatMode
ACL
} }
} }
} }
@@ -579,12 +655,13 @@ You should receive a response similar to this:
"gameScores": { "gameScores": {
"results": [ "results": [
{ {
"id": "XN75D94OBD",
"updatedAt": "2019-09-17T06:50:26.357Z",
"createdAt": "2019-09-17T06:50:26.357Z",
"playerName": "Sean Plott", "playerName": "Sean Plott",
"score": 1337 "score": 1337,
}, "cheatMode": false,
{ "ACL": null
"playerName": "Luke Skywalker",
"score": 2558
} }
] ]
} }
@@ -599,7 +676,7 @@ Parse GraphQL Server allows you to create a custom GraphQL schema with own queri
To start creating your custom schema, you need to code a `schema.graphql` file and initialize Parse Server with `--graphQLSchema` and `--cloud` options: To start creating your custom schema, you need to code a `schema.graphql` file and initialize Parse Server with `--graphQLSchema` and `--cloud` options:
```bash ```bash
$ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://localhost/test --mountGraphQL --mountPlayground --graphQLSchema ./schema.graphql --cloud ./main.js $ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://localhost/test --publicServerURL http://localhost:1337/parse --cloud ./cloud/main.js --graphQLSchema ./cloud/schema.graphql --mountGraphQL --mountPlayground
``` ```
### Creating your first custom query ### Creating your first custom query