From b4df7a2524cb0986a3e703bd31994862a6d718fb Mon Sep 17 00:00:00 2001 From: = Date: Thu, 18 Jul 2019 22:28:20 -0700 Subject: [PATCH 1/6] Add --graphQLSchema to CLI --- src/Options/Definitions.js | 4 ++++ src/Options/docs.js | 1 + src/Options/index.js | 2 ++ src/ParseServer.js | 12 +++++++++++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Options/Definitions.js b/src/Options/Definitions.js index 4005b2504d..5b6f580b37 100644 --- a/src/Options/Definitions.js +++ b/src/Options/Definitions.js @@ -158,6 +158,10 @@ module.exports.ParseServerOptions = { help: 'Mount path for the GraphQL endpoint, defaults to /graphql', default: '/graphql', }, + graphQLSchema: { + env: 'PARSE_SERVER_GRAPH_QLSCHEMA', + help: 'Full path to your GraphQL custom schema.graphql file.', + }, host: { env: 'PARSE_SERVER_HOST', help: 'The host to serve ParseServer on, defaults to 0.0.0.0', diff --git a/src/Options/docs.js b/src/Options/docs.js index fa8d584c94..3962d2134b 100644 --- a/src/Options/docs.js +++ b/src/Options/docs.js @@ -28,6 +28,7 @@ * @property {String} fileKey Key for your files * @property {Adapter} filesAdapter Adapter module for the files sub-system * @property {String} graphQLPath Mount path for the GraphQL endpoint, defaults to /graphql + * @property {String} graphQLSchema Full path to your GraphQL custom schema.graphql file. * @property {String} host The host to serve ParseServer on, defaults to 0.0.0.0 * @property {String} javascriptKey Key for the Javascript SDK * @property {Boolean} jsonLogs Log as structured JSON objects diff --git a/src/Options/index.js b/src/Options/index.js index a1d3c377a2..2198c08222 100644 --- a/src/Options/index.js +++ b/src/Options/index.js @@ -180,6 +180,8 @@ export interface ParseServerOptions { startLiveQueryServer: ?boolean; /* Live query server configuration options (will start the liveQuery server) */ liveQueryServerOptions: ?LiveQueryServerOptions; + /* Full path to your GraphQL custom schema.graphql file. */ + graphQLSchema: ?string; /* Mounts the GraphQL endpoint :ENV: PARSE_SERVER_MOUNT_GRAPHQL :DEFAULT: false */ diff --git a/src/ParseServer.js b/src/ParseServer.js index 738a0624b9..1297127d06 100644 --- a/src/ParseServer.js +++ b/src/ParseServer.js @@ -5,7 +5,9 @@ var batch = require('./batch'), express = require('express'), middlewares = require('./middlewares'), Parse = require('parse/node').Parse, - path = require('path'); + { parse } = require('graphql'), + path = require('path'), + fs = require('fs'); import { ParseServerOptions, LiveQueryServerOptions } from './Options'; import defaults from './defaults'; @@ -267,9 +269,17 @@ class ParseServer { app.use(options.mountPath, this.app); if (options.mountGraphQL === true || options.mountPlayground === true) { + let graphQLCustomTypeDefs = undefined; + if (options.graphQLSchema) { + graphQLCustomTypeDefs = parse( + fs.readFileSync(options.graphQLSchema, 'utf8') + ); + } + const parseGraphQLServer = new ParseGraphQLServer(this, { graphQLPath: options.graphQLPath, playgroundPath: options.playgroundPath, + graphQLCustomTypeDefs, }); if (options.mountGraphQL) { From d9b7365b7282c114fc2fb5847c7731679c7bbafc Mon Sep 17 00:00:00 2001 From: = Date: Thu, 18 Jul 2019 23:11:35 -0700 Subject: [PATCH 2/6] Add custom graphql schema instructions to readme file --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b2be7a90ec..db183035e0 100644 --- a/README.md +++ b/README.md @@ -361,7 +361,7 @@ Take a look at [Live Query Guide](https://docs.parseplatform.org/parse-server/gu # GraphQL -[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. +[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. ## Running @@ -555,6 +555,52 @@ You should receive a response similar to this: } ``` +## Customizing your GraphQL Schema + +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. + +To start creating your custom schema, you need to code a `schema.graphql` file and initialize Parse Server with `--graphQLSchema` and `--cloud` options: + +```bash +$ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongodb://localhost/test --mountGraphQL --mountPlayground --graphQLSchema ./schema.graphql --cloud ./main.js +``` + +### Creating your first custom query + +Use the codes below for your `schema.graphql` and `main.js` files. Then restart your Parse Server. + +```graphql +# schema.graphql +extend type Query { + hello: String! @resolve +} +``` + +```js +// main.js +Parse.Cloud.define('hello', async () => { + return 'Hello world!'; +}); +``` + +You can now run your custom query using GraphQL Playground: + +```graphql +query { + hello +} +``` + +You should receive the response below: + +```json +{ + "data": { + "hello": "Hello world!" + } +} +``` + ## Learning more The [Parse GraphQL Guide](http://docs.parseplatform.org/graphql/guide/) is a very good source for learning how to use the Parse GraphQL API. From 66f11bede961041172fc912e2874896435e9979b Mon Sep 17 00:00:00 2001 From: Antonio Davi Macedo Coelho de Castro Date: Fri, 19 Jul 2019 11:37:10 -0700 Subject: [PATCH 3/6] Update README.md Co-Authored-By: Tom Fox --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index db183035e0..0fbf3e44ae 100644 --- a/README.md +++ b/README.md @@ -567,7 +567,7 @@ $ parse-server --appId APPLICATION_ID --masterKey MASTER_KEY --databaseURI mongo ### Creating your first custom query -Use the codes below for your `schema.graphql` and `main.js` files. Then restart your Parse Server. +Use the code below for your `schema.graphql` and `main.js` files. Then restart your Parse Server. ```graphql # schema.graphql From b5b35408ed9c58b671a5a4b51194d533d8208344 Mon Sep 17 00:00:00 2001 From: Antonio Davi Macedo Coelho de Castro Date: Fri, 19 Jul 2019 11:37:47 -0700 Subject: [PATCH 4/6] Update src/Options/Definitions.js Co-Authored-By: Tom Fox --- src/Options/Definitions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Options/Definitions.js b/src/Options/Definitions.js index 5b6f580b37..1139050b5f 100644 --- a/src/Options/Definitions.js +++ b/src/Options/Definitions.js @@ -160,7 +160,7 @@ module.exports.ParseServerOptions = { }, graphQLSchema: { env: 'PARSE_SERVER_GRAPH_QLSCHEMA', - help: 'Full path to your GraphQL custom schema.graphql file.', + help: 'Full path to your GraphQL custom schema.graphql file', }, host: { env: 'PARSE_SERVER_HOST', From 0788a0e313b79eede3baf141d1b9bb9bd11fc841 Mon Sep 17 00:00:00 2001 From: Antonio Davi Macedo Coelho de Castro Date: Fri, 19 Jul 2019 11:38:05 -0700 Subject: [PATCH 5/6] Update src/Options/docs.js Co-Authored-By: Tom Fox --- src/Options/docs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Options/docs.js b/src/Options/docs.js index 3962d2134b..4883d133c4 100644 --- a/src/Options/docs.js +++ b/src/Options/docs.js @@ -28,7 +28,7 @@ * @property {String} fileKey Key for your files * @property {Adapter} filesAdapter Adapter module for the files sub-system * @property {String} graphQLPath Mount path for the GraphQL endpoint, defaults to /graphql - * @property {String} graphQLSchema Full path to your GraphQL custom schema.graphql file. + * @property {String} graphQLSchema Full path to your GraphQL custom schema.graphql file * @property {String} host The host to serve ParseServer on, defaults to 0.0.0.0 * @property {String} javascriptKey Key for the Javascript SDK * @property {Boolean} jsonLogs Log as structured JSON objects From c49d6d05a229ea186d9aea68a282afc62b6b9bc1 Mon Sep 17 00:00:00 2001 From: Antonio Davi Macedo Coelho de Castro Date: Fri, 19 Jul 2019 11:38:12 -0700 Subject: [PATCH 6/6] Update src/Options/index.js Co-Authored-By: Tom Fox --- src/Options/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Options/index.js b/src/Options/index.js index 2198c08222..f45c31b20a 100644 --- a/src/Options/index.js +++ b/src/Options/index.js @@ -180,7 +180,7 @@ export interface ParseServerOptions { startLiveQueryServer: ?boolean; /* Live query server configuration options (will start the liveQuery server) */ liveQueryServerOptions: ?LiveQueryServerOptions; - /* Full path to your GraphQL custom schema.graphql file. */ + /* Full path to your GraphQL custom schema.graphql file */ graphQLSchema: ?string; /* Mounts the GraphQL endpoint :ENV: PARSE_SERVER_MOUNT_GRAPHQL