2019-06-19 17:19:47 -07:00
|
|
|
import Parse from 'parse/node';
|
|
|
|
|
import { GraphQLSchema, GraphQLObjectType } from 'graphql';
|
GraphQL Custom Schema (#5821)
This PR empowers the Parse GraphQL API with custom user-defined schema. The developers can now write their own types, queries, and mutations, which will merged with the ones that are automatically generated. The new types are resolved by the application's cloud code functions.
Therefore, regarding https://github.com/parse-community/parse-server/issues/5777, this PR closes the cloud functions needs and also addresses the graphql customization topic. In my view, I think that this PR, together with https://github.com/parse-community/parse-server/pull/5782 and https://github.com/parse-community/parse-server/pull/5818, when merged, closes the issue.
How it works:
1. When initializing ParseGraphQLServer, now the developer can pass a custom schema that will be merged to the auto-generated one:
```
parseGraphQLServer = new ParseGraphQLServer(parseServer, {
graphQLPath: '/graphql',
graphQLCustomTypeDefs: gql`
extend type Query {
custom: Custom @namespace
}
type Custom {
hello: String @resolve
hello2: String @resolve(to: "hello")
userEcho(user: _UserFields!): _UserClass! @resolve
}
`,
});
```
Note:
- This PR includes a @namespace directive that can be used to the top level field of the nested queries and mutations (it basically just returns an empty object);
- This PR includes a @resolve directive that can be used to notify the Parse GraphQL Server to resolve that field using a cloud code function. The `to` argument specifies the function name. If the `to` argument is not passed, the Parse GraphQL Server will look for a function with the same name of the field;
- This PR allows creating custom types using the auto-generated ones as in `userEcho(user: _UserFields!): _UserClass! @resolve`;
- This PR allows to extend the auto-generated types, as in `extend type Query { ... }`.
2. Once the schema was set, you just need to write regular cloud code functions:
```
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
Parse.Cloud.define('userEcho', async req => {
return req.params.user;
});
```
3. Now you are ready to play with your new custom api:
```
query {
custom {
hello
hello2
userEcho(user: { username: "somefolk" }) {
username
}
}
}
```
should return
```
{
"data": {
"custom": {
"hello": "Hello world!",
"hello2": "Hello world!",
"userEcho": {
"username": "somefolk"
}
}
}
}
```
2019-07-18 12:43:49 -07:00
|
|
|
import { mergeSchemas, SchemaDirectiveVisitor } from 'graphql-tools';
|
2019-06-19 17:19:47 -07:00
|
|
|
import requiredParameter from '../requiredParameter';
|
|
|
|
|
import * as defaultGraphQLTypes from './loaders/defaultGraphQLTypes';
|
|
|
|
|
import * as parseClassTypes from './loaders/parseClassTypes';
|
|
|
|
|
import * as parseClassQueries from './loaders/parseClassQueries';
|
|
|
|
|
import * as parseClassMutations from './loaders/parseClassMutations';
|
|
|
|
|
import * as defaultGraphQLQueries from './loaders/defaultGraphQLQueries';
|
|
|
|
|
import * as defaultGraphQLMutations from './loaders/defaultGraphQLMutations';
|
2019-07-25 20:46:25 +01:00
|
|
|
import ParseGraphQLController, {
|
|
|
|
|
ParseGraphQLConfig,
|
|
|
|
|
} from '../Controllers/ParseGraphQLController';
|
|
|
|
|
import DatabaseController from '../Controllers/DatabaseController';
|
2019-07-12 17:58:47 -03:00
|
|
|
import { toGraphQLError } from './parseGraphQLUtils';
|
GraphQL Custom Schema (#5821)
This PR empowers the Parse GraphQL API with custom user-defined schema. The developers can now write their own types, queries, and mutations, which will merged with the ones that are automatically generated. The new types are resolved by the application's cloud code functions.
Therefore, regarding https://github.com/parse-community/parse-server/issues/5777, this PR closes the cloud functions needs and also addresses the graphql customization topic. In my view, I think that this PR, together with https://github.com/parse-community/parse-server/pull/5782 and https://github.com/parse-community/parse-server/pull/5818, when merged, closes the issue.
How it works:
1. When initializing ParseGraphQLServer, now the developer can pass a custom schema that will be merged to the auto-generated one:
```
parseGraphQLServer = new ParseGraphQLServer(parseServer, {
graphQLPath: '/graphql',
graphQLCustomTypeDefs: gql`
extend type Query {
custom: Custom @namespace
}
type Custom {
hello: String @resolve
hello2: String @resolve(to: "hello")
userEcho(user: _UserFields!): _UserClass! @resolve
}
`,
});
```
Note:
- This PR includes a @namespace directive that can be used to the top level field of the nested queries and mutations (it basically just returns an empty object);
- This PR includes a @resolve directive that can be used to notify the Parse GraphQL Server to resolve that field using a cloud code function. The `to` argument specifies the function name. If the `to` argument is not passed, the Parse GraphQL Server will look for a function with the same name of the field;
- This PR allows creating custom types using the auto-generated ones as in `userEcho(user: _UserFields!): _UserClass! @resolve`;
- This PR allows to extend the auto-generated types, as in `extend type Query { ... }`.
2. Once the schema was set, you just need to write regular cloud code functions:
```
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
Parse.Cloud.define('userEcho', async req => {
return req.params.user;
});
```
3. Now you are ready to play with your new custom api:
```
query {
custom {
hello
hello2
userEcho(user: { username: "somefolk" }) {
username
}
}
}
```
should return
```
{
"data": {
"custom": {
"hello": "Hello world!",
"hello2": "Hello world!",
"userEcho": {
"username": "somefolk"
}
}
}
}
```
2019-07-18 12:43:49 -07:00
|
|
|
import * as schemaDirectives from './loaders/schemaDirectives';
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-08-15 23:23:41 +02:00
|
|
|
const RESERVED_GRAPHQL_TYPE_NAMES = [
|
|
|
|
|
'String',
|
|
|
|
|
'Boolean',
|
|
|
|
|
'Int',
|
|
|
|
|
'Float',
|
|
|
|
|
'ID',
|
|
|
|
|
'ArrayResult',
|
|
|
|
|
'Query',
|
|
|
|
|
'Mutation',
|
|
|
|
|
'Subscription',
|
|
|
|
|
'ObjectsQuery',
|
|
|
|
|
'UsersQuery',
|
|
|
|
|
'ObjectsMutation',
|
|
|
|
|
'FilesMutation',
|
|
|
|
|
'UsersMutation',
|
|
|
|
|
'FunctionsMutation',
|
|
|
|
|
'Viewer',
|
|
|
|
|
'SignUpFieldsInput',
|
|
|
|
|
'LogInFieldsInput',
|
|
|
|
|
];
|
|
|
|
|
const RESERVED_GRAPHQL_OBJECT_QUERY_NAMES = ['get', 'find'];
|
|
|
|
|
const RESERVED_GRAPHQL_OBJECT_MUTATION_NAMES = ['create', 'update', 'delete'];
|
|
|
|
|
|
2019-06-19 17:19:47 -07:00
|
|
|
class ParseGraphQLSchema {
|
2019-07-25 20:46:25 +01:00
|
|
|
databaseController: DatabaseController;
|
|
|
|
|
parseGraphQLController: ParseGraphQLController;
|
|
|
|
|
parseGraphQLConfig: ParseGraphQLConfig;
|
|
|
|
|
graphQLCustomTypeDefs: any;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
params: {
|
|
|
|
|
databaseController: DatabaseController,
|
|
|
|
|
parseGraphQLController: ParseGraphQLController,
|
|
|
|
|
log: any,
|
|
|
|
|
} = {}
|
|
|
|
|
) {
|
|
|
|
|
this.parseGraphQLController =
|
|
|
|
|
params.parseGraphQLController ||
|
|
|
|
|
requiredParameter('You must provide a parseGraphQLController instance!');
|
2019-06-19 17:19:47 -07:00
|
|
|
this.databaseController =
|
2019-07-25 20:46:25 +01:00
|
|
|
params.databaseController ||
|
2019-06-19 17:19:47 -07:00
|
|
|
requiredParameter('You must provide a databaseController instance!');
|
2019-07-25 20:46:25 +01:00
|
|
|
this.log =
|
|
|
|
|
params.log || requiredParameter('You must provide a log instance!');
|
|
|
|
|
this.graphQLCustomTypeDefs = params.graphQLCustomTypeDefs;
|
2019-06-19 17:19:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async load() {
|
2019-07-25 20:46:25 +01:00
|
|
|
const { parseGraphQLConfig } = await this._initializeSchemaAndConfig();
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
const parseClasses = await this._getClassesForSchema(parseGraphQLConfig);
|
|
|
|
|
const parseClassesString = JSON.stringify(parseClasses);
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
if (
|
|
|
|
|
this.graphQLSchema &&
|
|
|
|
|
!this._hasSchemaInputChanged({
|
|
|
|
|
parseClasses,
|
|
|
|
|
parseClassesString,
|
|
|
|
|
parseGraphQLConfig,
|
|
|
|
|
})
|
|
|
|
|
) {
|
|
|
|
|
return this.graphQLSchema;
|
2019-06-19 17:19:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.parseClasses = parseClasses;
|
|
|
|
|
this.parseClassesString = parseClassesString;
|
2019-07-25 20:46:25 +01:00
|
|
|
this.parseGraphQLConfig = parseGraphQLConfig;
|
2019-06-19 17:19:47 -07:00
|
|
|
this.parseClassTypes = {};
|
2019-08-15 23:23:41 +02:00
|
|
|
this.viewerType = null;
|
GraphQL Custom Schema (#5821)
This PR empowers the Parse GraphQL API with custom user-defined schema. The developers can now write their own types, queries, and mutations, which will merged with the ones that are automatically generated. The new types are resolved by the application's cloud code functions.
Therefore, regarding https://github.com/parse-community/parse-server/issues/5777, this PR closes the cloud functions needs and also addresses the graphql customization topic. In my view, I think that this PR, together with https://github.com/parse-community/parse-server/pull/5782 and https://github.com/parse-community/parse-server/pull/5818, when merged, closes the issue.
How it works:
1. When initializing ParseGraphQLServer, now the developer can pass a custom schema that will be merged to the auto-generated one:
```
parseGraphQLServer = new ParseGraphQLServer(parseServer, {
graphQLPath: '/graphql',
graphQLCustomTypeDefs: gql`
extend type Query {
custom: Custom @namespace
}
type Custom {
hello: String @resolve
hello2: String @resolve(to: "hello")
userEcho(user: _UserFields!): _UserClass! @resolve
}
`,
});
```
Note:
- This PR includes a @namespace directive that can be used to the top level field of the nested queries and mutations (it basically just returns an empty object);
- This PR includes a @resolve directive that can be used to notify the Parse GraphQL Server to resolve that field using a cloud code function. The `to` argument specifies the function name. If the `to` argument is not passed, the Parse GraphQL Server will look for a function with the same name of the field;
- This PR allows creating custom types using the auto-generated ones as in `userEcho(user: _UserFields!): _UserClass! @resolve`;
- This PR allows to extend the auto-generated types, as in `extend type Query { ... }`.
2. Once the schema was set, you just need to write regular cloud code functions:
```
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
Parse.Cloud.define('userEcho', async req => {
return req.params.user;
});
```
3. Now you are ready to play with your new custom api:
```
query {
custom {
hello
hello2
userEcho(user: { username: "somefolk" }) {
username
}
}
}
```
should return
```
{
"data": {
"custom": {
"hello": "Hello world!",
"hello2": "Hello world!",
"userEcho": {
"username": "somefolk"
}
}
}
}
```
2019-07-18 12:43:49 -07:00
|
|
|
this.graphQLAutoSchema = null;
|
2019-06-19 17:19:47 -07:00
|
|
|
this.graphQLSchema = null;
|
|
|
|
|
this.graphQLTypes = [];
|
|
|
|
|
this.graphQLObjectsQueries = {};
|
|
|
|
|
this.graphQLQueries = {};
|
|
|
|
|
this.graphQLObjectsMutations = {};
|
|
|
|
|
this.graphQLMutations = {};
|
|
|
|
|
this.graphQLSubscriptions = {};
|
GraphQL Custom Schema (#5821)
This PR empowers the Parse GraphQL API with custom user-defined schema. The developers can now write their own types, queries, and mutations, which will merged with the ones that are automatically generated. The new types are resolved by the application's cloud code functions.
Therefore, regarding https://github.com/parse-community/parse-server/issues/5777, this PR closes the cloud functions needs and also addresses the graphql customization topic. In my view, I think that this PR, together with https://github.com/parse-community/parse-server/pull/5782 and https://github.com/parse-community/parse-server/pull/5818, when merged, closes the issue.
How it works:
1. When initializing ParseGraphQLServer, now the developer can pass a custom schema that will be merged to the auto-generated one:
```
parseGraphQLServer = new ParseGraphQLServer(parseServer, {
graphQLPath: '/graphql',
graphQLCustomTypeDefs: gql`
extend type Query {
custom: Custom @namespace
}
type Custom {
hello: String @resolve
hello2: String @resolve(to: "hello")
userEcho(user: _UserFields!): _UserClass! @resolve
}
`,
});
```
Note:
- This PR includes a @namespace directive that can be used to the top level field of the nested queries and mutations (it basically just returns an empty object);
- This PR includes a @resolve directive that can be used to notify the Parse GraphQL Server to resolve that field using a cloud code function. The `to` argument specifies the function name. If the `to` argument is not passed, the Parse GraphQL Server will look for a function with the same name of the field;
- This PR allows creating custom types using the auto-generated ones as in `userEcho(user: _UserFields!): _UserClass! @resolve`;
- This PR allows to extend the auto-generated types, as in `extend type Query { ... }`.
2. Once the schema was set, you just need to write regular cloud code functions:
```
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
Parse.Cloud.define('userEcho', async req => {
return req.params.user;
});
```
3. Now you are ready to play with your new custom api:
```
query {
custom {
hello
hello2
userEcho(user: { username: "somefolk" }) {
username
}
}
}
```
should return
```
{
"data": {
"custom": {
"hello": "Hello world!",
"hello2": "Hello world!",
"userEcho": {
"username": "somefolk"
}
}
}
}
```
2019-07-18 12:43:49 -07:00
|
|
|
this.graphQLSchemaDirectivesDefinitions = null;
|
|
|
|
|
this.graphQLSchemaDirectives = {};
|
2019-06-19 17:19:47 -07:00
|
|
|
|
|
|
|
|
defaultGraphQLTypes.load(this);
|
|
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
this._getParseClassesWithConfig(parseClasses, parseGraphQLConfig).forEach(
|
|
|
|
|
([parseClass, parseClassConfig]) => {
|
|
|
|
|
parseClassTypes.load(this, parseClass, parseClassConfig);
|
|
|
|
|
parseClassQueries.load(this, parseClass, parseClassConfig);
|
|
|
|
|
parseClassMutations.load(this, parseClass, parseClassConfig);
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-08-14 21:25:28 +02:00
|
|
|
defaultGraphQLTypes.loadArrayResult(this, parseClasses);
|
2019-06-19 17:19:47 -07:00
|
|
|
defaultGraphQLQueries.load(this);
|
|
|
|
|
defaultGraphQLMutations.load(this);
|
|
|
|
|
|
|
|
|
|
let graphQLQuery = undefined;
|
|
|
|
|
if (Object.keys(this.graphQLQueries).length > 0) {
|
|
|
|
|
graphQLQuery = new GraphQLObjectType({
|
|
|
|
|
name: 'Query',
|
|
|
|
|
description: 'Query is the top level type for queries.',
|
|
|
|
|
fields: this.graphQLQueries,
|
|
|
|
|
});
|
2019-08-15 23:23:41 +02:00
|
|
|
this.addGraphQLType(graphQLQuery, true, true);
|
2019-06-19 17:19:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let graphQLMutation = undefined;
|
|
|
|
|
if (Object.keys(this.graphQLMutations).length > 0) {
|
|
|
|
|
graphQLMutation = new GraphQLObjectType({
|
|
|
|
|
name: 'Mutation',
|
|
|
|
|
description: 'Mutation is the top level type for mutations.',
|
|
|
|
|
fields: this.graphQLMutations,
|
|
|
|
|
});
|
2019-08-15 23:23:41 +02:00
|
|
|
this.addGraphQLType(graphQLMutation, true, true);
|
2019-06-19 17:19:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let graphQLSubscription = undefined;
|
|
|
|
|
if (Object.keys(this.graphQLSubscriptions).length > 0) {
|
|
|
|
|
graphQLSubscription = new GraphQLObjectType({
|
|
|
|
|
name: 'Subscription',
|
|
|
|
|
description: 'Subscription is the top level type for subscriptions.',
|
|
|
|
|
fields: this.graphQLSubscriptions,
|
|
|
|
|
});
|
2019-08-15 23:23:41 +02:00
|
|
|
this.addGraphQLType(graphQLSubscription, true, true);
|
2019-06-19 17:19:47 -07:00
|
|
|
}
|
|
|
|
|
|
GraphQL Custom Schema (#5821)
This PR empowers the Parse GraphQL API with custom user-defined schema. The developers can now write their own types, queries, and mutations, which will merged with the ones that are automatically generated. The new types are resolved by the application's cloud code functions.
Therefore, regarding https://github.com/parse-community/parse-server/issues/5777, this PR closes the cloud functions needs and also addresses the graphql customization topic. In my view, I think that this PR, together with https://github.com/parse-community/parse-server/pull/5782 and https://github.com/parse-community/parse-server/pull/5818, when merged, closes the issue.
How it works:
1. When initializing ParseGraphQLServer, now the developer can pass a custom schema that will be merged to the auto-generated one:
```
parseGraphQLServer = new ParseGraphQLServer(parseServer, {
graphQLPath: '/graphql',
graphQLCustomTypeDefs: gql`
extend type Query {
custom: Custom @namespace
}
type Custom {
hello: String @resolve
hello2: String @resolve(to: "hello")
userEcho(user: _UserFields!): _UserClass! @resolve
}
`,
});
```
Note:
- This PR includes a @namespace directive that can be used to the top level field of the nested queries and mutations (it basically just returns an empty object);
- This PR includes a @resolve directive that can be used to notify the Parse GraphQL Server to resolve that field using a cloud code function. The `to` argument specifies the function name. If the `to` argument is not passed, the Parse GraphQL Server will look for a function with the same name of the field;
- This PR allows creating custom types using the auto-generated ones as in `userEcho(user: _UserFields!): _UserClass! @resolve`;
- This PR allows to extend the auto-generated types, as in `extend type Query { ... }`.
2. Once the schema was set, you just need to write regular cloud code functions:
```
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
Parse.Cloud.define('userEcho', async req => {
return req.params.user;
});
```
3. Now you are ready to play with your new custom api:
```
query {
custom {
hello
hello2
userEcho(user: { username: "somefolk" }) {
username
}
}
}
```
should return
```
{
"data": {
"custom": {
"hello": "Hello world!",
"hello2": "Hello world!",
"userEcho": {
"username": "somefolk"
}
}
}
}
```
2019-07-18 12:43:49 -07:00
|
|
|
this.graphQLAutoSchema = new GraphQLSchema({
|
2019-06-19 17:19:47 -07:00
|
|
|
types: this.graphQLTypes,
|
|
|
|
|
query: graphQLQuery,
|
|
|
|
|
mutation: graphQLMutation,
|
|
|
|
|
subscription: graphQLSubscription,
|
|
|
|
|
});
|
|
|
|
|
|
GraphQL Custom Schema (#5821)
This PR empowers the Parse GraphQL API with custom user-defined schema. The developers can now write their own types, queries, and mutations, which will merged with the ones that are automatically generated. The new types are resolved by the application's cloud code functions.
Therefore, regarding https://github.com/parse-community/parse-server/issues/5777, this PR closes the cloud functions needs and also addresses the graphql customization topic. In my view, I think that this PR, together with https://github.com/parse-community/parse-server/pull/5782 and https://github.com/parse-community/parse-server/pull/5818, when merged, closes the issue.
How it works:
1. When initializing ParseGraphQLServer, now the developer can pass a custom schema that will be merged to the auto-generated one:
```
parseGraphQLServer = new ParseGraphQLServer(parseServer, {
graphQLPath: '/graphql',
graphQLCustomTypeDefs: gql`
extend type Query {
custom: Custom @namespace
}
type Custom {
hello: String @resolve
hello2: String @resolve(to: "hello")
userEcho(user: _UserFields!): _UserClass! @resolve
}
`,
});
```
Note:
- This PR includes a @namespace directive that can be used to the top level field of the nested queries and mutations (it basically just returns an empty object);
- This PR includes a @resolve directive that can be used to notify the Parse GraphQL Server to resolve that field using a cloud code function. The `to` argument specifies the function name. If the `to` argument is not passed, the Parse GraphQL Server will look for a function with the same name of the field;
- This PR allows creating custom types using the auto-generated ones as in `userEcho(user: _UserFields!): _UserClass! @resolve`;
- This PR allows to extend the auto-generated types, as in `extend type Query { ... }`.
2. Once the schema was set, you just need to write regular cloud code functions:
```
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
Parse.Cloud.define('userEcho', async req => {
return req.params.user;
});
```
3. Now you are ready to play with your new custom api:
```
query {
custom {
hello
hello2
userEcho(user: { username: "somefolk" }) {
username
}
}
}
```
should return
```
{
"data": {
"custom": {
"hello": "Hello world!",
"hello2": "Hello world!",
"userEcho": {
"username": "somefolk"
}
}
}
}
```
2019-07-18 12:43:49 -07:00
|
|
|
if (this.graphQLCustomTypeDefs) {
|
|
|
|
|
schemaDirectives.load(this);
|
|
|
|
|
|
|
|
|
|
this.graphQLSchema = mergeSchemas({
|
|
|
|
|
schemas: [
|
|
|
|
|
this.graphQLSchemaDirectivesDefinitions,
|
|
|
|
|
this.graphQLAutoSchema,
|
|
|
|
|
this.graphQLCustomTypeDefs,
|
|
|
|
|
],
|
|
|
|
|
mergeDirectives: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const graphQLSchemaTypeMap = this.graphQLSchema.getTypeMap();
|
|
|
|
|
Object.keys(graphQLSchemaTypeMap).forEach(graphQLSchemaTypeName => {
|
|
|
|
|
const graphQLSchemaType = graphQLSchemaTypeMap[graphQLSchemaTypeName];
|
|
|
|
|
if (typeof graphQLSchemaType.getFields === 'function') {
|
|
|
|
|
const graphQLCustomTypeDef = this.graphQLCustomTypeDefs.definitions.find(
|
|
|
|
|
definition => definition.name.value === graphQLSchemaTypeName
|
|
|
|
|
);
|
|
|
|
|
if (graphQLCustomTypeDef) {
|
|
|
|
|
const graphQLSchemaTypeFieldMap = graphQLSchemaType.getFields();
|
|
|
|
|
Object.keys(graphQLSchemaTypeFieldMap).forEach(
|
|
|
|
|
graphQLSchemaTypeFieldName => {
|
|
|
|
|
const graphQLSchemaTypeField =
|
|
|
|
|
graphQLSchemaTypeFieldMap[graphQLSchemaTypeFieldName];
|
|
|
|
|
if (!graphQLSchemaTypeField.astNode) {
|
|
|
|
|
const astNode = graphQLCustomTypeDef.fields.find(
|
|
|
|
|
field => field.name.value === graphQLSchemaTypeFieldName
|
|
|
|
|
);
|
|
|
|
|
if (astNode) {
|
|
|
|
|
graphQLSchemaTypeField.astNode = astNode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
SchemaDirectiveVisitor.visitSchemaDirectives(
|
|
|
|
|
this.graphQLSchema,
|
|
|
|
|
this.graphQLSchemaDirectives
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this.graphQLSchema = this.graphQLAutoSchema;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 17:19:47 -07:00
|
|
|
return this.graphQLSchema;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 23:23:41 +02:00
|
|
|
addGraphQLType(type, throwError = false, ignoreReserved = false) {
|
|
|
|
|
if (
|
|
|
|
|
(!ignoreReserved && RESERVED_GRAPHQL_TYPE_NAMES.includes(type.name)) ||
|
|
|
|
|
this.graphQLTypes.find(existingType => existingType.name === type.name)
|
|
|
|
|
) {
|
|
|
|
|
const message = `Type ${type.name} could not be added to the auto schema because it collided with an existing type.`;
|
|
|
|
|
if (throwError) {
|
|
|
|
|
throw new Error(message);
|
|
|
|
|
}
|
|
|
|
|
this.log.warn(message);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
this.graphQLTypes.push(type);
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addGraphQLObjectQuery(
|
|
|
|
|
fieldName,
|
|
|
|
|
field,
|
|
|
|
|
throwError = false,
|
|
|
|
|
ignoreReserved = false
|
|
|
|
|
) {
|
|
|
|
|
if (
|
|
|
|
|
(!ignoreReserved &&
|
|
|
|
|
RESERVED_GRAPHQL_OBJECT_QUERY_NAMES.includes(fieldName)) ||
|
|
|
|
|
this.graphQLObjectsQueries[fieldName]
|
|
|
|
|
) {
|
|
|
|
|
const message = `Object query ${fieldName} could not be added to the auto schema because it collided with an existing field.`;
|
|
|
|
|
if (throwError) {
|
|
|
|
|
throw new Error(message);
|
|
|
|
|
}
|
|
|
|
|
this.log.warn(message);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
this.graphQLObjectsQueries[fieldName] = field;
|
|
|
|
|
return field;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addGraphQLObjectMutation(
|
|
|
|
|
fieldName,
|
|
|
|
|
field,
|
|
|
|
|
throwError = false,
|
|
|
|
|
ignoreReserved = false
|
|
|
|
|
) {
|
|
|
|
|
if (
|
|
|
|
|
(!ignoreReserved &&
|
|
|
|
|
RESERVED_GRAPHQL_OBJECT_MUTATION_NAMES.includes(fieldName)) ||
|
|
|
|
|
this.graphQLObjectsMutations[fieldName]
|
|
|
|
|
) {
|
|
|
|
|
const message = `Object mutation ${fieldName} could not be added to the auto schema because it collided with an existing field.`;
|
|
|
|
|
if (throwError) {
|
|
|
|
|
throw new Error(message);
|
|
|
|
|
}
|
|
|
|
|
this.log.warn(message);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
this.graphQLObjectsMutations[fieldName] = field;
|
|
|
|
|
return field;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 17:19:47 -07:00
|
|
|
handleError(error) {
|
|
|
|
|
if (error instanceof Parse.Error) {
|
|
|
|
|
this.log.error('Parse error: ', error);
|
|
|
|
|
} else {
|
|
|
|
|
this.log.error('Uncaught internal server error.', error, error.stack);
|
|
|
|
|
}
|
2019-07-12 17:58:47 -03:00
|
|
|
throw toGraphQLError(error);
|
2019-06-19 17:19:47 -07:00
|
|
|
}
|
2019-07-25 20:46:25 +01:00
|
|
|
|
|
|
|
|
async _initializeSchemaAndConfig() {
|
|
|
|
|
const [schemaController, parseGraphQLConfig] = await Promise.all([
|
|
|
|
|
this.databaseController.loadSchema(),
|
|
|
|
|
this.parseGraphQLController.getGraphQLConfig(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
this.schemaController = schemaController;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
parseGraphQLConfig,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets all classes found by the `schemaController`
|
|
|
|
|
* minus those filtered out by the app's parseGraphQLConfig.
|
|
|
|
|
*/
|
|
|
|
|
async _getClassesForSchema(parseGraphQLConfig: ParseGraphQLConfig) {
|
|
|
|
|
const { enabledForClasses, disabledForClasses } = parseGraphQLConfig;
|
|
|
|
|
const allClasses = await this.schemaController.getAllClasses();
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(enabledForClasses) || Array.isArray(disabledForClasses)) {
|
|
|
|
|
let includedClasses = allClasses;
|
|
|
|
|
if (enabledForClasses) {
|
|
|
|
|
includedClasses = allClasses.filter(clazz => {
|
|
|
|
|
return enabledForClasses.includes(clazz.className);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (disabledForClasses) {
|
|
|
|
|
// Classes included in `enabledForClasses` that
|
|
|
|
|
// are also present in `disabledForClasses` will
|
|
|
|
|
// still be filtered out
|
|
|
|
|
includedClasses = includedClasses.filter(clazz => {
|
|
|
|
|
return !disabledForClasses.includes(clazz.className);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.isUsersClassDisabled = !includedClasses.some(clazz => {
|
|
|
|
|
return clazz.className === '_User';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return includedClasses;
|
|
|
|
|
} else {
|
|
|
|
|
return allClasses;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method returns a list of tuples
|
|
|
|
|
* that provide the parseClass along with
|
|
|
|
|
* its parseClassConfig where provided.
|
|
|
|
|
*/
|
|
|
|
|
_getParseClassesWithConfig(
|
|
|
|
|
parseClasses,
|
|
|
|
|
parseGraphQLConfig: ParseGraphQLConfig
|
|
|
|
|
) {
|
|
|
|
|
const { classConfigs } = parseGraphQLConfig;
|
2019-08-15 23:23:41 +02:00
|
|
|
|
|
|
|
|
// Make sures that the default classes and classes that
|
|
|
|
|
// starts with capitalized letter will be generated first.
|
|
|
|
|
const sortClasses = (a, b) => {
|
|
|
|
|
a = a.className;
|
|
|
|
|
b = b.className;
|
|
|
|
|
if (a[0] === '_') {
|
|
|
|
|
if (b[0] !== '_') {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (b[0] === '_') {
|
|
|
|
|
if (a[0] !== '_') {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (a === b) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else if (a < b) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return parseClasses.sort(sortClasses).map(parseClass => {
|
2019-07-25 20:46:25 +01:00
|
|
|
let parseClassConfig;
|
|
|
|
|
if (classConfigs) {
|
|
|
|
|
parseClassConfig = classConfigs.find(
|
|
|
|
|
c => c.className === parseClass.className
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return [parseClass, parseClassConfig];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks for changes to the parseClasses
|
|
|
|
|
* objects (i.e. database schema) or to
|
|
|
|
|
* the parseGraphQLConfig object. If no
|
|
|
|
|
* changes are found, return true;
|
|
|
|
|
*/
|
|
|
|
|
_hasSchemaInputChanged(params: {
|
|
|
|
|
parseClasses: any,
|
|
|
|
|
parseClassesString: string,
|
|
|
|
|
parseGraphQLConfig: ?ParseGraphQLConfig,
|
|
|
|
|
}): boolean {
|
|
|
|
|
const { parseClasses, parseClassesString, parseGraphQLConfig } = params;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
JSON.stringify(this.parseGraphQLConfig) ===
|
|
|
|
|
JSON.stringify(parseGraphQLConfig)
|
|
|
|
|
) {
|
|
|
|
|
if (this.parseClasses === parseClasses) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.parseClassesString === parseClassesString) {
|
|
|
|
|
this.parseClasses = parseClasses;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { ParseGraphQLSchema };
|