Skip to content

Commit a0d5217

Browse files
davimacedoTomWFox
andauthored
GraphQL custom schema on CLI (parse-community#5828)
* Add --graphQLSchema to CLI * Add custom graphql schema instructions to readme file * Update README.md Co-Authored-By: Tom Fox <[email protected]> * Update src/Options/Definitions.js Co-Authored-By: Tom Fox <[email protected]> * Update src/Options/docs.js Co-Authored-By: Tom Fox <[email protected]> * Update src/Options/index.js Co-Authored-By: Tom Fox <[email protected]>
1 parent 97f95bb commit a0d5217

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Take a look at [Live Query Guide](https://docs.parseplatform.org/parse-server/gu
361361

362362
# GraphQL
363363

364-
[GraphQL](https://graphql.org/), developed by Facebook, is an open-source data query and manipulation language for APIs. In addition to the traditional REST API, Parse Server automatically generates a GraphQL API based on your current application schema.
364+
[GraphQL](https://graphql.org/), developed by Facebook, is an open-source data query and manipulation language for APIs. In addition to the traditional REST API, Parse Server automatically generates a GraphQL API based on your current application schema. Parse Server also allows you to define your custom GraphQL queries and mutations, whose resolvers can be bound to your cloud code functions.
365365

366366
## Running
367367

@@ -555,6 +555,52 @@ You should receive a response similar to this:
555555
}
556556
```
557557

558+
## Customizing your GraphQL Schema
559+
560+
Parse GraphQL Server allows you to create a custom GraphQL schema with own queries and mutations to be merged with the auto-generated ones. You can resolve these operations using your regular cloud code functions.
561+
562+
To start creating your custom schema, you need to code a `schema.graphql` file and initialize Parse Server with `--graphQLSchema` and `--cloud` options:
563+
564+
```bash
565+
$ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://localhost/test --mountGraphQL --mountPlayground --graphQLSchema ./schema.graphql --cloud ./main.js
566+
```
567+
568+
### Creating your first custom query
569+
570+
Use the code below for your `schema.graphql` and `main.js` files. Then restart your Parse Server.
571+
572+
```graphql
573+
# schema.graphql
574+
extend type Query {
575+
hello: String! @resolve
576+
}
577+
```
578+
579+
```js
580+
// main.js
581+
Parse.Cloud.define('hello', async () => {
582+
return 'Hello world!';
583+
});
584+
```
585+
586+
You can now run your custom query using GraphQL Playground:
587+
588+
```graphql
589+
query {
590+
hello
591+
}
592+
```
593+
594+
You should receive the response below:
595+
596+
```json
597+
{
598+
"data": {
599+
"hello": "Hello world!"
600+
}
601+
}
602+
```
603+
558604
## Learning more
559605

560606
The [Parse GraphQL Guide](http://docs.parseplatform.org/graphql/guide/) is a very good source for learning how to use the Parse GraphQL API.

src/Options/Definitions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ module.exports.ParseServerOptions = {
158158
help: 'Mount path for the GraphQL endpoint, defaults to /graphql',
159159
default: '/graphql',
160160
},
161+
graphQLSchema: {
162+
env: 'PARSE_SERVER_GRAPH_QLSCHEMA',
163+
help: 'Full path to your GraphQL custom schema.graphql file',
164+
},
161165
host: {
162166
env: 'PARSE_SERVER_HOST',
163167
help: 'The host to serve ParseServer on, defaults to 0.0.0.0',

src/Options/docs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @property {String} fileKey Key for your files
2929
* @property {Adapter<FilesAdapter>} filesAdapter Adapter module for the files sub-system
3030
* @property {String} graphQLPath Mount path for the GraphQL endpoint, defaults to /graphql
31+
* @property {String} graphQLSchema Full path to your GraphQL custom schema.graphql file
3132
* @property {String} host The host to serve ParseServer on, defaults to 0.0.0.0
3233
* @property {String} javascriptKey Key for the Javascript SDK
3334
* @property {Boolean} jsonLogs Log as structured JSON objects

src/Options/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ export interface ParseServerOptions {
180180
startLiveQueryServer: ?boolean;
181181
/* Live query server configuration options (will start the liveQuery server) */
182182
liveQueryServerOptions: ?LiveQueryServerOptions;
183+
/* Full path to your GraphQL custom schema.graphql file */
184+
graphQLSchema: ?string;
183185
/* Mounts the GraphQL endpoint
184186
:ENV: PARSE_SERVER_MOUNT_GRAPHQL
185187
:DEFAULT: false */

src/ParseServer.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ var batch = require('./batch'),
55
express = require('express'),
66
middlewares = require('./middlewares'),
77
Parse = require('parse/node').Parse,
8-
path = require('path');
8+
{ parse } = require('graphql'),
9+
path = require('path'),
10+
fs = require('fs');
911

1012
import { ParseServerOptions, LiveQueryServerOptions } from './Options';
1113
import defaults from './defaults';
@@ -267,9 +269,17 @@ class ParseServer {
267269
app.use(options.mountPath, this.app);
268270

269271
if (options.mountGraphQL === true || options.mountPlayground === true) {
272+
let graphQLCustomTypeDefs = undefined;
273+
if (options.graphQLSchema) {
274+
graphQLCustomTypeDefs = parse(
275+
fs.readFileSync(options.graphQLSchema, 'utf8')
276+
);
277+
}
278+
270279
const parseGraphQLServer = new ParseGraphQLServer(this, {
271280
graphQLPath: options.graphQLPath,
272281
playgroundPath: options.playgroundPath,
282+
graphQLCustomTypeDefs,
273283
});
274284

275285
if (options.mountGraphQL) {

0 commit comments

Comments
 (0)