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"
      }
    }
  }
}
```
This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-07-18 12:43:49 -07:00
committed by Douglas Muraoka
parent 0b86a86209
commit 71d92aed8d
6 changed files with 239 additions and 38 deletions

View File

@@ -189,7 +189,9 @@ describe('ParseGraphQLServer', () => {
});
});
describe('API', () => {
describe('Auto API', () => {
let httpServer;
const headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-Javascript-Key': 'test',
@@ -340,7 +342,7 @@ describe('ParseGraphQLServer', () => {
beforeAll(async () => {
const expressApp = express();
const httpServer = http.createServer(expressApp);
httpServer = http.createServer(expressApp);
expressApp.use('/parse', parseServer.app);
ParseServer.createLiveQueryServer(httpServer, {
port: 1338,
@@ -384,6 +386,10 @@ describe('ParseGraphQLServer', () => {
});
});
afterAll(async () => {
await httpServer.close();
});
describe('GraphQL', () => {
it('should be healthy', async () => {
const health = (await apolloClient.query({
@@ -5243,4 +5249,113 @@ describe('ParseGraphQLServer', () => {
});
});
});
describe('Custom API', () => {
let httpServer;
const headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-Javascript-Key': 'test',
};
let apolloClient;
beforeAll(async () => {
const expressApp = express();
httpServer = http.createServer(expressApp);
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
}
`,
});
parseGraphQLServer.applyGraphQL(expressApp);
await new Promise(resolve => httpServer.listen({ port: 13377 }, resolve));
const httpLink = createUploadLink({
uri: 'http://localhost:13377/graphql',
fetch,
headers,
});
apolloClient = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
defaultOptions: {
query: {
fetchPolicy: 'no-cache',
},
},
});
});
afterAll(async () => {
await httpServer.close();
});
it('can resolve a custom query using default function name', async () => {
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
const result = await apolloClient.query({
query: gql`
query Hello {
custom {
hello
}
}
`,
});
expect(result.data.custom.hello).toEqual('Hello world!');
});
it('can resolve a custom query using function name set by "to" argument', async () => {
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
const result = await apolloClient.query({
query: gql`
query Hello {
custom {
hello2
}
}
`,
});
expect(result.data.custom.hello2).toEqual('Hello world!');
});
it('should resolve auto types', async () => {
Parse.Cloud.define('userEcho', async req => {
return req.params.user;
});
const result = await apolloClient.query({
query: gql`
query UserEcho($user: _UserFields!) {
custom {
userEcho(user: $user) {
username
}
}
}
`,
variables: {
user: {
username: 'somefolk',
},
},
});
expect(result.data.custom.userEcho.username).toEqual('somefolk');
});
});
});