|
2 | 2 |
|
3 | 3 | Apollo Server to support GraphQL interactions with the UI and external partners. |
4 | 4 |
|
5 | | -To run locally: `npm start` |
| 5 | +Our Apollo server installation consists of: |
| 6 | +- Data Sources: Code used to communicate with external APIs, databases, file systems, etc. |
| 7 | +- GraphQL Schemas: The definition of our Graph including types, queries and mutations |
| 8 | +- Resolvers: The code that processes incoming queries and uses the available data sources to generate the results |
| 9 | +- Mocks: Stub or placeholder data that can be returned when a resolver has not yet been developed |
| 10 | + |
| 11 | +## Installation |
| 12 | +- Make a local copy of the example dotenv file: `cp .env-example .env` |
| 13 | +- Setup MySQL: |
| 14 | + - If you are running on a Mac: |
| 15 | + - If you have homebrew installed, run `brew install mysql` and then start it `brew services start mysql` |
| 16 | + - Initialize the Database and the dataMigrations table: `./data-migrations/database-init.sh` |
| 17 | + - Run all database migrations: `./data-migrations/process.sh` |
| 18 | +- Install all of the dependencies: `npm install` |
| 19 | +- Generate the Typescript types: `npm run generate` |
| 20 | +- Startup the application in development mode: `npm run dev` |
| 21 | +- Navigate to `http://localhost:4000` in your browser |
| 22 | + |
| 23 | +## Running current database migrations |
| 24 | +- See the readme file in the `data-migrations` directory for instructions on running data migrations in your local environment. |
| 25 | + |
| 26 | +## Adding a new query/mutation |
| 27 | +You should always start by updating an existing GraphQL Schema file or adding a new one to the `src/schemas` directory. |
| 28 | + |
| 29 | +Please include comments everywhere. These comments appear in the GraphQL explorer and help others undertand how to interact with the query or mutation. |
| 30 | + |
| 31 | +If you added a new schema file, make sure you update the `src/schemas.ts` file to pull in the new file when the server starts up. |
| 32 | + |
| 33 | +Once the schema has been added, you will need to run `npm run generate` this kicks off a script that builds out Typescript Types for the new schema and queries. |
| 34 | + |
| 35 | +### Create a new Model |
| 36 | +You will need to create a Model if your new query/mutation will need to transform the response from the data source in any way prior to sending it to the caller or to the data source. |
| 37 | + |
| 38 | +For example: |
| 39 | +- If my data source returns a property called `funder_id` and I want to send a boolean flag called `isFunder` to the caller, I perform the logic in a Model. |
| 40 | +- If I simply want to rename a property prior to returning it to the client like the data source returning `identifier` but needing to send `DMPId` to the caller. |
| 41 | + |
| 42 | +Make sure that you transform the raw response from the data source into your Model in your new resolver. |
| 43 | +For example: |
| 44 | +``` |
| 45 | +const response = await someDataSource.query('test'); |
| 46 | +return new MyModel(response); |
| 47 | +``` |
| 48 | + |
| 49 | +### Create a Mock |
| 50 | +If you will be unable to create the corresponding resolver(s) at this point because of time constraints or because the data source is not yet ready, then you should add a new Mock file to the `src/schemas/` directory (or update and existing one with your changes). If you add a new mock be sure to update the `src/mocks.ts` to pull in your new mock when the server starts up. |
| 51 | + |
| 52 | +Note that mocks should represent the data structure that will be returned from your resolver to the caller. NOT the dtat structure that the resolver receives from the data source! |
| 53 | + |
| 54 | +### Create a Resolver |
| 55 | +If your data source is ready and you have the bandwidth, add a new Resolver to the `src/resolvers/` directory (or update one with your new query/mutation). If you add a new Resolver be sure to update the `src/resolvers.ts` file to make sure it is included when the server starts up. |
| 56 | + |
| 57 | +### Add tests |
| 58 | +You MUST add tests if you added or modified a Model! To do so, find the corresponding file (or add a new one) in the `src/models/__tests_/` directory. |
| 59 | + |
| 60 | +Resolver tests are not yet particularly useful. We will be updating this to add these integration tests in the near future. |
| 61 | + |
| 62 | +## Useful commands |
| 63 | +- To run the Codegen utility to generate our Typescript types: `npm run generate` |
| 64 | +- To run the server in development mode: `npm run dev` |
| 65 | +- To run the server normally: `npm start` |
| 66 | +- To build the application: `npm run build` |
0 commit comments