Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

feat(templates): adding external templates #2215

Closed
wants to merge 13 commits into from
Closed
59 changes: 59 additions & 0 deletions COMMUNITY-TEMPLATES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Using Graphback With Your Own Templates

In case you want to use some other templates than the ones offered, Graphback also supports adding your templates. The process is fairly simple and is as follows:

## Step 1: Creating an object for your template

To add your template, you first need to have it in the form of an object which you'll later add to an array. This is best explained by an example:

Let's say you have a template which you want to name `a-sample-template` and the files for it are located in the repository `https://github.com/sample-user/sample-project`. Then the object for your template would look like this:

```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ts file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the url should point to a particular ts file on the person's repository?

{
"name": "a-sample-template",
"description": "Fetching current templates in the sample template",
"repos": [
{
"uri": "https://github.com/sample-user/sample-project",
"branch": "specify-desired-branch",
"path": "exact-path-of-the-directory",
"mountpath": "client"
},
{
"uri": "https://github.com/sample-user/sample-project",
"branch": "specify-desired-branch",
"path": "exact-path-of-the-directory"
}
],
}
```

Note that you can specify as many repositories as you want (with the desired branch and mount path). Files from the specified repositories would be picked up for your template.

## Step 2: Adding your template object

In the packages directory of Graphback you'll find the file `community-templates.ts`. This exports an array called `externalTemplates`. It is in this array that you should add your template object for it to be picked up by the CLI. So the final file would look something like this:

```
export default externalTemplates = [
{
"name": "a-sample-template",
"description": "Fetching current templates in the sample template",
"repos": [
{
"uri": "https://github.com/sample-user/sample-project",
"branch": "specify-desired-branch",
"path": "exact-path-of-the-directory",
"mountpath": "client"
},
{
"uri": "https://github.com/sample-user/sample-project",
"branch": "specify-desired-branch",
"path": "exact-path-of-the-directory"
}
],
}
]
```

With this, you're good to go. Yes, it's that simple! If you still have any doubts or are facing problems feel free to reach out to us!
1 change: 1 addition & 0 deletions packages/community-templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default externalTemplates = [];
13 changes: 10 additions & 3 deletions packages/create-graphback/src/init/starterTemplates.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { createWriteStream, mkdirSync, existsSync } from 'fs';
import chalk from 'chalk';
import ora from 'ora'
import * as github from 'parse-github-url';
import * as request from 'request';
import github from 'parse-github-url';
import request from 'request';
import * as tar from 'tar';
import * as tmp from 'tmp';
import externalTemplates from '../../../community-templates';
import { Template, TemplateRepository } from './templateMetadata';

/**
* available templates
*/
export const allTemplates: Template[] = [
export let allTemplates: Template[] = [
{
name: 'apollo-fullstack-react-postgres-ts',
description: 'Apollo GraphQL Server connecting to Postgres database and React client using TypeScript',
Expand Down Expand Up @@ -113,6 +114,12 @@ export const allTemplates: Template[] = [
}
];

const externalTemplatesArray: Template[] = externalTemplates;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also improve how we present those templates - Graphback and community templates when executing create-graphback cmd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing the code @wtrocki. Do you have any suggestions about how I could go on implementing this? All templates must still reside in the allTemplates array for the code to function as expected, right? So how can I go about differentiating the templates in the CLI? A simple solution I can think of is appending (community) to the name of each template I import from the json file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/aerogear/graphback/blob/master/packages/create-graphback/src/init/components.ts#L31-L53

Experiment with this methods. Now all will be printed there.
If we can split them into two categories etc. that will be awesome.

externalTemplatesArray.forEach(
template => (template.name = 'Community: ' + template.name)
);
allTemplates = allTemplates.concat(externalTemplatesArray);

/**
* information about repository
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/graphback-core/src/runtime/CRUDService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as DataLoader from "dataloader";
import DataLoader from "dataloader";
import { PubSubEngine, withFilter } from 'graphql-subscriptions';
import { GraphQLResolveInfo } from 'graphql';
import { GraphbackCRUDGeneratorConfig, GraphbackOperationType, upperCaseFirstChar, getSubscriptionName } from '..';
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"noUnusedParameters": false,
"noImplicitAny": false,
"noImplicitThis": false,
"resolveJsonModule": true,
"esModuleInterop": true,
"strictNullChecks": false,
"lib": [
"esnext"
Expand Down